diff options
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 { |
