Skip to content

internal/json: replace segmentio/encoding with encoding/json/v2 - #1287

Open
nahapetyan-serob wants to merge 7 commits into
mainfrom
serob/use-encoding-json-v2
Open

nahapetyan-serob wants to merge 7 commits into
mainfrom
serob/use-encoding-json-v2

Conversation

@nahapetyan-serob

Copy link
Copy Markdown

segmentio/encoding was added in #807 for one method: DontMatchCaseInsensitiveStructFields(). The json/v2 API does strict field-name matching natively, so the dependency isn't needed.

Behavior is unchanged. DefaultOptionsV1() keeps all v1 semantics — duplicate names permitted, invalid UTF-8 replaced, v1 null/merge handling, any-length arrays — and MatchCaseInsensitiveNames(false) overrides the one we don't want. No v2 strictness is adopted here. internal/json/json_test.go is untouched and passes as-is.

UnmarshalUseNumber stays on the v1 decoder and remains case-insensitive: no public API combines UseNumber with strict matching.

go-json-experiment/json hosts a mirror of encoding/json/v2 with the goexperiment.jsonv2 build tag (which is enabled by default on Go 1.27)

Breaking: go.mod goes 1.25.0 → 1.26 (the dependency declares it); Go 1.25 is no longer supported. CI matrix ["1.25","1.26"] → ["1.26","1.27"], which also covers both of the dependency's build paths.

@nahapetyan-serob
nahapetyan-serob force-pushed the serob/use-encoding-json-v2 branch from 5818f09 to d0941b2 Compare September 23, 2026 09:14
Comment thread internal/json/json.go Outdated
return err
}
return NewDecoder(bytes.NewReader(data)).Decode(v)
return jsonv2.Unmarshal(data, v, caseSensitiveV1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not using the Decoder also in this case? In this way the behavior is 100% not changed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Will go back to using the Decoder.

guglielmo-san
guglielmo-san previously approved these changes Sep 25, 2026
@raveesh-me

Copy link
Copy Markdown
Contributor

Hi @nahapetyan-serob, thanks for this. Dropping the segmentio dependency would be a nice simplification.

I noticed one interaction with a change that landed after this PR was opened, and wanted to flag it before it merges. Since #1401, jsonrpc2.DecodeMessage decodes every message envelope with internaljson.UnmarshalUseNumber, to keep large numeric IDs exact. In this PR, UnmarshalUseNumber moves to encoding/json's decoder, which matches field names case-insensitively. So with this branch merged into current main, the envelope is case-insensitive again, which is the behaviour #807 fixed (GHSA-wvj2-96wp-fq3f).

A minimal check, with this branch merged into main:

m, err := jsonrpc2.DecodeMessage([]byte(`{"jsonrpc":"2.0","id":1,"method":"tools/list","Method":"tools/call"}`))
// m.(*jsonrpc2.Request).Method is "tools/call"; on main it is "tools/list".

{"jsonrpc":"2.0","id":1,"METHOD":"tools/call"} is also accepted as a tools/call request; on main it isn't decoded as a request at all.

The merge is clean and the existing case-sensitivity tests only cover Unmarshal, so nothing currently catches it. Keeping UnmarshalUseNumber case-sensitive should be enough. #1454 (an unrelated allocation fix in the same file) adds TestUnmarshalUseNumberCaseSensitivity, which fails on this branch merged with main and would guard it whichever PR lands first.

On how the two PRs relate: #1454 keeps the current dependency and only changes how Unmarshal reads its input. With v2's decoder the per-call buffer is already small, so if this PR lands first, most of #1454's code change wouldn't be needed. I'd rebase it down to the tests.

@nahapetyan-serob

nahapetyan-serob commented Oct 26, 2026 •

Copy link
Copy Markdown
Author

Thanks @raveesh-me, good catch, confirmed. It's fixed on this branch:
UnmarshalUseNumber now uses the same case-sensitive v2 decoder as Unmarshal,
with a custom unmarshaler that keeps numbers in an any as encoding/json.Number.
I'm going to hold this PR until Go 1.28 is released. The minimum supported version
then becomes 1.27, which has encoding/json/v2 in the standard library, so we can
migrate to it directly instead of depending on go-json-experiment/json.
In the meantime I'll review #1454 for the buffer issue (#1453). This PR will be
rebased on top of it later, and your tests will carry over.

guglielmo-san added a commit that referenced this pull request Oct 28, 2026
…#1454)

`internal/json.Unmarshal` decoded through segmentio's streaming
`json.Decoder`, which copies its input into a read buffer of at least 32
KiB on every call. Unmarshal runs several times per request, so these
buffers made up most of the memory allocated to serve a tool call (528
KiB per call in `BenchmarkStreamableServing`). Details in #1453.

This change decodes in place:

- `json.Parse` into a `json.RawMessage` with `DontCopyRawMessage` finds
the extent of the first JSON value. It's the same `parseValue` the
Decoder runs on its buffer.
- That value is then decoded with the same flags as before.
- If finding the value fails, the input is malformed and goes through a
`Decoder` as before. Errors are therefore exactly today's: `io.EOF` for
empty input, `io.ErrUnexpectedEOF` for truncated input, and the same
`SyntaxError` and `UnmarshalTypeError` text.

Case-sensitive matching and the depth limit are unchanged.

One function, `newDecoder`, configures the Decoder, and `parseFlags`
gives `json.Parse` the same rules. `TestUnmarshalSameAsStream` and
`FuzzUnmarshalSameAsStream` compare the new path with the old one for
`Unmarshal` and `UnmarshalUseNumber` into several target types, checking
the decoded value, the error type and the error text. Locally the fuzzer
ran about 5 million inputs without finding a difference.
`TestUnmarshalDoesNotAliasInput` checks that decoded values never share
memory with the input, since the value is now found in place and callers
may reuse their buffers. `TestUnmarshalUseNumberCaseSensitivity` covers
`UnmarshalUseNumber`, which decodes every JSON-RPC envelope since #1401;
the existing case-sensitivity tests only cover `Unmarshal`.

```
name                  old time/op    new time/op    delta
StreamableServing     368.7µs ± 2%   219.3µs ± 1%   -40.51%
Unmarshal             4.015µs ± 1%   1.337µs ± 1%   -66.71%

name                  old alloc/op   new alloc/op   delta
StreamableServing     528.3KiB ± 0%  60.1KiB ± 1%   -88.62%
Unmarshal             37.41KiB ± 0%  5.39KiB ± 0%   -85.59%
```

Run with `go test ./...`, `go test -race ./...`, `go vet ./...`,
staticcheck and `go generate ./...` on Go 1.26.6, and `internal/json`
and `mcp` on Go 1.25.0.

This covers every call site, including the one #1395 changes, and
doesn't conflict with #1287.

Fixes #1453

Co-authored-by: Guglielmo Colombo <guglielmoc@google.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants