This repository was archived by the owner on Dec 15, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -371,6 +371,29 @@ func Test_omitempty_nil_interface(t *testing.T) {
371371 should .Equal (string (js ), str )
372372}
373373
374+ func Test_omitempty_nil_nonempty_interface (t * testing.T ) {
375+ type TestData struct {
376+ Field MyInterface `json:"field,omitempty"`
377+ }
378+ should := require .New (t )
379+
380+ obj := TestData {
381+ Field : nil ,
382+ }
383+
384+ js , err := json .Marshal (obj )
385+ should .Equal (nil , err )
386+ should .Equal ("{}" , string (js ))
387+
388+ str , err := MarshalToString (obj )
389+ should .Equal (nil , err )
390+ should .Equal (string (js ), str )
391+
392+ err = Unmarshal (js , & obj )
393+ should .Equal (nil , err )
394+ should .Equal (nil , obj .Field )
395+ }
396+
374397func Test_marshal_nil_marshaler_interface (t * testing.T ) {
375398 type TestData struct {
376399 Field json.Marshaler `json:"field"`
@@ -407,4 +430,9 @@ func Test_marshal_nil_nonempty_interface(t *testing.T) {
407430 str , err := MarshalToString (obj )
408431 should .Equal (nil , err )
409432 should .Equal (string (js ), str )
433+
434+ obj .Field = MyString ("hello" )
435+ err = Unmarshal (js , & obj )
436+ should .Equal (nil , err )
437+ should .Equal (nil , obj .Field )
410438}
You can’t perform that action at this time.
0 commit comments