mirror of
https://github.com/json-iterator/go.git
synced 2025-04-01 21:24:21 +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 {
|
if field.Type.Kind() == reflect.Map && typ.NumField() > 1 {
|
||||||
encoder = &optionalEncoder{encoder}
|
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 {
|
for _, fieldName := range fieldNames {
|
||||||
structEncoder_.fields = append(structEncoder_.fields,
|
structEncoder_.fields = append(structEncoder_.fields,
|
||||||
|
@ -200,3 +200,18 @@ func Test_recursive_struct(t *testing.T) {
|
|||||||
err = UnmarshalFromString(str, &obj)
|
err = UnmarshalFromString(str, &obj)
|
||||||
should.Nil(err)
|
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