mirror of
https://github.com/json-iterator/go.git
synced 2025-06-06 22:36:25 +02:00
Merge branch 'javierprovecho-master'
This commit is contained in:
commit
37ba1b32b5
@ -200,7 +200,12 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
|||||||
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)
|
||||||
if field.Anonymous && (field.Tag.Get("json") == "" || strings.Split(field.Tag.Get("json"), ",")[0] == "") {
|
tag := field.Tag.Get("json")
|
||||||
|
tagParts := strings.Split(tag, ",")
|
||||||
|
if tag == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if field.Anonymous && (tag == "" || tagParts[0] == "") {
|
||||||
if field.Type.Kind() == reflect.Struct {
|
if field.Type.Kind() == reflect.Struct {
|
||||||
structDescriptor, err := describeStruct(cfg, field.Type)
|
structDescriptor, err := describeStruct(cfg, field.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -231,8 +236,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
fieldNames := calcFieldNames(field.Name, tagParts[0], tag)
|
||||||
fieldNames := calcFieldNames(field.Name, tagParts[0], string(field.Tag.Get("json")))
|
|
||||||
fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
|
||||||
decoder := fieldDecoders[fieldCacheKey]
|
decoder := fieldDecoders[fieldCacheKey]
|
||||||
if decoder == nil {
|
if decoder == nil {
|
||||||
|
62
jsoniter_alias_test.go
Normal file
62
jsoniter_alias_test.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_alias(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type myint int
|
||||||
|
type myint8 int8
|
||||||
|
type myint16 int16
|
||||||
|
type myint32 int32
|
||||||
|
type myint64 int64
|
||||||
|
type myuint uint
|
||||||
|
type myuint8 uint8
|
||||||
|
type myuint16 uint16
|
||||||
|
type myuint32 uint32
|
||||||
|
type myuint64 uint64
|
||||||
|
type myfloat32 float32
|
||||||
|
type myfloat64 float64
|
||||||
|
type mystring string
|
||||||
|
type mybool bool
|
||||||
|
type myuintptr uintptr
|
||||||
|
var a struct {
|
||||||
|
A myint8 `json:"a"`
|
||||||
|
B myint16 `json:"b"`
|
||||||
|
C myint32 `json:"c"`
|
||||||
|
D myint64 `json:"d"`
|
||||||
|
E myuint8 `json:"e"`
|
||||||
|
F myuint16 `json:"f"`
|
||||||
|
G myuint32 `json:"g"`
|
||||||
|
H myuint64 `json:"h"`
|
||||||
|
I myfloat32 `json:"i"`
|
||||||
|
J myfloat64 `json:"j"`
|
||||||
|
K mystring `json:"k"`
|
||||||
|
L myint `json:"l"`
|
||||||
|
M myuint `json:"m"`
|
||||||
|
N mybool `json:"n"`
|
||||||
|
O myuintptr `json:"o"`
|
||||||
|
}
|
||||||
|
|
||||||
|
should.Nil(UnmarshalFromString(`{"a" : 1, "b" : 1, "c": 1, "d" : 1, "e" : 1, "f" : 1, "g" : 1, "h": 1, "i" : 1, "j" : 1, "k" :"xxxx", "l" : 1, "m":1, "n": true, "o" : 1}`, &a))
|
||||||
|
should.Equal(myfloat32(1), a.I)
|
||||||
|
should.Equal(myfloat64(1), a.J)
|
||||||
|
should.Equal(myint8(1), a.A)
|
||||||
|
should.Equal(myint16(1), a.B)
|
||||||
|
should.Equal(myint32(1), a.C)
|
||||||
|
should.Equal(myint64(1), a.D)
|
||||||
|
should.Equal(myuint8(1), a.E)
|
||||||
|
should.Equal(myuint16(1), a.F)
|
||||||
|
should.Equal(myuint32(1), a.G)
|
||||||
|
should.Equal(myuint64(1), a.H)
|
||||||
|
should.Equal(mystring("xxxx"), a.K)
|
||||||
|
should.Equal(mybool(true), a.N)
|
||||||
|
should.Equal(myuintptr(1), a.O)
|
||||||
|
b, err := Marshal(a)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"a":1,"b":1,"c":1,"d":1,"e":1,"f":1,"g":1,"h":1,"i":1,"j":1,"k":"xxxx","l":1,"m":1,"n":true,"o":1}`, string(b))
|
||||||
|
|
||||||
|
}
|
@ -6,10 +6,11 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_uint64_invalid(t *testing.T) {
|
func Test_read_uint64_invalid(t *testing.T) {
|
||||||
@ -81,12 +82,52 @@ func Test_read_int64_array(t *testing.T) {
|
|||||||
should.Equal(3, len(val))
|
should.Equal(3, len(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_int32_overflow(t *testing.T) {
|
func Test_read_int_overflow(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
input := "123456789123456789,"
|
inputArr := []string{"123451", "-123451"}
|
||||||
iter := ParseString(ConfigDefault, input)
|
for _, s := range inputArr {
|
||||||
|
iter := ParseString(ConfigDefault, s)
|
||||||
|
iter.ReadInt8()
|
||||||
|
should.NotNil(iter.Error)
|
||||||
|
|
||||||
|
iterU := ParseString(ConfigDefault, s)
|
||||||
|
iterU.ReadUint8()
|
||||||
|
should.NotNil(iterU.Error)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
inputArr = []string{"12345678912", "-12345678912"}
|
||||||
|
for _, s := range inputArr {
|
||||||
|
iter := ParseString(ConfigDefault, s)
|
||||||
|
iter.ReadInt16()
|
||||||
|
should.NotNil(iter.Error)
|
||||||
|
|
||||||
|
iterUint := ParseString(ConfigDefault, s)
|
||||||
|
iterUint.ReadUint16()
|
||||||
|
should.NotNil(iterUint.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputArr = []string{"3111111111", "-3111111111", "1234232323232323235678912", "-1234567892323232323212"}
|
||||||
|
for _, s := range inputArr {
|
||||||
|
iter := ParseString(ConfigDefault, s)
|
||||||
iter.ReadInt32()
|
iter.ReadInt32()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
|
|
||||||
|
iterUint := ParseString(ConfigDefault, s)
|
||||||
|
iterUint.ReadUint32()
|
||||||
|
should.NotNil(iterUint.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputArr = []string{"9223372036854775811", "-9523372036854775807", "1234232323232323235678912", "-1234567892323232323212"}
|
||||||
|
for _, s := range inputArr {
|
||||||
|
iter := ParseString(ConfigDefault, s)
|
||||||
|
iter.ReadInt64()
|
||||||
|
should.NotNil(iter.Error)
|
||||||
|
|
||||||
|
iterUint := ParseString(ConfigDefault, s)
|
||||||
|
iterUint.ReadUint64()
|
||||||
|
should.NotNil(iterUint.Error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_int64(t *testing.T) {
|
func Test_read_int64(t *testing.T) {
|
||||||
|
@ -129,3 +129,19 @@ func Test_encode_map_with_sorted_keys(t *testing.T) {
|
|||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(string(bytes), output)
|
should.Equal(string(bytes), output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_encode_map_uint_keys(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
m := map[uint64]interface{}{
|
||||||
|
uint64(1): "a",
|
||||||
|
uint64(2): "a",
|
||||||
|
uint64(4): "a",
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(m)
|
||||||
|
should.Nil(err)
|
||||||
|
|
||||||
|
output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
|
||||||
|
should.Equal(string(bytes), output)
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -404,6 +404,20 @@ func Test_omit_empty(t *testing.T) {
|
|||||||
should.Equal(`{"field-2":"hello"}`, str)
|
should.Equal(`{"field-2":"hello"}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_ignore_field_on_not_valid_type(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type TestObject struct {
|
||||||
|
Field1 string `json:"field-1,omitempty"`
|
||||||
|
Field2 func() `json:"-"`
|
||||||
|
}
|
||||||
|
obj := TestObject{}
|
||||||
|
obj.Field1 = "hello world"
|
||||||
|
obj.Field2 = func() {}
|
||||||
|
str, err := MarshalToString(&obj)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`{"field-1":"hello world"}`, str)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_recursive_struct(t *testing.T) {
|
func Test_recursive_struct(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user