Parcourir la source

Merge branch 'javierprovecho-master'

Tao Wen il y a 8 ans
Parent
commit
37ba1b32b5
5 fichiers modifiés avec 146 ajouts et 9 suppressions
  1. 7 3
      feature_reflect_extension.go
  2. 62 0
      jsoniter_alias_test.go
  3. 47 6
      jsoniter_int_test.go
  4. 16 0
      jsoniter_map_test.go
  5. 14 0
      jsoniter_object_test.go

+ 7 - 3
feature_reflect_extension.go

@@ -200,7 +200,12 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
 	bindings := []*Binding{}
 	for i := 0; i < typ.NumField(); 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 {
 				structDescriptor, err := describeStruct(cfg, field.Type)
 				if err != nil {
@@ -231,8 +236,7 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
 				continue
 			}
 		}
-		tagParts := strings.Split(field.Tag.Get("json"), ",")
-		fieldNames := calcFieldNames(field.Name, tagParts[0], string(field.Tag.Get("json")))
+		fieldNames := calcFieldNames(field.Name, tagParts[0], tag)
 		fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
 		decoder := fieldDecoders[fieldCacheKey]
 		if decoder == nil {

+ 62 - 0
jsoniter_alias_test.go

@@ -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))
+
+}

+ 47 - 6
jsoniter_int_test.go

@@ -6,10 +6,11 @@ import (
 	"bytes"
 	"encoding/json"
 	"fmt"
-	"github.com/stretchr/testify/require"
 	"io/ioutil"
 	"strconv"
 	"testing"
+
+	"github.com/stretchr/testify/require"
 )
 
 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))
 }
 
-func Test_read_int32_overflow(t *testing.T) {
+func Test_read_int_overflow(t *testing.T) {
 	should := require.New(t)
-	input := "123456789123456789,"
-	iter := ParseString(ConfigDefault, input)
-	iter.ReadInt32()
-	should.NotNil(iter.Error)
+	inputArr := []string{"123451", "-123451"}
+	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()
+		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) {

+ 16 - 0
jsoniter_map_test.go

@@ -129,3 +129,19 @@ func Test_encode_map_with_sorted_keys(t *testing.T) {
 	should.Nil(err)
 	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)
+
+}

+ 14 - 0
jsoniter_object_test.go

@@ -404,6 +404,20 @@ func Test_omit_empty(t *testing.T) {
 	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) {
 	should := require.New(t)
 	type TestObject struct {