A new Go API for JSON
Go’s new `encoding/json/v2` API prompts debate over how JSON serialization should balance type safety, performance, and ergonomics in a relatively simple language. Commenters weigh runtime reflection-based marshalling against compile-time guarantees seen in languages like Rust and Haskell, argue over defaults such as how nil slices and maps are encoded, and note the trade-offs in supporting schemas or stronger typing on a format designed for flexibility. The change is also viewed in the broader context of Go’s deliberately slow standard-library evolution, its emerging role as a good fit for LLM-generated code, and the new practice of versioned stdlib packages like `json/v2` and `math/rand/v2` to ship breaking improvements without disrupting existing code.
Marshal API, Errors, and Type Safety
- Debate over why
json.Marshalreturns an error:- One side: errors for marshalling are usually programmer bugs (unmarshallable types), and should be caught at compile time or even panic.
- Others: input can include types whose marshalability is only known at runtime (
any, non-finite floats, customMarshalJSONerrors, lazy loading failures), so returningerroris justified.
- Some argue Go’s reflection-based JSON is “implicit” and that other languages offer compile‑time type-safe (de)serialization.
- Counterpoint: Go’s general
any/interface-based API is intentionally reusable; if you want stronger guarantees, use typed structs or different APIs.
New JSON v2 API & Usability
- Many are positive on
encoding/json/v2, especially its cleaner API and options. - Some miss
MarshalIndent; others point out equivalent functionality viajsontext.Value.IndentorWithIndentoptions, though it’s seen as less obvious. - v2 adds features like better options configuration, but doesn’t radically change abstractions.
Nil vs Empty Collections in JSON
- New default: nil slices/maps serialize as empty arrays/objects.
- Supporters: matches how most Go code conceptually treats nil vs empty; v1 behavior caused bugs.
- Critics: conflating nil and empty breaks exact round‑tripping and complicates PATCH/partial‑update semantics where
nullvs “missing” matters. - Broader complaint: Go’s “zero value” semantics and nil behavior make precise (de)serialization tricky.
Performance, Schemas, and Advanced Parsing
- Discussion of staged parsing / SIMD / schema‑aware parsing (referencing academic work).
- Some claim “most JSON users don’t want schemas/strong typing”; others strongly disagree, citing heavy use of JSON Schema, OpenAPI, and typed libraries (e.g., serde-style designs).
- Consensus: JSON is simple as a format, but robust, fast, fully correct implementations with all edge cases (numbers, duplicate keys, limits) are complex.
Go’s Ecosystem, Velocity, and LLM Era
- Mixed feelings about Go’s slow evolution (generics, modules, JSON v2 taking years).
- Some appreciate the stability and deliberate design; others see Go as “incomplete” or too slow to adopt known ideas.
- Several comments frame Go as well-suited for LLM-generated code: fast compile times, simple syntax, easy review.
- Counterpoints: other languages (Rust, Kotlin, C#, OCaml, etc.) may be preferable depending on desired balance of simplicity vs performance.