A new experimental Go API for JSON

Link to blog post from today:

As somebody who lives and breathes JSON every day all day, this is really interesting to me. I don’t personally care about things like rejecting invalid UTF-8 characters, but I can see a use case for the new jsontext package:

The jsontext package provides functionality for interacting with JSON at a syntactic level and derives its name from RFC 8259, section 2 where the grammar for JSON data is literally called JSON-text. Since it only interacts with JSON at a syntactic level, it does not depend on Go reflection.

Out of the box, unmarshal performance is greatly improved:

The Marshal performance of v2 is roughly at parity with v1. Sometimes it is slightly faster, but other times it is slightly slower. The Unmarshal performance of v2 is significantly faster than v1, with benchmarks demonstrating improvements of up to 10x.

And if you implement a new interface you can enable streaming:

In order to obtain greater performance gains, existing implementations of Marshaler and Unmarshaler should migrate to also implement MarshalerTo and UnmarshalerFrom, so that they can benefit from processing JSON in a purely streaming manner. For example, recursive parsing of OpenAPI specifications in UnmarshalJSON methods significantly hurt performance in a particular service of Kubernetes (see kubernetes/kube-openapi#315), while switching to UnmarshalJSONFrom improved performance by orders of magnitude.

I’m interested in checking this out and wish it came a bit sooner. I’m about to launch into a project where I’m building the go library for an existing mongo-like DB API and this sounds like it would be a good fit for my project. What do you all think?

2 Likes