Technology1 min read

A New Go API for JSON

By · Published by Everything Blog

In short

Go 1.27 has made the encoding/json/v2 and encoding/json/jsontext packages generally available following an experimental phase launched last year. These new components address legacy behavioral shortcomings, including case-insensitive unmarshaling, trailing data errors with io.Reader, and the practice of serializing nil slices or maps as null. Additionally, the new implementation removes performance bottlenecks caused by MarshalJSON requiring allocated byte slices and utility functions writing solely to bytes.Buffer.

Key points

  • The updated JSON architecture resolves critical security and compliance issues that exist…: The updated JSON architecture resolves critical security and compliance issues that existed in Go for over 15 years.
  • Usability is improved by addressing io.Reader trailing data errors and expanding configur…: Usability is improved by addressing io.Reader trailing data errors and expanding configuration passing across the call stack.
  • The new packages eliminate unnecessary memory allocations to establish significantly bett…: The new packages eliminate unnecessary memory allocations to establish significantly better performance defaults for serialization workflows.

Sources checked by our analysis pipeline: 1

The Go Blog

A new Go API for JSON

Introduction

JavaScript Object Notation (JSON) is a simple data interchange format, the most popular data format used on the internet, and widely read and written by Go programs. Go has included support for JSON in the standard library since well before Go 1.0. We first we wrote about support for JSON in Go more than 15 years ago!

Over time, packages evolve with the needs of their users, and encoding/json

is no exception.

Last year, we wrote about a new experimental API for JSON designed to bring long-awaited improvements and fixes.

After additional feedback and refinement, the new encoding/json/v2

and encoding/json/jsontext

packages are stable and generally available in Go 1.27!

This post is an updated version of the article introducing the experiment, describing the problems with encoding/json

, and an introduction to the new packages.

Problems with encoding/json

Overall, encoding/json

has held up well.

The idea of marshaling and unmarshaling arbitrary Go types

with some default representation in JSON, combined with the ability to

customize the representation, has proven to be…

Technology