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:
parent
91b9e828b7
commit
a92111261c
@ -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,
|
||||
|
@ -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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user