A New Go API for JSON go.dev · 88 points · 22 comments · HN thread TL;DR The Go ...

@best

A New Go API for JSON go.dev · 88 points · 22 comments · HN thread

TL;DR The Go team announces that encoding/json/v2 and encoding/json/jsontext are stable and generally available in Go 1.27, after being an experiment since Go 1.25. The post explains the long-standing flaws of encoding/json (accepting invalid UTF-8 and duplicate names, nil slices marshaling as null, case-insensitive field matching, costly MarshalJSON/UnmarshalJSON) and how v2 fixes them while keeping v1 compatible through options.

  • v2 rejects invalid UTF-8 and duplicate object member names by default, and matches JSON names to struct fields case-sensitively.
  • A nil Go slice or map now marshals as an empty JSON array or object instead of null.
  • New jsontext package provides a streaming, syntax-only Encoder/Decoder without reflection; new MarshalerTo and UnmarshalerFrom interfaces let types stream JSON directly.
  • Unmarshal performance is reported up to 10x faster than v1; Marshal is roughly at parity.
  • v1 is now implemented in terms of v2 with a full set of compatibility options, so both versions interoperate and migration can happen call site by call site.
  • The project took about six years from first draft in late 2020, with much of the work done by contributors not employed by Google.

Discussion The thread is short and mostly curious rather than hostile. Commenters react to the six-year timeline, with some reading it as careful design and others as a lot of effort for a JSON API. A recurring practical topic is the removal of MarshalIndent, which one commenter found hard to replace; two others pointed out that indenting is available through jsontext.Value.Indent or jsontext.WithIndent. A side debate about why Marshal returns an error turns into a discussion of runtime data versus compile-time types. Several threads also touch on how Go fits into an LLM-assisted workflow.

  • Glad the redesign happened and it looks good (many): Several commenters say they generally like v2 and are glad the Go team fixed long-standing problems in encoding/json rather than leaving them alone.
  • Six years is a long time for a JSON API (some): One commenter finds the timeline surprising, though they appreciate the team taking care with interfaces before shipping.
  • Removing MarshalIndent makes quick code harder (some): A commenter wishes MarshalIndent had stayed, since the replacement is non-obvious and awkward for quick debugging output. Others replied that the functionality moved to jsontext.
  • Marshal should not return an error (a few): One commenter argues a marshal error is really a code bug and should panic or be caught at compile time. Others reply that data can be dynamic (any, custom MarshalJSON, lazy loading), so an error is needed.
  • Go is becoming a target language for LLM-written code (a few): Commenters note that they increasingly prompt models rather than write Go by hand, and that Go's simplicity suits smaller contexts for local LLMs.
  • Schema-specialized parsing could bring simdjson-level speed (a few): One commenter, working on Dart, describes generating schema-specific parsers that could match simdjson performance, and links academic work on staged parsing.

Links from the comments

#go #json #stdlib #encoding #performance #migration