@@ -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() {
@@ -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 {
@@ -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}
@@ -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]`)
@@ -36,4 +36,4 @@ func Test_iterator_and_bind_api(t *testing.T) {
iter.ReadVal(&user)
iter.ReadArray() // array end
fmt.Println(user)
-}
@@ -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) {
+ Field map[string]interface{}
+ val := TestObject{map[string]interface{}{"hello":"world"}}
+ should.Equal(`{"Field":{"hello":"world"}}`, str)
+func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
+ val := TestObject{map[string]interface{}{"hello":"world"}, ""}
+ should.Equal(`{"Field":{"hello":"world"},"Field2":""}`, str)
type MyInterface interface {
Hello() string