diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2026-02-02 17:52:37 -0800 |
|---|---|---|
| committer | Joseph Tsai <joetsai@digital-static.net> | 2026-02-12 16:09:06 -0800 |
| commit | c9cbeb0a1b08a9830a3d2d4abe0c2108e52f7647 (patch) | |
| tree | f52aba650a85b15a0a009df469e37c4c4ad76b13 /src/encoding/json/v2/example_test.go | |
| parent | 92c7fcf137848ad74f88f75fc21bcb159eb08104 (diff) | |
| download | go-c9cbeb0a1b08a9830a3d2d4abe0c2108e52f7647.tar.xz | |
encoding/json/v2: remove `unknown` tag option and DiscardUnknownMembers
WARNING: This commit contains breaking changes
for those already using GOEXPERIMENT=jsonv2.
This removes support for the `unknown` tag option and
the DiscardUnknownMembers marshal option.
The `unknown` tag option semantics are a bit too subtle
even for experienced Go programmers to understand.
Remove support for it. The exact same feature (or something similar)
can be added back into a future release of json/v2.
We already support the `inline` tag option,
which can handle most cases of what someone might want to do
with unknown fields (such as preserve them).
Fixes #77271
Updates #76444
Change-Id: I875952f0755e58aac4c571869b2cdb56e75cfda9
Reviewed-on: https://go-review.googlesource.com/c/go/+/741320
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/encoding/json/v2/example_test.go')
| -rw-r--r-- | src/encoding/json/v2/example_test.go | 62 |
1 files changed, 0 insertions, 62 deletions
diff --git a/src/encoding/json/v2/example_test.go b/src/encoding/json/v2/example_test.go index dc1f06674c..684ca9c6a2 100644 --- a/src/encoding/json/v2/example_test.go +++ b/src/encoding/json/v2/example_test.go @@ -8,7 +8,6 @@ package json_test import ( "bytes" - "errors" "fmt" "log" "math" @@ -338,67 +337,6 @@ func Example_inlinedFields() { // } } -// Due to version skew, the set of JSON object members known at compile-time -// may differ from the set of members encountered at execution-time. -// As such, it may be useful to have finer grain handling of unknown members. -// This package supports preserving, rejecting, or discarding such members. -func Example_unknownMembers() { - const input = `{ - "Name": "Teal", - "Value": "#008080", - "WebSafe": false - }` - type Color struct { - Name string - Value string - - // Unknown is a Go struct field that holds unknown JSON object members. - // It is marked as having this behavior with the "unknown" tag option. - // - // The type may be a jsontext.Value or map[string]T. - Unknown jsontext.Value `json:",unknown"` - } - - // By default, unknown members are stored in a Go field marked as "unknown" - // or ignored if no such field exists. - var color Color - err := json.Unmarshal([]byte(input), &color) - if err != nil { - log.Fatal(err) - } - fmt.Println("Unknown members:", string(color.Unknown)) - - // Specifying RejectUnknownMembers causes Unmarshal - // to reject the presence of any unknown members. - err = json.Unmarshal([]byte(input), new(Color), json.RejectUnknownMembers(true)) - serr, ok := errors.AsType[*json.SemanticError](err) - if ok && serr.Err == json.ErrUnknownName { - fmt.Println("Unmarshal error:", serr.Err, strconv.Quote(serr.JSONPointer.LastToken())) - } - - // By default, Marshal preserves unknown members stored in - // a Go struct field marked as "unknown". - b, err := json.Marshal(color) - if err != nil { - log.Fatal(err) - } - fmt.Println("Output with unknown members: ", string(b)) - - // Specifying DiscardUnknownMembers causes Marshal - // to discard any unknown members. - b, err = json.Marshal(color, json.DiscardUnknownMembers(true)) - if err != nil { - log.Fatal(err) - } - fmt.Println("Output without unknown members:", string(b)) - - // Output: - // Unknown members: {"WebSafe":false} - // Unmarshal error: unknown object member name "WebSafe" - // Output with unknown members: {"Name":"Teal","Value":"#008080","WebSafe":false} - // Output without unknown members: {"Name":"Teal","Value":"#008080"} -} - // The "format" tag option can be used to alter the formatting of certain types. func Example_formatFlags() { value := struct { |
