A new experimental Go API for JSON
a day ago
- #API development
- #JSON handling
- #Go programming
- Introduction of a new experimental Go API for JSON in Go 1.25, addressing long-standing issues with the current encoding/json package.
- Problems with encoding/json include imprecise JSON syntax handling, invalid UTF-8 acceptance, duplicate member names, leaking nilness of slices and maps, case-insensitive unmarshaling, and inconsistent method calling.
- API deficiencies in encoding/json include difficulty in unmarshaling from io.Reader, restrictive options setting, and performance limitations.
- Performance limitations in encoding/json involve MarshalJSON and UnmarshalJSON methods leading to unnecessary allocations and lack of streaming capabilities.
- Introduction of encoding/json/v2 and encoding/json/jsontext packages to address these issues, with a focus on syntactic and semantic separation for better JSON handling.
- Behavior differences in v2 include stricter UTF-8 validation, rejection of duplicate names, case-sensitive unmarshaling, and optimized nil slice/map handling.
- Performance optimizations in v2 show significant improvements, especially in unmarshaling, with benchmarks indicating up to 10x speed improvements.
- The experimental packages are not visible by default and require the GOEXPERIMENT=jsonv2 flag or build tag for use, encouraging community feedback for future development.