Menu

Go JSON v2: when an empty patch stops being empty
📰

Go JSON v2: when an empty patch stops being empty

DEV Community: go·Shivam Dwivedi·10 days ago
#WR6B5LNQ
#devtogo#devto#dev#json#patch
Reading 0:00
15s threshold

An empty update should be a fairly quiet object. Mine grew two fields after I switched JSON packages.

With the same Go struct and the same zero value, encoding/json produced {} . encoding/json/v2 produced {"count":0,"enabled":false} . Both calls succeeded.

For a PATCH-style endpoint where an absent member means “leave this alone,” that is a change in the request's meaning. The serializer hasn't become confused. The old tags are asking a different question under v2.

Outputs from the executed Go example. The PATCH interpretation is the explicitly defined example contract, not a rule imposed by JSON itself. Image by the author.

Decide what an absent field means

Go's new announcement makes encoding/json/v2 generally available in Go 1.27. [1] I used Go 1.27.3 for this example and ran both packages in the same program.

Here is my small update contract: omitted fields leave the stored value unchanged; present fields replace it.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.