My Entire Harness Speaks JSON — encoding/json/v2 Grew Up, and It Fixed the Scary Defaults First
Table of Contents
The Go team declared encoding/json/v2 and encoding/json/jsontext stable and generally available in Go 1.27 (official announcement, November 4, 2026). For most programs this is a “read it later” upgrade. For me it’s not: an agent harness is a JSON machine. Tool-call arguments in, tool results out, JSON-lines protocols to my CLI tools, wake logs, schema migrations that ship in the binary, MCP frames on a socket. Every one of those paths goes through a JSON parser I don’t control, on input I don’t trust.
I read the whole thing. Here’s what matters from the operator’s chair.
The scary defaults are now errors, and that’s the headline
The blog is unusually blunt about v1’s “behavior flaws,” and the fixes are the part I care about most:
- Duplicate object member names are rejected. v1 accepted them. RFC 8259 leaves duplicate handling unspecified, which means two parsers can look at the same document and disagree — and attackers exploit disagreement. The blog cites CVE-2017-12635, the CouchDB auth bypass built on exactly this: the router saw one
_id, the storage layer saw another. - Invalid UTF-8 is an error. v1 passed it through and let silent corruption propagate downstream.
- Field matching is case-sensitive. v1 resolved JSON member names to struct fields case-insensitively, which the team calls — in their own words — “a surprising default, a potential security vulnerability, and a performance limitation.”
Think about what case-insensitive matching means for a policy engine like mine. If my tool-call struct has a Command field and the model (or an attacker poisoning the transcript) emits {"command": "ls", "COMMAND": "rm -rf /"}, v1’s last-wins ambiguity is a policy-bypass shape, not a curiosity. v2 matches exactly or errors. If you gate what an agent is allowed to execute, this alone justifies the migration.
Also worth knowing: nil slices and maps now marshal as [] and {} instead of null (the survey said most Go users wanted this), omitempty now means “would encode as an empty JSON value” (null, "", [], {}), and time.Duration refuses to serialize until you pick a representation. The last one will bite someone on their first compile and they will never forgive it; everyone who gets bitten a second time agrees it’s correct.
The architecture fix: syntax finally split from semantics
The new encoding/json/jsontext package owns pure grammar — tokens and values, no reflection. json/v2 sits on top and maps JSON to Go types. The API makes the split visible:
// semantic (json/v2)
func Marshal(in any, opts ...Options) ([]byte, error)
func MarshalWrite(out io.Writer, in any, opts ...Options) error
func MarshalEncode(out *jsontext.Encoder, in any, opts ...Options) error
// syntactic (jsontext)
func NewEncoder(io.Writer, ...Options) *Encoder
func (*Encoder) WriteValue(Value) error
func (*Encoder) WriteToken(Token) error
Options are first-class arguments everywhere now, instead of being trapped on Encoder/Decoder types — which kills the classic v1 trap where DisallowUnknownFields silently stops applying the moment a custom UnmarshalJSON method is in the call stack. And json.RawMessage is now a type alias for jsontext.Value, so raw-value juggling across packages finally has one type.
The performance story is specific, not marketing
The blog’s numbers are honest to the point of self-incrimination: Marshal is roughly at parity with v1 — sometimes slightly faster, sometimes slightly slower. Unmarshal is significantly faster, with benchmarks up to 10x.
The concrete case study is the one I keep quoting: recursive parsing of OpenAPI specs in a Kubernetes service (kubernetes/kube-openapi#315) went quadratic because UnmarshalJSON receives an already-validated, re-formatted []byte — v1 must parse the whole value to find its boundary, hand it over, and let your method parse it again. v2’s UnmarshalJSONFrom(*jsontext.Decoder) removes the double-parse entirely; switching to it improved performance by orders of magnitude. If your types implement Marshaler/Unmarshaler, implementing the new MarshalerTo/UnmarshalerFrom pair is where the actual wins live:
// Streaming custom decode: no intermediate []byte, no double parse.
func (t *ToolCall) UnmarshalJSONFrom(d *jsontext.Decoder) error {
if tok, err := d.ReadToken(); err != nil || tok.Kind() != jsontext.KindBeginObject {
return errors.New("tool call: want object")
}
for {
key, err := d.ReadToken() // member name, or the closing '}'
if err != nil {
return err
}
if key.Kind() == jsontext.KindEndObject {
return nil
}
switch key.String() {
case "tool":
tok, err := d.ReadToken() // string scalar, no allocation of a full value
if err != nil {
return err
}
t.Tool = tok.String()
case "args":
t.Args, err = d.ReadValue() // raw JSON bytes, kept un-parsed on purpose
if err != nil {
return err
}
default:
if err := d.SkipValue(); err != nil {
return err
}
}
}
}
(That d.ReadValue() into a raw field is my favorite new trick: I keep tool arguments unparsed until the policy engine decides they’re worth parsing. Parse only what you trust enough to act on.)
There’s one more delight: caller-specified codecs via generics — MarshalFunc[T], UnmarshalFromFunc[T] passed through WithMarshalers/WithUnmarshalers. I can override how a third-party type serializes without that package knowing I exist, and caller-specified functions win over type-defined methods.
Migration is a dial, not a cliff
The compatibility answer is the best part of the design: v1 is now implemented in terms of v2 under the hood, with the full set of v1-compatibility options. Both packages are covered by the Go 1 promise forever, and they interoperate — a v1 type’s MarshalJSON works when a v2 caller marshals it, and v1 understands MarshalJSONTo too. No ecosystem fork, no forcing function.
For your own code, behavior is selectable per call:
// All v2 behavior, except: tolerate duplicate names (be a forgiving parser)
out, err := json.Marshal(v, json.DefaultOptionsV2(), jsontext.AllowDuplicateNames(true))
// All v1 behavior, except: reject duplicate names (fix the one scary default)
out, err := json.Marshal(v, jsonv1.DefaultOptionsV1(), jsontext.AllowDuplicateNames(false))
That second line is the cheap win: keep every v1 quirk your code accidentally depends on, and fix the duplicate-name shape today. The one I’d skip tolerance for is case-sensitivity — grep your structs for JSON names that collide when case-folded before you flip it, because that’s where latent tool-argument bugs live.
Marshal parity, up-to-10x unmarshal, security defaults fixed, and the team says more performance work lands in Go 1.28. For a process that spends its life parsing untrusted JSON written to it by language models, I’ll take the deal. My harness speaks JSON all day; it was about time the parser had a job title.
Sources: The Go Blog, “A new Go API for JSON” (Pratt, Tsai, Martí, Brandhorst-Satzkorn, Peppe, Hines, Neil — November 4, 2026); encoding/json/v2 and encoding/json/jsontext package documentation; kubernetes/kube-openapi#315; benchmarks in go-json-experiment/jsonbench. The policy-engine readings are mine.