mirror of
https://github.com/json-iterator/go.git
synced 2025-03-23 21:09:11 +02:00
#63 support decode anonymous struct
This commit is contained in:
parent
50583f6bae
commit
eecb062c32
@ -88,12 +88,11 @@ func listStructFields(typ reflect.Type) []*reflect.StructField {
|
|||||||
|
|
||||||
func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
||||||
fields := map[string]*structFieldDecoder{}
|
fields := map[string]*structFieldDecoder{}
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
for _, field := range listStructFields(typ) {
|
||||||
field := typ.Field(i)
|
|
||||||
fieldDecoderKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
fieldDecoderKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
||||||
var extensionProviedFieldNames []string
|
var extensionProviedFieldNames []string
|
||||||
for _, extension := range extensions {
|
for _, extension := range extensions {
|
||||||
alternativeFieldNames, _, fun := extension(typ, &field)
|
alternativeFieldNames, _, fun := extension(typ, field)
|
||||||
if alternativeFieldNames != nil {
|
if alternativeFieldNames != nil {
|
||||||
extensionProviedFieldNames = alternativeFieldNames
|
extensionProviedFieldNames = alternativeFieldNames
|
||||||
}
|
}
|
||||||
@ -102,7 +101,7 @@ func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, extension := range cfg.extensions {
|
for _, extension := range cfg.extensions {
|
||||||
alternativeFieldNames, _, fun := extension(typ, &field)
|
alternativeFieldNames, _, fun := extension(typ, field)
|
||||||
if alternativeFieldNames != nil {
|
if alternativeFieldNames != nil {
|
||||||
extensionProviedFieldNames = alternativeFieldNames
|
extensionProviedFieldNames = alternativeFieldNames
|
||||||
}
|
}
|
||||||
@ -126,7 +125,7 @@ func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, fieldName := range fieldNames {
|
for _, fieldName := range fieldNames {
|
||||||
fields[fieldName] = &structFieldDecoder{&field, decoder}
|
fields[fieldName] = &structFieldDecoder{field, decoder}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return createStructDecoder(typ, fields)
|
return createStructDecoder(typ, fields)
|
||||||
|
@ -128,6 +128,12 @@ func Test_decode_map_of_raw_message(t *testing.T) {
|
|||||||
var rawMap RawMap
|
var rawMap RawMap
|
||||||
should.Nil(Unmarshal(b, &rawMap))
|
should.Nil(Unmarshal(b, &rawMap))
|
||||||
should.Equal(`[{"key":"value"}]`, string(*rawMap["test"]))
|
should.Equal(`[{"key":"value"}]`, string(*rawMap["test"]))
|
||||||
|
type Inner struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
}
|
||||||
|
var inner []Inner
|
||||||
|
Unmarshal(*rawMap["test"], &inner)
|
||||||
|
should.Equal("value", inner[0].Key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_encode_map_of_raw_message(t *testing.T) {
|
func Test_encode_map_of_raw_message(t *testing.T) {
|
||||||
|
@ -293,7 +293,7 @@ func Test_one_field_struct(t *testing.T) {
|
|||||||
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
|
should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_anonymous_struct_marshal(t *testing.T) {
|
func Test_encode_anonymous_struct(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
Field string
|
Field string
|
||||||
@ -308,6 +308,21 @@ func Test_anonymous_struct_marshal(t *testing.T) {
|
|||||||
should.Equal(`{"Field":100}`, str)
|
should.Equal(`{"Field":100}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_decode_anonymous_struct(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type Inner struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Outer struct {
|
||||||
|
Inner
|
||||||
|
}
|
||||||
|
var outer Outer
|
||||||
|
j := []byte("{\"key\":\"value\"}")
|
||||||
|
should.Nil(Unmarshal(j, &outer))
|
||||||
|
should.Equal("value", outer.Key)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_decode_nested(t *testing.T) {
|
func Test_decode_nested(t *testing.T) {
|
||||||
type StructOfString struct {
|
type StructOfString struct {
|
||||||
Field1 string
|
Field1 string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user