mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
#91 fix one ptr embedded struct
This commit is contained in:
parent
ea8fa7cc63
commit
04eae11ba5
@ -277,6 +277,8 @@ func createStructDescriptor(cfg *frozenConfig, typ reflect.Type, bindings []*Bin
|
|||||||
fallthrough
|
fallthrough
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
onePtrOptimization = true
|
onePtrOptimization = true
|
||||||
|
case reflect.Struct:
|
||||||
|
onePtrOptimization = isStructOnePtr(firstField.Type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
structDescriptor := &StructDescriptor{
|
structDescriptor := &StructDescriptor{
|
||||||
@ -296,6 +298,21 @@ func createStructDescriptor(cfg *frozenConfig, typ reflect.Type, bindings []*Bin
|
|||||||
return structDescriptor
|
return structDescriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isStructOnePtr(typ reflect.Type) bool {
|
||||||
|
if typ.NumField() == 1 {
|
||||||
|
firstField := typ.Field(0)
|
||||||
|
switch firstField.Type.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
return true
|
||||||
|
case reflect.Map:
|
||||||
|
return true
|
||||||
|
case reflect.Struct:
|
||||||
|
return isStructOnePtr(firstField.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type sortableBindings []*Binding
|
type sortableBindings []*Binding
|
||||||
|
|
||||||
func (bindings sortableBindings) Len() int {
|
func (bindings sortableBindings) Len() int {
|
||||||
|
@ -186,6 +186,25 @@ func Test_nested_one_field_struct(t *testing.T) {
|
|||||||
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
|
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_struct_with_embedded_ptr_with_tag(t *testing.T) {
|
||||||
|
type O1 struct {
|
||||||
|
O1F string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option struct {
|
||||||
|
O1 *O1
|
||||||
|
}
|
||||||
|
|
||||||
|
type T struct {
|
||||||
|
Option `json:","`
|
||||||
|
}
|
||||||
|
var obj T
|
||||||
|
should := require.New(t)
|
||||||
|
output, err := MarshalToString(obj)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"O1":null}`, output)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_struct_with_one_nil(t *testing.T) {
|
func Test_struct_with_one_nil(t *testing.T) {
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
F *float64
|
F *float64
|
||||||
|
Loading…
x
Reference in New Issue
Block a user