1
0
mirror of https://github.com/json-iterator/go.git synced 2025-02-01 19:14:29 +02:00

fix map[string]interface{} in struct

This commit is contained in:
Tao Wen 2017-02-03 18:44:54 +08:00
parent 1e91dbbf58
commit 6880076b44
6 changed files with 42 additions and 3 deletions

View File

@ -569,7 +569,6 @@ func (any *objectAny) IterateObject() (func() (string, Any, bool), bool) {
var fieldValueAsAny Any
if i == len(cacheKeys) {
fieldName = any.val.Type().Field(i).Name
fmt.Println(fieldName)
cacheKeys = append(cacheKeys, fieldName)
fieldValue := any.val.Field(i)
if fieldValue.CanInterface() {

View File

@ -226,7 +226,7 @@ func (codec *interfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
}
func (encoder *interfaceCodec) encodeInterface(val interface{}, stream *Stream) {
WriteToStream(val, stream, encoder)
stream.WriteVal(val)
}
type anyCodec struct {

View File

@ -37,6 +37,11 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
if err != nil {
return prefix(fmt.Sprintf("{%s}", field.Name)).addToEncoder(encoder, err)
}
// map is stored as pointer in the struct
// but if struct only has one map, it is inlined
if field.Type.Kind() == reflect.Map && typ.NumField() > 1 {
encoder = &optionalEncoder{field.Type, encoder}
}
for _, fieldName := range fieldNames {
if structEncoder_.firstField == nil {
structEncoder_.firstField = &structFieldEncoder{&field, fieldName, encoder}

View File

@ -241,6 +241,18 @@ func Test_write_val_empty_array(t *testing.T) {
should.Equal("[]", str)
}
func Test_write_array_of_interface_in_struct(t *testing.T) {
should := require.New(t)
type TestObject struct {
Field []interface{}
Field2 string
}
val := TestObject{[]interface{}{1, 2}, ""}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"Field":[1,2],"Field2":""}`, str)
}
func Benchmark_jsoniter_array(b *testing.B) {
b.ReportAllocs()
input := []byte(`[1,2,3,4,5,6,7,8,9]`)

View File

@ -36,4 +36,4 @@ func Test_iterator_and_bind_api(t *testing.T) {
iter.ReadVal(&user)
iter.ReadArray() // array end
fmt.Println(user)
}
}

View File

@ -22,6 +22,29 @@ func Test_write_map_of_interface(t *testing.T) {
should.Equal(`{"hello":"world"}`, str)
}
func Test_write_map_of_interface_in_struct(t *testing.T) {
type TestObject struct {
Field map[string]interface{}
}
should := require.New(t)
val := TestObject{map[string]interface{}{"hello":"world"}}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"Field":{"hello":"world"}}`, str)
}
func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
type TestObject struct {
Field map[string]interface{}
Field2 string
}
should := require.New(t)
val := TestObject{map[string]interface{}{"hello":"world"}, ""}
str, err := MarshalToString(val)
should.Nil(err)
should.Equal(`{"Field":{"hello":"world"},"Field2":""}`, str)
}
type MyInterface interface {
Hello() string
}