Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/example_orderedobject_test.go

raw
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package json_test
6
7 import (
8 "fmt"
9 "log"
10 "reflect"
11
12 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json"
13 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext"
14 )
15
16 // OrderedObject is an ordered sequence of name/value members in a JSON object.
17 //
18 // RFC 8259 defines an object as an "unordered collection".
19 // JSON implementations need not make "ordering of object members visible"
20 // to applications nor will they agree on the semantic meaning of an object if
21 // "the names within an object are not unique". For maximum compatibility,
22 // applications should avoid relying on ordering or duplicity of object names.
23 type OrderedObject[V any] []ObjectMember[V]
24
25 // ObjectMember is a JSON object member.
26 type ObjectMember[V any] struct {
27 Name string
28 Value V
29 }
30
31 // MarshalJSONTo encodes obj as a JSON object into enc.
32 func (obj *OrderedObject[V]) MarshalJSONTo(enc *jsontext.Encoder) error {
33 if err := enc.WriteToken(jsontext.BeginObject); err != nil {
34 return err
35 }
36 for i := range *obj {
37 member := &(*obj)[i]
38 if err := json.MarshalEncode(enc, &member.Name); err != nil {
39 return err
40 }
41 if err := json.MarshalEncode(enc, &member.Value); err != nil {
42 return err
43 }
44 }
45 if err := enc.WriteToken(jsontext.EndObject); err != nil {
46 return err
47 }
48 return nil
49 }
50
51 // UnmarshalJSONFrom decodes a JSON object from dec into obj.
52 func (obj *OrderedObject[V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
53 if k := dec.PeekKind(); k != '{' {
54 return fmt.Errorf("expected object start, but encountered %v", k)
55 }
56 if _, err := dec.ReadToken(); err != nil {
57 return err
58 }
59 for dec.PeekKind() != '}' {
60 *obj = append(*obj, ObjectMember[V]{})
61 member := &(*obj)[len(*obj)-1]
62 if err := json.UnmarshalDecode(dec, &member.Name); err != nil {
63 return err
64 }
65 if err := json.UnmarshalDecode(dec, &member.Value); err != nil {
66 return err
67 }
68 }
69 if _, err := dec.ReadToken(); err != nil {
70 return err
71 }
72 return nil
73 }
74
75 // The exact order of JSON object can be preserved through the use of a
76 // specialized type that implements [MarshalerTo] and [UnmarshalerFrom].
77 func Example_orderedObject() {
78 // Round-trip marshal and unmarshal an ordered object.
79 // We expect the order and duplicity of JSON object members to be preserved.
80 // Specify jsontext.AllowDuplicateNames since this object contains "fizz" twice.
81 want := OrderedObject[string]{
82 {"fizz", "buzz"},
83 {"hello", "world"},
84 {"fizz", "wuzz"},
85 }
86 b, err := json.Marshal(&want, jsontext.AllowDuplicateNames(true))
87 if err != nil {
88 log.Fatal(err)
89 }
90 var got OrderedObject[string]
91 err = json.Unmarshal(b, &got, jsontext.AllowDuplicateNames(true))
92 if err != nil {
93 log.Fatal(err)
94 }
95
96 // Sanity check.
97 if !reflect.DeepEqual(got, want) {
98 log.Fatalf("roundtrip mismatch: got %v, want %v", got, want)
99 }
100
101 // Print the serialized JSON object.
102 (*jsontext.Value)(&b).Indent() // indent for readability
103 fmt.Println(string(b))
104
105 // Output:
106 // {
107 // "fizz": "buzz",
108 // "hello": "world",
109 // "fizz": "wuzz"
110 // }
111 }