mirror of https://github.com/goccy/go-json.git
fix omitempty string is nil for marshaler
This commit is contained in:
parent
7b8b524c92
commit
4d0a50640b
|
@ -2339,3 +2339,22 @@ func TestIssue318(t *testing.T) {
|
||||||
t.Fatalf("failed to encode. expected %s but got %s", expected, string(b))
|
t.Fatalf("failed to encode. expected %s but got %s", expected, string(b))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type emptyStringMarshaler struct {
|
||||||
|
Value stringMarshaler `json:"value,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type stringMarshaler string
|
||||||
|
|
||||||
|
func (s stringMarshaler) MarshalJSON() ([]byte, error) {
|
||||||
|
return []byte(`"` + s + `"`), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyStringMarshaler(t *testing.T) {
|
||||||
|
value := emptyStringMarshaler{}
|
||||||
|
expected, err := stdjson.Marshal(value)
|
||||||
|
assertErr(t, err)
|
||||||
|
got, err := json.Marshal(value)
|
||||||
|
assertErr(t, err)
|
||||||
|
assertEq(t, "struct", string(expected), string(got))
|
||||||
|
}
|
||||||
|
|
|
@ -589,6 +589,8 @@ func IsNilForMarshaler(v interface{}) bool {
|
||||||
return rv.IsNil()
|
return rv.IsNil()
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
return rv.IsNil() || rv.Len() == 0
|
return rv.IsNil() || rv.Len() == 0
|
||||||
|
case reflect.String:
|
||||||
|
return rv.Len() == 0
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue