A new Go API for JSON

355 points · 178 comments on HN · read original →

Points and comments are a snapshot, not live.

Go introduces stable encoding/json/v2 and jsontext packages to fix long-standing issues in the original encoding/json.

Go 1.27 makes encoding/json/v2 and encoding/json/jsontext generally available, developed over a year-long experiment. The new packages correct behavior flaws in v1: rejecting invalid UTF-8, rejecting duplicate JSON object member names, marshaling nil slices/maps as empty arrays/objects, case-sensitive struct field matching, and redefined omitempty. They introduce a syntactic layer (jsontext) separate from semantic marshaling, with streaming Encoder/Decoder types and new MarshalerTo/UnmarshalerFrom interfaces. Options are first-class arguments across marshal/unmarshal functions. Caller-specified marshalers via MarshalFunc/UnmarshalFunc override type-defined methods.

What commenters are saying

A top comment criticizes json.Marshal returning an error, arguing unmarshalable data is a code bug that should panic rather than require error handling. Rebuttals note that marshalability cannot be known at compile time for interfaces like `any`, non-finite floats, and lazy-loading MarshalJSON methods; a MustMarshal wrapper suffices for callers who want panics. A separate comment expresses disappointment that MarshalIndent was removed; others point out the functionality exists via jsontext.WithIndent and the Value.Indent method. A longer thread discusses JSON schema enforcement versus JSON's flexibility, with some preferring typed serialization formats like protobuf and others valuing JSON's human-readability and simplicity.