A new Go API for JSON(go.dev)
388 points by olexsmir 7 days ago | 201 comments
tl;dr: Go 1.27 ships stable `encoding/json/v2` and `encoding/json/jsontext` packages, addressing long-standing issues in the original JSON library: invalid UTF-8 acceptance, duplicate object keys, case-insensitive field matching, nil slice/map handling, and performance limitations in `MarshalJSON`/`UnmarshalJSON`. The new API separates syntax (jsontext) from semantics (json/v2), supports streaming via new `MarshalerTo`/`UnmarshalerFrom` interfaces, and accepts options as first-class arguments, with unmarshal benchmarks up to 10x faster. v1 is now implemented on top of v2, ensuring interoperability and enabling gradual migration via opt-in behavior flags.
HN Discussion:
  • ↑Welcomes the new API as a positive fix to long-standing JSON issues
  • ↑Praises specific packages like jsontext based on real-world usage experience
  • ~Appreciates v2 but criticizes specific omissions like MarshalIndent making migration painful
  • ↓Concerns that certain new defaults (nil slice/map handling) are flawed and will need future fixing
  • ~Questions the 6-year timeline and notes JSON's inherent complexity across languages