1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-23 21:09:11 +02:00

fix struct with one pointer field

This commit is contained in:
Tao Wen 2017-05-05 17:27:41 +08:00
parent 91b9e828b7
commit a92111261c
2 changed files with 19 additions and 0 deletions

View File

@ -51,6 +51,10 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
if field.Type.Kind() == reflect.Map && typ.NumField() > 1 {
encoder = &optionalEncoder{encoder}
}
// one field pointer field will be inlined
if field.Type.Kind() == reflect.Ptr && typ.NumField() == 1 {
encoder = (encoder.(*optionalEncoder)).valueEncoder
}
}
for _, fieldName := range fieldNames {
structEncoder_.fields = append(structEncoder_.fields,

View File

@ -200,3 +200,18 @@ func Test_recursive_struct(t *testing.T) {
err = UnmarshalFromString(str, &obj)
should.Nil(err)
}
func Test_one_field_struct(t *testing.T) {
should := require.New(t)
type AnotherObject struct {
}
type TestObject struct {
Me *AnotherObject
}
obj := TestObject{}
str, err := MarshalToString(obj)
should.Nil(err)
should.Equal(`{"Me":{}}`, str)
err = UnmarshalFromString(str, &obj)
should.Nil(err)
}