Background
JSON is a lightweight data interchange format that has been favored by developers for its flexible features since the inception of Go’s encoding/json
package ten years ago. Developers can customize the JSON representation of struct fields through struct tags, and also allow Go types to customize their own JSON representation. However, with the development of Go language and JSON standards, some functional deficiencies and performance limitations have gradually been exposed.
- Missing functionality: For example, it is not possible to specify custom formatting for the
time.Time
type, and it is not possible to omit specific Go values during serialization, etc. - API deficiencies: For example, there is no simple way to correctly deserialize JSON from an
io.Reader
. - Performance limitations: The performance of the standard
json
package is not satisfactory, especially when dealing with large amounts of data. - Behavioral flaws: For example, the error handling for JSON syntax is not strict enough, and case-insensitive deserialization, etc.
Just like math/v2
, Go official proposes encoding/json/v2
to solve the above problems. The main goal of this article is to analyze some issues about null values in encoding/json
and how they are resolved in encoding/json/v2
. This article does not involve other modifications in encoding/json/v2
.
This article was first published in the Medium MPP plan. If you are a Medium user, please follow me on Medium. Thank you very much.
omitempty
In the encoding/json
package, there is a description of omitempty
as follows:
The “omitempty” option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.
However, this predefined “empty” value judgment logic does not meet the needs of all actual scenarios. Let’s look at an example:
|
|
The output result is:
|
|
Although omitempty
is added to each field of Post
, the result is not as expected.
omitempty
cannot handle emptystruct
, such asPost.Category
- The way
omitempty
handlestime.Time
is not what we understand asUTC =0
, that is,1970-01-01 00:00:00
, but0001-01-01T00:00:00Z
.
omitzero Tag
In encoding/json/v2
, a new tag omitzero
will be added, which adds two functions to solve the above two problems. (This feature is still under development, but you can experience the new features in advance through go-json-experiment/json)
- Better handling of
time.Time
. - Support for custom
IsZero
function.
For example, the following code:
|
|
Compared with encoding/json
, encoding/json/v2
solves the above problems.
|
|
Conclusion
By introducing the omitzero
tag, Go has taken an essential step in solving the pain points of “empty” value processing in JSON encoding. This solution not only meets developers’ needs for a more flexible definition of “empty” values but also maintains compatibility with existing systems. The landing time of omitzero
is not determined yet, and it will not be available until the Go 1.24
version at the earliest. In addition, encoding/xml
and other packages will also follow the json package and add the omitzero
tag.
encoding/json/v2
also includes updates in other aspects, such as performance. Interested Gophers can learn about these changes in advance, and this blog will continue to pay attention to this proposal.
Go language is beginning to mature.