J
.github
Update to fdfc67f44913e48d2e3458a3b79edf0a919442de (#213)
internal
Update to fdfc67f44913e48d2e3458a3b79edf0a919442de (#213)
jsontext
Update to Go 1.27.0 (#218)
v1
Update to Go 1.27.0 (#218)
alias_gen.go
Update to 464dc3f344dde60a58ada8b5c75b73a124548b43 (#206)
alias.go
Update to Go 1.27.0 (#218)
api.png
Refactor API to use variadic options (#298)
arshal_any.go
Update to 464dc3f344dde60a58ada8b5c75b73a124548b43 (#206)
arshal_default.go
Update to 40f0f8125549b511e41607712acb0435ce07d060 (#216)
arshal_embedded.go
Update to 6a1dd03423311e92d0ec4f1b4eedae2c9d2b6e96 (#215)
arshal_funcs.go
Update to Go 1.27.0 (#218)
arshal_methods.go
Update to Go 1.27.0 (#218)
arshal_test.go
Update to 40f0f8125549b511e41607712acb0435ce07d060 (#216)
arshal_time_test.go
Use types alias under goexperiment.jsonv2 and Go 1.25 (#186)
arshal_time.go
Update to fdfc67f44913e48d2e3458a3b79edf0a919442de (#213)
arshal.go
Update to Go 1.27.0 (#218)
AUTHORS
Initial commit of base files
bench_test.go
Update to 464dc3f344dde60a58ada8b5c75b73a124548b43 (#206)
CONTRIBUTORS
Initial commit of base files
doc.go
Update to Go 1.27.0 (#218)
errors_test.go
Use types alias under goexperiment.jsonv2 and Go 1.25 (#186)
errors.go
Update to Go 1.27.0 (#218)
example_marshaljson_test.go
Update to Go 1.27.0 (#220)
example_orderedobject_test.go
Update to Go 1.27.0 (#218)
example_test.go
Update to Go 1.27.0 (#218)
fields_test.go
Update to 6a1dd03423311e92d0ec4f1b4eedae2c9d2b6e96 (#215)
fields.go
Update to 6a1dd03423311e92d0ec4f1b4eedae2c9d2b6e96 (#215)
fold_test.go
Use types alias under goexperiment.jsonv2 and Go 1.25 (#186)
fold.go
Use types alias under goexperiment.jsonv2 and Go 1.25 (#186)
fuzz_test.go
Use types alias under goexperiment.jsonv2 and Go 1.25 (#186)
go.mod
Update to 35abaf75c35adc9b22038885781b8be70a8476e0 (#200)
go.sum
Use new slices package available in Go 1.21 (#289)
inline_test.go
Use types alias under goexperiment.jsonv2 and Go 1.25 (#186)
intern_test.go
Use types alias under goexperiment.jsonv2 and Go 1.25 (#186)
intern.go
Update to 1cae25d6a63fd0458f6cab6c509c16a95dea7499 (#212)
LICENSE
Initial commit of base files
migrate.sh
Update to fdfc67f44913e48d2e3458a3b79edf0a919442de (#213)
options_format_global.go
Update to fdfc67f44913e48d2e3458a3b79edf0a919442de (#213)
options_format_test.go
Update to fdfc67f44913e48d2e3458a3b79edf0a919442de (#213)
options_format.go
Update to fdfc67f44913e48d2e3458a3b79edf0a919442de (#213)
options.go
Update to Go 1.27.0 (#218)
README.md
Update README.md (#219)

JSON Serialization (v2)

This module currently hosts a mirror of encoding/json/v2. Importing this package with the goexperiment.jsonv2 build tag (which is enabled by default on Go 1.27) will alias the implementation in the Go standard library.

This module contained the historical prototype of encoding/json/v2 before it was formally adopted into the Go standard library as an experiment in Go 1.25 and then later as a stable API in Go 1.27.

Development

This module was primarily developed by @dsnet, @mvdan, and @johanbrandhorst with feedback provided by @rogpeppe, @ChrisHines, and @rsc. During the process to adopt the project into the Go standard library, API review was provided by @aclements, @neild, and @prattmic.

Discussion about semantics occur semi-regularly, where a record of past meetings can be found here.

Design overview

This package aims to provide a clean separation between syntax and semantics. Syntax deals with the structural representation of JSON (as specified in RFC 4627, RFC 7159, RFC 7493, RFC 8259, and RFC 8785). Semantics deals with the meaning of syntactic data as usable application data.

The Encoder and Decoder types are streaming tokenizers concerned with the packing or parsing of JSON data. They operate on Token and Value types which represent the common data structures that are representable in JSON. Encoder and Decoder do not aim to provide any interpretation of the data.

Functions like Marshal, MarshalWrite, MarshalEncode, Unmarshal, UnmarshalRead, and UnmarshalDecode provide semantic meaning by correlating any arbitrary Go type with some JSON representation of that type (as stored in data types like []byte, io.Writer, io.Reader, Encoder, or Decoder).

This diagram provides a high-level overview of the v2 json and jsontext packages. Purple blocks represent types, while blue blocks represent functions or methods. The arrows and their direction represent the approximate flow of data. The bottom half of the diagram contains functionality that is only concerned with syntax (implemented by the jsontext package), while the upper half contains functionality that assigns semantic meaning to syntactic data handled by the bottom half (as implemented by the v2 json package).

In contrast to v1 encoding/json, options are represented as separate types rather than being setter methods on the Encoder or Decoder types. Some options affects JSON serialization at the syntactic layer, while others affect it at the semantic layer. Some options only affect JSON when decoding, while others affect JSON while encoding.

Performance

One of the goals of the v2 module is to be more performant than v1, but not at the expense of correctness. In general, v2 is at performance parity with v1 for marshaling, but dramatically faster for unmarshaling.

See https://github.com/go-json-experiment/jsonbench for benchmarks comparing v2 with v1 and a number of other popular JSON implementations.