diff --git a/feature_reflect.go b/feature_reflect.go index 05d91b4..7c3006e 100644 --- a/feature_reflect.go +++ b/feature_reflect.go @@ -466,6 +466,19 @@ func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error } return encoder, nil } + if reflect.PtrTo(typ).Implements(marshalerType) { + checkIsEmpty, err := createCheckIsEmpty(reflect.PtrTo(typ)) + if err != nil { + return nil, err + } + templateInterface := reflect.New(typ).Interface() + var encoder ValEncoder = &marshalerEncoder{ + templateInterface: extractInterface(templateInterface), + checkIsEmpty: checkIsEmpty, + } + encoder = &optionalEncoder{encoder} + return encoder, nil + } if typ.Implements(textMarshalerType) { checkIsEmpty, err := createCheckIsEmpty(typ) if err != nil { diff --git a/jsoniter_customize_test.go b/jsoniter_customize_test.go index b2d9d1b..e10d715 100644 --- a/jsoniter_customize_test.go +++ b/jsoniter_customize_test.go @@ -314,3 +314,27 @@ func Test_recursive_empty_interface_customization(t *testing.T) { Unmarshal([]byte("[100]"), &obj) should.Equal([]interface{}{int64(100)}, obj) } + +type GeoLocation struct { + Id string `json:"id,omitempty" db:"id"` +} + +func (p *GeoLocation) MarshalJSON() ([]byte, error) { + return []byte(`{}`), nil +} + +func (p *GeoLocation) UnmarshalJSON(input []byte) error { + p.Id = "hello" + return nil +} + +func Test_marshal_and_unmarshal_on_non_pointer(t *testing.T) { + should := require.New(t) + locations := []GeoLocation{{"000"}} + bytes, err := Marshal(locations) + should.Nil(err) + should.Equal("[{}]", string(bytes)) + err = Unmarshal([]byte("[1]"), &locations) + should.Nil(err) + should.Equal("hello", locations[0].Id) +} \ No newline at end of file