diff --git a/encode_test.go b/encode_test.go index ce6a336..58e50b5 100644 --- a/encode_test.go +++ b/encode_test.go @@ -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)) +} diff --git a/internal/encoder/encoder.go b/internal/encoder/encoder.go index d210ec4..14eb6a0 100644 --- a/internal/encoder/encoder.go +++ b/internal/encoder/encoder.go @@ -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 }