1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-29 21:20:52 +02:00

fix anonymous fields

This commit is contained in:
Tao Wen 2017-06-21 00:26:18 +08:00
parent 3333ec11a0
commit ff3c624fa9
12 changed files with 58 additions and 53 deletions

View File

@ -1,12 +1,12 @@
package extra package extra
import ( import (
"github.com/json-iterator/go"
"unsafe"
"encoding/json" "encoding/json"
"strings" "github.com/json-iterator/go"
"math" "math"
"reflect" "reflect"
"strings"
"unsafe"
) )
const MaxUint = ^uint(0) const MaxUint = ^uint(0)

View File

@ -1,9 +1,9 @@
package extra package extra
import ( import (
"testing"
"github.com/json-iterator/go" "github.com/json-iterator/go"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"testing"
) )
func init() { func init() {
@ -98,4 +98,4 @@ func Test_empty_array_as_object(t *testing.T) {
var val struct{} var val struct{}
should.Nil(jsoniter.UnmarshalFromString(`[]`, &val)) should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
should.Equal(struct{}{}, val) should.Equal(struct{}{}, val)
} }

View File

@ -1,9 +1,9 @@
package extra package extra
import ( import (
"testing"
"github.com/json-iterator/go" "github.com/json-iterator/go"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"testing"
) )
func Test_lower_case_with_underscores(t *testing.T) { func Test_lower_case_with_underscores(t *testing.T) {

View File

@ -1,9 +1,9 @@
package extra package extra
import ( import (
"testing"
"github.com/json-iterator/go/require"
"github.com/json-iterator/go" "github.com/json-iterator/go"
"github.com/json-iterator/go/require"
"testing"
) )
func Test_private_fields(t *testing.T) { func Test_private_fields(t *testing.T) {

View File

@ -2,8 +2,8 @@ package extra
import ( import (
"github.com/json-iterator/go" "github.com/json-iterator/go"
"unsafe"
"time" "time"
"unsafe"
) )
// keep epoch milliseconds // keep epoch milliseconds
@ -15,6 +15,7 @@ func RegisterTimeAsInt64Codec(precision time.Duration) {
type timeAsInt64Codec struct { type timeAsInt64Codec struct {
precision time.Duration precision time.Duration
} }
func (codec *timeAsInt64Codec) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) { func (codec *timeAsInt64Codec) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
nanoseconds := iter.ReadInt64() * codec.precision.Nanoseconds() nanoseconds := iter.ReadInt64() * codec.precision.Nanoseconds()
*((*time.Time)(ptr)) = time.Unix(0, nanoseconds) *((*time.Time)(ptr)) = time.Unix(0, nanoseconds)

View File

@ -1,10 +1,10 @@
package extra package extra
import ( import (
"github.com/json-iterator/go"
"github.com/json-iterator/go/require"
"testing" "testing"
"time" "time"
"github.com/json-iterator/go/require"
"github.com/json-iterator/go"
) )
func Test_time_as_int64(t *testing.T) { func Test_time_as_int64(t *testing.T) {

View File

@ -10,11 +10,11 @@ import (
) )
type Config struct { type Config struct {
IndentionStep int IndentionStep int
MarshalFloatWith6Digits bool MarshalFloatWith6Digits bool
EscapeHtml bool EscapeHtml bool
SortMapKeys bool SortMapKeys bool
UseNumber bool UseNumber bool
} }
type frozenConfig struct { type frozenConfig struct {

View File

@ -1,8 +1,8 @@
package jsoniter package jsoniter
import ( import (
"strconv"
"math" "math"
"strconv"
) )
var intDigits []int8 var intDigits []int8

View File

@ -1,11 +1,11 @@
package jsoniter package jsoniter
import ( import (
"reflect"
"fmt" "fmt"
"unsafe" "reflect"
"strings" "strings"
"unicode" "unicode"
"unsafe"
) )
var typeDecoders = map[string]ValDecoder{} var typeDecoders = map[string]ValDecoder{}
@ -192,6 +192,7 @@ func _getTypeEncoderFromExtension(typ reflect.Type) ValEncoder {
} }
func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, error) { func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, error) {
anonymousBindings := []*Binding{}
bindings := []*Binding{} bindings := []*Binding{}
for i := 0; i < typ.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i) field := typ.Field(i)
@ -202,7 +203,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
return nil, err return nil, err
} }
for _, binding := range structDescriptor.Fields { for _, binding := range structDescriptor.Fields {
bindings = append(bindings, binding) anonymousBindings = append(anonymousBindings, binding)
} }
} else if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct { } else if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {
structDescriptor, err := describeStruct(cfg, field.Type.Elem()) structDescriptor, err := describeStruct(cfg, field.Type.Elem())
@ -214,7 +215,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
binding.Encoder = &structFieldEncoder{&field, binding.Encoder, false} binding.Encoder = &structFieldEncoder{&field, binding.Encoder, false}
binding.Decoder = &optionalDecoder{field.Type, binding.Decoder} binding.Decoder = &optionalDecoder{field.Type, binding.Decoder}
binding.Decoder = &structFieldDecoder{&field, binding.Decoder} binding.Decoder = &structFieldDecoder{&field, binding.Decoder}
bindings = append(bindings, binding) anonymousBindings = append(anonymousBindings, binding)
} }
} }
} else { } else {
@ -272,6 +273,8 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
binding.Decoder = &structFieldDecoder{binding.Field, binding.Decoder} binding.Decoder = &structFieldDecoder{binding.Field, binding.Decoder}
binding.Encoder = &structFieldEncoder{binding.Field, binding.Encoder, shouldOmitEmpty} binding.Encoder = &structFieldEncoder{binding.Field, binding.Encoder, shouldOmitEmpty}
} }
// insert anonymous bindings to the head
structDescriptor.Fields = append(anonymousBindings, structDescriptor.Fields...)
return structDescriptor, nil return structDescriptor, nil
} }

View File

@ -105,7 +105,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &threeFieldsStructDecoder{typ, return &threeFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3}, nil fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3}, nil
case 4: case 4:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -138,8 +138,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &fourFieldsStructDecoder{typ, return &fourFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4}, nil fieldName4, fieldDecoder4}, nil
case 5: case 5:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -177,8 +177,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &fiveFieldsStructDecoder{typ, return &fiveFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5}, nil fieldName4, fieldDecoder4, fieldName5, fieldDecoder5}, nil
case 6: case 6:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -221,8 +221,8 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &sixFieldsStructDecoder{typ, return &sixFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6}, nil fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6}, nil
case 7: case 7:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -270,9 +270,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &sevenFieldsStructDecoder{typ, return &sevenFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7}, nil fieldName7, fieldDecoder7}, nil
case 8: case 8:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -325,9 +325,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &eightFieldsStructDecoder{typ, return &eightFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8}, nil fieldName7, fieldDecoder7, fieldName8, fieldDecoder8}, nil
case 9: case 9:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -385,9 +385,9 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &nineFieldsStructDecoder{typ, return &nineFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9}, nil fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9}, nil
case 10: case 10:
var fieldName1 int32 var fieldName1 int32
var fieldName2 int32 var fieldName2 int32
@ -450,10 +450,10 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
} }
} }
return &tenFieldsStructDecoder{typ, return &tenFieldsStructDecoder{typ,
fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3, fieldName1, fieldDecoder1, fieldName2, fieldDecoder2, fieldName3, fieldDecoder3,
fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6, fieldName4, fieldDecoder4, fieldName5, fieldDecoder5, fieldName6, fieldDecoder6,
fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9, fieldName7, fieldDecoder7, fieldName8, fieldDecoder8, fieldName9, fieldDecoder9,
fieldName10, fieldDecoder10}, nil fieldName10, fieldDecoder10}, nil
} }
return &generalStructDecoder{typ, fields}, nil return &generalStructDecoder{typ, fields}, nil
} }

View File

@ -68,11 +68,11 @@ func Test_decode_int_key_map(t *testing.T) {
func Test_encode_TextMarshaler_key_map(t *testing.T) { func Test_encode_TextMarshaler_key_map(t *testing.T) {
should := require.New(t) should := require.New(t)
f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero) f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero)
val := map[*big.Float]string{f: "2"} val := map[*big.Float]string{f: "2"}
str, err := MarshalToString(val) str, err := MarshalToString(val)
should.Nil(err) should.Nil(err)
should.Equal(`{"1":"2"}`, str) should.Equal(`{"1":"2"}`, str)
} }
func Test_decode_TextMarshaler_key_map(t *testing.T) { func Test_decode_TextMarshaler_key_map(t *testing.T) {

View File

@ -208,9 +208,9 @@ func Test_decode_struct_field_with_tag(t *testing.T) {
func Test_decode_struct_field_with_tag_string(t *testing.T) { func Test_decode_struct_field_with_tag_string(t *testing.T) {
should := require.New(t) should := require.New(t)
type TestObject struct { type TestObject struct {
Field1 int `json:",string"` Field1 int `json:",string"`
} }
obj := TestObject{Field1: 100} obj := TestObject{Field1: 100}
should.Nil(UnmarshalFromString(`{"Field1": "100"}`, &obj)) should.Nil(UnmarshalFromString(`{"Field1": "100"}`, &obj))
should.Equal(100, obj.Field1) should.Equal(100, obj.Field1)
@ -346,8 +346,10 @@ func Test_multiple_level_anonymous_struct(t *testing.T) {
Field3 string Field3 string
} }
should := require.New(t) should := require.New(t)
output, err := MarshalToString(Level3{Level2{Level1{"1"}, "2"}, "3"}) obj := Level3{Level2{Level1{"1"}, "2"}, "3"}
output, err := MarshalToString(obj)
should.Nil(err) should.Nil(err)
fmt.Println(output)
should.Contains(output, `"Field1":"1"`) should.Contains(output, `"Field1":"1"`)
should.Contains(output, `"Field2":"2"`) should.Contains(output, `"Field2":"2"`)
should.Contains(output, `"Field3":"3"`) should.Contains(output, `"Field3":"3"`)
@ -369,7 +371,8 @@ func Test_multiple_level_anonymous_struct_with_ptr(t *testing.T) {
Field3 string Field3 string
} }
should := require.New(t) should := require.New(t)
output, err := MarshalToString(Level3{&Level2{&Level1{"1", "", "4"}, "2", ""}, "3"}) obj := Level3{&Level2{&Level1{"1", "", "4"}, "2", ""}, "3"}
output, err := MarshalToString(obj)
should.Nil(err) should.Nil(err)
should.Contains(output, `"Field1":"1"`) should.Contains(output, `"Field1":"1"`)
should.Contains(output, `"Field2":"2"`) should.Contains(output, `"Field2":"2"`)
@ -377,8 +380,6 @@ func Test_multiple_level_anonymous_struct_with_ptr(t *testing.T) {
should.Contains(output, `"Field4":"4"`) should.Contains(output, `"Field4":"4"`)
} }
func Test_shadow_struct_field(t *testing.T) { func Test_shadow_struct_field(t *testing.T) {
should := require.New(t) should := require.New(t)
type omit *struct{} type omit *struct{}
@ -393,7 +394,7 @@ func Test_shadow_struct_field(t *testing.T) {
OmitMaxAge omit `json:"cacheAge,omitempty"` OmitMaxAge omit `json:"cacheAge,omitempty"`
// Add nice keys // Add nice keys
MaxAge int `json:"max_age"` MaxAge int `json:"max_age"`
}{ }{
CacheItem: &CacheItem{ CacheItem: &CacheItem{
Key: "value", Key: "value",