]> Cypherpunks repositories - gostls13.git/commit
encoding/json/v2: restrict presence of default options
authorJoe Tsai <joetsai@digital-static.net>
Sat, 11 Oct 2025 18:37:58 +0000 (11:37 -0700)
committerJoseph Tsai <joetsai@digital-static.net>
Tue, 14 Oct 2025 00:01:45 +0000 (17:01 -0700)
commit6fd8e88d07b08531a5585aa4fbcc6043d556742f
tree228722ad58335f82fc1ae1fb548ceba7fea3d3d5
parent1abc6b0204ed231311c9bbc53cfab36dc546aa8e
encoding/json/v2: restrict presence of default options

Originally, DefaultOptionsV1 and DefaultOptionsV2 represented
the full set of all options with specific ones set to true or false.

However, there are certain options such as WithIndent or WithMarshalers
that are neither v1 or v2 specific.
At some point we removed whitespace related options from the set:
https://github.com/go-json-experiment/json/pull/26

This avoids DefaultOptionsV1 or DefaultOptionsV2 from affecting
any previously set whitespace. However, why are whitespace options
special and thus excluded from the set? What about Marshalers?

As a more principaled way to address this, we restrict
DefaultOptionsV1 and DefaultOptionsV2 to only be the options
where the default setting changes between v1 and v2.
All other options are unpopulated.

This avoids a panic with GetOption(DefaultOptionsV2, WithMarshalers)
since DefaultOptionsV2 previously had the presence bit for
Marshalers set to true, but had no actual value.
Now, the presence bit is set to false, so the value is not consulted.

Fixes #75149

Change-Id: I30b45abd35404578b4135cc3bad1a1a2993cb0cf
Reviewed-on: https://go-review.googlesource.com/c/go/+/710878
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
src/encoding/json/internal/jsonopts/options.go
src/encoding/json/internal/jsonopts/options_test.go
src/encoding/json/v2/options.go
src/encoding/json/v2_options.go