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