@@ -15,9 +15,10 @@ import (
15
15
"runtime/debug"
16
16
"strconv"
17
17
"testing"
18
+ "time"
18
19
)
19
20
20
- type Optionals struct {
21
+ type OptionalsEmpty struct {
21
22
Sr string `json:"sr"`
22
23
So string `json:"so,omitempty"`
23
24
Sw string `json:"-"`
@@ -56,7 +57,7 @@ func TestOmitEmpty(t *testing.T) {
56
57
"str": {},
57
58
"sto": {}
58
59
}`
59
- var o Optionals
60
+ var o OptionalsEmpty
60
61
o .Sw = "something"
61
62
o .Mr = map [string ]any {}
62
63
o .Mo = map [string ]any {}
@@ -70,6 +71,120 @@ func TestOmitEmpty(t *testing.T) {
70
71
}
71
72
}
72
73
74
+ type OptionalsZero struct {
75
+ Sr string `json:"sr"`
76
+ So string `json:"so,omitzero"`
77
+ Sw string `json:"-"`
78
+
79
+ Ir int `json:"omitempty"` // actually named omitempty, not an option
80
+ Io int `json:"io,omitzero"`
81
+
82
+ Slr []string `json:"slr,random"`
83
+ Slo []string `json:"slo,omitzero"`
84
+ SloNonNil []string `json:"slononnil,omitzero"`
85
+
86
+ Mr map [string ]any `json:"mr"`
87
+ Mo map [string ]any `json:",omitzero"`
88
+
89
+ Fr float64 `json:"fr"`
90
+ Fo float64 `json:"fo,omitzero"`
91
+
92
+ Br bool `json:"br"`
93
+ Bo bool `json:"bo,omitzero"`
94
+
95
+ Ur uint `json:"ur"`
96
+ Uo uint `json:"uo,omitzero"`
97
+
98
+ Str struct {} `json:"str"`
99
+ Sto struct {} `json:"sto,omitzero"`
100
+
101
+ MyTime time.Time `json:"mytime,omitzero"`
102
+ }
103
+
104
+ func TestOmitZero (t * testing.T ) {
105
+ var want = `{
106
+ "sr": "",
107
+ "omitempty": 0,
108
+ "slr": null,
109
+ "slononnil": [],
110
+ "mr": {},
111
+ "Mo": {},
112
+ "fr": 0,
113
+ "br": false,
114
+ "ur": 0,
115
+ "str": {}
116
+ }`
117
+ var o OptionalsZero
118
+ o .Sw = "something"
119
+ o .SloNonNil = make ([]string , 0 )
120
+ o .Mr = map [string ]any {}
121
+ o .Mo = map [string ]any {}
122
+
123
+ got , err := MarshalIndent (& o , "" , " " )
124
+ if err != nil {
125
+ t .Fatalf ("MarshalIndent error: %v" , err )
126
+ }
127
+ if got := string (got ); got != want {
128
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
129
+ }
130
+ }
131
+
132
+ type OptionalsEmptyZero struct {
133
+ Sr string `json:"sr"`
134
+ So string `json:"so,omitempty,omitzero"`
135
+ Sw string `json:"-"`
136
+
137
+ Ir int `json:"omitempty"` // actually named omitempty, not an option
138
+ Io int `json:"io,omitempty,omitzero"`
139
+
140
+ Slr []string `json:"slr,random"`
141
+ Slo []string `json:"slo,omitempty,omitzero"`
142
+ SloNonNil []string `json:"slononnil,omitempty,omitzero"`
143
+
144
+ Mr map [string ]any `json:"mr"`
145
+ Mo map [string ]any `json:",omitempty,omitzero"`
146
+
147
+ Fr float64 `json:"fr"`
148
+ Fo float64 `json:"fo,omitempty,omitzero"`
149
+
150
+ Br bool `json:"br"`
151
+ Bo bool `json:"bo,omitempty,omitzero"`
152
+
153
+ Ur uint `json:"ur"`
154
+ Uo uint `json:"uo,omitempty,omitzero"`
155
+
156
+ Str struct {} `json:"str"`
157
+ Sto struct {} `json:"sto,omitempty,omitzero"`
158
+
159
+ MyTime time.Time `json:"mytime,omitempty,omitzero"`
160
+ }
161
+
162
+ func TestOmitEmptyZero (t * testing.T ) {
163
+ var want = `{
164
+ "sr": "",
165
+ "omitempty": 0,
166
+ "slr": null,
167
+ "mr": {},
168
+ "fr": 0,
169
+ "br": false,
170
+ "ur": 0,
171
+ "str": {}
172
+ }`
173
+ var o OptionalsEmptyZero
174
+ o .Sw = "something"
175
+ o .SloNonNil = make ([]string , 0 )
176
+ o .Mr = map [string ]any {}
177
+ o .Mo = map [string ]any {}
178
+
179
+ got , err := MarshalIndent (& o , "" , " " )
180
+ if err != nil {
181
+ t .Fatalf ("MarshalIndent error: %v" , err )
182
+ }
183
+ if got := string (got ); got != want {
184
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
185
+ }
186
+ }
187
+
73
188
type StringTag struct {
74
189
BoolStr bool `json:",string"`
75
190
IntStr int64 `json:",string"`
0 commit comments