Debian Codegraph @

encode_omit_zero_test.go

raw
1 package json_test
2
3 import (
4 stdjson "encoding/json"
5 "math"
6 "reflect"
7 "testing"
8 "time"
9
10 "github.com/goccy/go-json"
11 )
12
13 // omitzero omits a field whose value is zero, as encoding/json decides it from Go 1.24: by the IsZero method
14 // of the type if it has one, and else by reflect.Value.IsZero. With an older Go, encoding/json ignores the
15 // option, and so does go-json: the cases compare with encoding/json of the Go the test runs with.
16
17 type omitZeroValueMethod struct{ N int }
18
19 func (v omitZeroValueMethod) IsZero() bool { return v.N < 0 }
20
21 type omitZeroPointerMethod struct{ N int }
22
23 func (v *omitZeroPointerMethod) IsZero() bool { return v.N < 0 }
24
25 type omitZeroer interface{ IsZero() bool }
26
27 type omitZeroMarshaler struct{ N int }
28
29 func (v *omitZeroMarshaler) MarshalJSON() ([]byte, error) { return []byte(`"m"`), nil }
30
31 func (v *omitZeroMarshaler) IsZero() bool { return v.N < 0 }
32
33 type omitZeroTextMarshaler struct{ N int }
34
35 func (v *omitZeroTextMarshaler) MarshalText() ([]byte, error) { return []byte("t"), nil }
36
37 type omitZeroMarshalers struct {
38 JSON omitZeroMarshaler `json:"json,omitzero"`
39 JSONPtr *omitZeroMarshaler `json:"json_ptr,omitzero"`
40 Text omitZeroTextMarshaler `json:"text,omitzero"`
41 TextPtr *omitZeroTextMarshaler `json:"text_ptr,omitzero"`
42 }
43
44 type omitZeroInner struct {
45 A int
46 B string
47 }
48
49 type omitZeroEmbedded struct {
50 E int `json:"e,omitzero"`
51 }
52
53 type omitZeroScalars struct {
54 Bool bool `json:"bool,omitzero"`
55 Int int `json:"int,omitzero"`
56 Int8 int8 `json:"int8,omitzero"`
57 Int16 int16 `json:"int16,omitzero"`
58 Int32 int32 `json:"int32,omitzero"`
59 Uint64 uint64 `json:"uint64,omitzero"`
60 Float32 float32 `json:"float32,omitzero"`
61 Float64 float64 `json:"float64,omitzero"`
62 String string `json:"string,omitzero"`
63 Number json.Number `json:"number,omitzero"`
64 Ptr *int `json:"ptr,omitzero"`
65 Slice []int `json:"slice,omitzero"`
66 Map map[string]int `json:"map,omitzero"`
67 Any any `json:"any,omitzero"`
68 Array [2]int `json:"array,omitzero"`
69 Empty [0]int `json:"empty,omitzero"`
70 Struct omitZeroInner `json:"struct,omitzero"`
71 Bytes []byte `json:"bytes,omitzero"`
72 Quoted int `json:"quoted,omitzero,string"`
73 }
74
75 type omitZeroMethods struct {
76 Time time.Time `json:"time,omitzero"`
77 TimePtr *time.Time `json:"time_ptr,omitzero"`
78 Value omitZeroValueMethod `json:"value,omitzero"`
79 ValuePtr *omitZeroValueMethod `json:"value_ptr,omitzero"`
80 Pointer omitZeroPointerMethod `json:"pointer,omitzero"`
81 PointerP *omitZeroPointerMethod `json:"pointer_ptr,omitzero"`
82 Interface omitZeroer `json:"interface,omitzero"`
83 }
84
85 type omitZeroBoth struct {
86 Slice []int `json:"slice,omitempty,omitzero"`
87 Map map[string]int `json:"map,omitzero,omitempty"`
88 Struct omitZeroInner `json:"struct,omitempty,omitzero"`
89 Int int `json:"int,omitempty,omitzero"`
90 }
91
92 type omitZeroLayout struct {
93 First int `json:"first,omitzero"`
94 omitZeroEmbedded `json:",omitzero"`
95 AKeyWhichIsLongerThanAChunkOfTheEncoder string `json:"a_key_which_is_longer_than_a_chunk_of_the_encoder,omitzero"`
96 Named omitZeroEmbedded `json:"named,omitzero"`
97 Last string `json:"last,omitzero"`
98 }
99
100 type omitZeroOnly struct {
101 A int `json:"a,omitzero"`
102 }
103
104 func TestEncodeOmitZero(t *testing.T) {
105 one := 1
106 zero := 0
107 now := time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC)
108 var zeroTime time.Time
109 values := []any{
110 omitZeroScalars{},
111 &omitZeroScalars{},
112 omitZeroScalars{
113 Bool: true, Int: 1, Int8: -1, Int16: 2, Int32: 3, Uint64: 4, Float32: 1.5, Float64: 2.5, String: "s",
114 Number: "1", Ptr: &one, Slice: []int{1}, Map: map[string]int{"a": 1}, Any: 1, Array: [2]int{0, 1},
115 Struct: omitZeroInner{B: "b"}, Bytes: []byte("b"), Quoted: 7,
116 },
117 omitZeroScalars{
118 Float32: float32(math.Copysign(0, -1)), Float64: math.Copysign(0, -1), Ptr: &zero,
119 Slice: []int{}, Map: map[string]int{}, Any: (*int)(nil), Bytes: []byte{},
120 },
121 omitZeroScalars{Any: 0, Array: [2]int{}, Struct: omitZeroInner{}},
122 omitZeroScalars{Array: [2]int{1}},
123 omitZeroMethods{},
124 omitZeroMethods{
125 Time: now, TimePtr: &zeroTime, Value: omitZeroValueMethod{-1}, ValuePtr: &omitZeroValueMethod{-1},
126 Pointer: omitZeroPointerMethod{-1}, PointerP: &omitZeroPointerMethod{-1},
127 Interface: omitZeroValueMethod{-1},
128 },
129 omitZeroMethods{
130 TimePtr: &now, Value: omitZeroValueMethod{0}, ValuePtr: &omitZeroValueMethod{1},
131 Pointer: omitZeroPointerMethod{0}, PointerP: &omitZeroPointerMethod{1},
132 Interface: &omitZeroPointerMethod{1},
133 },
134 omitZeroMethods{Interface: (*omitZeroPointerMethod)(nil)},
135 omitZeroMethods{Interface: (*omitZeroValueMethod)(nil)},
136 omitZeroBoth{},
137 omitZeroBoth{Slice: []int{}, Map: map[string]int{}},
138 omitZeroBoth{Slice: []int{1}, Map: map[string]int{"a": 1}, Struct: omitZeroInner{A: 1}, Int: 1},
139 omitZeroLayout{},
140 omitZeroLayout{
141 First: 1, omitZeroEmbedded: omitZeroEmbedded{E: 2}, AKeyWhichIsLongerThanAChunkOfTheEncoder: "long",
142 Named: omitZeroEmbedded{E: 3}, Last: "last",
143 },
144 omitZeroMarshalers{},
145 &omitZeroMarshalers{},
146 &omitZeroMarshalers{JSON: omitZeroMarshaler{-1}, JSONPtr: &omitZeroMarshaler{-1}, TextPtr: &omitZeroTextMarshaler{}},
147 &omitZeroMarshalers{JSON: omitZeroMarshaler{1}, JSONPtr: &omitZeroMarshaler{1}, Text: omitZeroTextMarshaler{1}},
148 omitZeroOnly{},
149 omitZeroOnly{A: 1},
150 &omitZeroOnly{},
151 []omitZeroOnly{{}, {A: 1}},
152 map[string]omitZeroOnly{"z": {}, "o": {A: 1}},
153 struct {
154 Inner *omitZeroOnly `json:"inner,omitzero"`
155 }{Inner: &omitZeroOnly{}},
156 struct {
157 Inner omitZeroOnly `json:"inner,omitzero"`
158 Next int `json:"next"`
159 }{},
160 }
161 for _, v := range values {
162 want, err := stdjson.Marshal(v)
163 if err != nil {
164 t.Fatal(err)
165 }
166 got, err := json.Marshal(v)
167 if err != nil {
168 t.Fatalf("%T: %v", v, err)
169 }
170 if string(got) != string(want) {
171 t.Errorf("%T: got %s, want %s", v, got, want)
172 }
173 wantIndent, err := stdjson.MarshalIndent(v, "", " ")
174 if err != nil {
175 t.Fatal(err)
176 }
177 gotIndent, err := json.MarshalIndent(v, "", " ")
178 if err != nil {
179 t.Fatalf("%T: %v", v, err)
180 }
181 if string(gotIndent) != string(wantIndent) {
182 t.Errorf("%T: indent: got %s, want %s", v, gotIndent, wantIndent)
183 }
184 // the order of the fields may change, but not which of them are written.
185 ordered, err := json.MarshalWithOption(v, json.OptimizeFieldOrder())
186 if err != nil {
187 t.Fatalf("%T: %v", v, err)
188 }
189 var gotFields, wantFields any
190 if err := stdjson.Unmarshal(ordered, &gotFields); err != nil {
191 t.Fatalf("%T: %v: %s", v, err, ordered)
192 }
193 if err := stdjson.Unmarshal(want, &wantFields); err != nil {
194 t.Fatal(err)
195 }
196 if !reflect.DeepEqual(gotFields, wantFields) {
197 t.Errorf("%T: OptimizeFieldOrder: got %s, want %s", v, ordered, want)
198 }
199 }
200 }