forked from mirror/go-json
Merge pull request #323 from IncSW/fix-omitempty-string-marshaler
Fix IsNilForMarshaler for string type with omitempty
This commit is contained in:
commit
8923f69dd5
|
@ -2339,3 +2339,22 @@ func TestIssue318(t *testing.T) {
|
|||
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()
|
||||
case reflect.Slice:
|
||||
return rv.IsNil() || rv.Len() == 0
|
||||
case reflect.String:
|
||||
return rv.Len() == 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue