diff --git a/jsoniter_must_be_valid_test.go b/any_tests/jsoniter_must_be_valid_test.go similarity index 63% rename from jsoniter_must_be_valid_test.go rename to any_tests/jsoniter_must_be_valid_test.go index 01a91c9..4b5d262 100644 --- a/jsoniter_must_be_valid_test.go +++ b/any_tests/jsoniter_must_be_valid_test.go @@ -1,71 +1,72 @@ -package jsoniter +package any_tests import ( "testing" "github.com/stretchr/testify/require" + "github.com/json-iterator/go" ) // if must be valid is useless, just drop this test func Test_must_be_valid(t *testing.T) { should := require.New(t) - any := Get([]byte("123")) + any := jsoniter.Get([]byte("123")) should.Equal(any.MustBeValid().ToInt(), 123) - any = Wrap(int8(10)) + any = jsoniter.Wrap(int8(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(int16(10)) + any = jsoniter.Wrap(int16(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(int32(10)) + any = jsoniter.Wrap(int32(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(int64(10)) + any = jsoniter.Wrap(int64(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(uint(10)) + any = jsoniter.Wrap(uint(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(uint8(10)) + any = jsoniter.Wrap(uint8(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(uint16(10)) + any = jsoniter.Wrap(uint16(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(uint32(10)) + any = jsoniter.Wrap(uint32(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(uint64(10)) + any = jsoniter.Wrap(uint64(10)) should.Equal(any.MustBeValid().ToInt(), 10) - any = Wrap(float32(10)) + any = jsoniter.Wrap(float32(10)) should.Equal(any.MustBeValid().ToFloat64(), float64(10)) - any = Wrap(float64(10)) + any = jsoniter.Wrap(float64(10)) should.Equal(any.MustBeValid().ToFloat64(), float64(10)) - any = Wrap(true) + any = jsoniter.Wrap(true) should.Equal(any.MustBeValid().ToFloat64(), float64(1)) - any = Wrap(false) + any = jsoniter.Wrap(false) should.Equal(any.MustBeValid().ToFloat64(), float64(0)) - any = Wrap(nil) + any = jsoniter.Wrap(nil) should.Equal(any.MustBeValid().ToFloat64(), float64(0)) - any = Wrap(struct{ age int }{age: 1}) + any = jsoniter.Wrap(struct{ age int }{age: 1}) should.Equal(any.MustBeValid().ToFloat64(), float64(0)) - any = Wrap(map[string]interface{}{"abc": 1}) + any = jsoniter.Wrap(map[string]interface{}{"abc": 1}) should.Equal(any.MustBeValid().ToFloat64(), float64(0)) - any = Wrap("abc") + any = jsoniter.Wrap("abc") should.Equal(any.MustBeValid().ToFloat64(), float64(0)) - any = Wrap([]int{}) + any = jsoniter.Wrap([]int{}) should.Equal(any.MustBeValid().ToFloat64(), float64(0)) - any = Wrap([]int{1, 2}) + any = jsoniter.Wrap([]int{1, 2}) should.Equal(any.MustBeValid().ToFloat64(), float64(1)) } diff --git a/jsoniter_null_test.go b/jsoniter_null_test.go deleted file mode 100644 index 8c89147..0000000 --- a/jsoniter_null_test.go +++ /dev/null @@ -1,168 +0,0 @@ -package jsoniter - -import ( - "bytes" - "encoding/json" - "io" - "testing" - - "github.com/stretchr/testify/require" -) - -func Test_read_null(t *testing.T) { - should := require.New(t) - iter := ParseString(ConfigDefault, `null`) - should.True(iter.ReadNil()) - iter = ParseString(ConfigDefault, `null`) - should.Nil(iter.Read()) - iter = ParseString(ConfigDefault, `navy`) - iter.Read() - should.True(iter.Error != nil && iter.Error != io.EOF) - iter = ParseString(ConfigDefault, `navy`) - iter.ReadNil() - should.True(iter.Error != nil && iter.Error != io.EOF) -} - -func Test_write_null(t *testing.T) { - should := require.New(t) - buf := &bytes.Buffer{} - stream := NewStream(ConfigDefault, buf, 4096) - stream.WriteNil() - stream.Flush() - should.Nil(stream.Error) - should.Equal("null", buf.String()) -} - -func Test_encode_null(t *testing.T) { - should := require.New(t) - str, err := MarshalToString(nil) - should.Nil(err) - should.Equal("null", str) -} - -func Test_decode_null_object_field(t *testing.T) { - should := require.New(t) - iter := ParseString(ConfigDefault, `[null,"a"]`) - iter.ReadArray() - if iter.ReadObject() != "" { - t.FailNow() - } - iter.ReadArray() - if iter.ReadString() != "a" { - t.FailNow() - } - type TestObject struct { - Field string - } - objs := []TestObject{} - should.Nil(UnmarshalFromString("[null]", &objs)) - should.Len(objs, 1) -} - -func Test_decode_null_array_element(t *testing.T) { - should := require.New(t) - iter := ParseString(ConfigDefault, `[null,"a"]`) - should.True(iter.ReadArray()) - should.True(iter.ReadNil()) - should.True(iter.ReadArray()) - should.Equal("a", iter.ReadString()) -} - -func Test_decode_null_array(t *testing.T) { - should := require.New(t) - arr := []string{} - should.Nil(UnmarshalFromString("null", &arr)) - should.Nil(arr) -} - -func Test_decode_null_map(t *testing.T) { - should := require.New(t) - arr := map[string]string{} - should.Nil(UnmarshalFromString("null", &arr)) - should.Nil(arr) -} - -func Test_decode_null_string(t *testing.T) { - should := require.New(t) - iter := ParseString(ConfigDefault, `[null,"a"]`) - should.True(iter.ReadArray()) - should.Equal("", iter.ReadString()) - should.True(iter.ReadArray()) - should.Equal("a", iter.ReadString()) -} - -func Test_decode_null_skip(t *testing.T) { - iter := ParseString(ConfigDefault, `[null,"a"]`) - iter.ReadArray() - iter.Skip() - iter.ReadArray() - if iter.ReadString() != "a" { - t.FailNow() - } -} - -func Test_encode_nil_map(t *testing.T) { - should := require.New(t) - type Ttest map[string]string - var obj1 Ttest - output, err := json.Marshal(obj1) - should.Nil(err) - should.Equal("null", string(output)) - output, err = json.Marshal(&obj1) - should.Nil(err) - should.Equal("null", string(output)) - output, err = Marshal(obj1) - should.Nil(err) - should.Equal("null", string(output)) - output, err = Marshal(&obj1) - should.Nil(err) - should.Equal("null", string(output)) -} - -func Test_encode_nil_array(t *testing.T) { - should := require.New(t) - type Ttest []string - var obj1 Ttest - output, err := json.Marshal(obj1) - should.Nil(err) - should.Equal("null", string(output)) - output, err = json.Marshal(&obj1) - should.Nil(err) - should.Equal("null", string(output)) - output, err = Marshal(obj1) - should.Nil(err) - should.Equal("null", string(output)) - output, err = Marshal(&obj1) - should.Nil(err) - should.Equal("null", string(output)) -} - -func Test_decode_nil_num(t *testing.T) { - type TestData struct { - Field int `json:"field"` - } - should := require.New(t) - - data1 := []byte(`{"field": 42}`) - data2 := []byte(`{"field": null}`) - - // Checking stdlib behavior as well - obj2 := TestData{} - err := json.Unmarshal(data1, &obj2) - should.Equal(nil, err) - should.Equal(42, obj2.Field) - - err = json.Unmarshal(data2, &obj2) - should.Equal(nil, err) - should.Equal(42, obj2.Field) - - obj := TestData{} - - err = Unmarshal(data1, &obj) - should.Equal(nil, err) - should.Equal(42, obj.Field) - - err = Unmarshal(data2, &obj) - should.Equal(nil, err) - should.Equal(42, obj.Field) -} diff --git a/jsoniter_nested_test.go b/misc_tests/jsoniter_nested_test.go similarity index 83% rename from jsoniter_nested_test.go rename to misc_tests/jsoniter_nested_test.go index 29f8cee..e8ed6ec 100644 --- a/jsoniter_nested_test.go +++ b/misc_tests/jsoniter_nested_test.go @@ -1,9 +1,10 @@ -package jsoniter +package misc_tests import ( "encoding/json" "reflect" "testing" + "github.com/json-iterator/go" ) type Level1 struct { @@ -15,7 +16,7 @@ type Level2 struct { } func Test_nested(t *testing.T) { - iter := ParseString(ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`) + iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`) l1 := Level1{} for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() { switch l1Field { @@ -50,7 +51,7 @@ func Test_nested(t *testing.T) { func Benchmark_jsoniter_nested(b *testing.B) { for n := 0; n < b.N; n++ { - iter := ParseString(ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`) + iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`) l1 := Level1{} for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() { switch l1Field { @@ -63,7 +64,7 @@ func Benchmark_jsoniter_nested(b *testing.B) { } } -func readLevel1Hello(iter *Iterator) []Level2 { +func readLevel1Hello(iter *jsoniter.Iterator) []Level2 { l2Array := make([]Level2, 0, 2) for iter.ReadArray() { l2 := Level2{} diff --git a/misc_tests/jsoniter_null_test.go b/misc_tests/jsoniter_null_test.go new file mode 100644 index 0000000..36387d5 --- /dev/null +++ b/misc_tests/jsoniter_null_test.go @@ -0,0 +1,81 @@ +package misc_tests + +import ( + "bytes" + "io" + "testing" + + "github.com/stretchr/testify/require" + "github.com/json-iterator/go" +) + +func Test_read_null(t *testing.T) { + should := require.New(t) + iter := jsoniter.ParseString(jsoniter.ConfigDefault, `null`) + should.True(iter.ReadNil()) + iter = jsoniter.ParseString(jsoniter.ConfigDefault, `null`) + should.Nil(iter.Read()) + iter = jsoniter.ParseString(jsoniter.ConfigDefault, `navy`) + iter.Read() + should.True(iter.Error != nil && iter.Error != io.EOF) + iter = jsoniter.ParseString(jsoniter.ConfigDefault, `navy`) + iter.ReadNil() + should.True(iter.Error != nil && iter.Error != io.EOF) +} + +func Test_write_null(t *testing.T) { + should := require.New(t) + buf := &bytes.Buffer{} + stream := jsoniter.NewStream(jsoniter.ConfigDefault, buf, 4096) + stream.WriteNil() + stream.Flush() + should.Nil(stream.Error) + should.Equal("null", buf.String()) +} + +func Test_decode_null_object_field(t *testing.T) { + should := require.New(t) + iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`) + iter.ReadArray() + if iter.ReadObject() != "" { + t.FailNow() + } + iter.ReadArray() + if iter.ReadString() != "a" { + t.FailNow() + } + type TestObject struct { + Field string + } + objs := []TestObject{} + should.Nil(jsoniter.UnmarshalFromString("[null]", &objs)) + should.Len(objs, 1) +} + +func Test_decode_null_array_element(t *testing.T) { + should := require.New(t) + iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`) + should.True(iter.ReadArray()) + should.True(iter.ReadNil()) + should.True(iter.ReadArray()) + should.Equal("a", iter.ReadString()) +} + +func Test_decode_null_string(t *testing.T) { + should := require.New(t) + iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`) + should.True(iter.ReadArray()) + should.Equal("", iter.ReadString()) + should.True(iter.ReadArray()) + should.Equal("a", iter.ReadString()) +} + +func Test_decode_null_skip(t *testing.T) { + iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[null,"a"]`) + iter.ReadArray() + iter.Skip() + iter.ReadArray() + if iter.ReadString() != "a" { + t.FailNow() + } +} diff --git a/value_tests/map_test.go b/value_tests/map_test.go index 7ddda81..c9f9c9d 100644 --- a/value_tests/map_test.go +++ b/value_tests/map_test.go @@ -3,6 +3,7 @@ package test import "math/big" func init() { + nilMap := map[string]string(nil) marshalCases = append(marshalCases, map[string]interface{}{"abc": 1}, map[string]MyInterface{"hello": MyString("world")}, @@ -17,10 +18,15 @@ func init() { uint64(2): "a", uint64(4): "a", }, + nilMap, + &nilMap, ) unmarshalCases = append(unmarshalCases, unmarshalCase{ ptr: (*map[string]string)(nil), input: `{"k\"ey": "val"}`, + }, unmarshalCase{ + ptr: (*map[string]string)(nil), + input: `null`, }) } diff --git a/value_tests/slice_test.go b/value_tests/slice_test.go index e3779d4..17722c0 100644 --- a/value_tests/slice_test.go +++ b/value_tests/slice_test.go @@ -1,5 +1,14 @@ package test func init() { - marshalCases = append(marshalCases, []interface{}{"hello"}) -} \ No newline at end of file + nilSlice := []string(nil) + marshalCases = append(marshalCases, + []interface{}{"hello"}, + nilSlice, + &nilSlice, + ) + unmarshalCases = append(unmarshalCases, unmarshalCase{ + ptr: (*[]string)(nil), + input: "null", + }) +} diff --git a/value_tests/struct_test.go b/value_tests/struct_test.go index 79ca3b8..4dd5935 100644 --- a/value_tests/struct_test.go +++ b/value_tests/struct_test.go @@ -11,6 +11,11 @@ func init() { Field interface{} })(nil), input: `{"Field": "hello"}`, + }, unmarshalCase{ + ptr: (*struct { + Field int `json:"field"` + })(nil), + input: `{"field": null}`, }) marshalCases = append(marshalCases, struct { diff --git a/value_tests/value_test.go b/value_tests/value_test.go index ffa2c3a..8fffa46 100644 --- a/value_tests/value_test.go +++ b/value_tests/value_test.go @@ -15,7 +15,9 @@ type unmarshalCase struct { var unmarshalCases []unmarshalCase -var marshalCases []interface{} +var marshalCases = []interface{}{ + nil, +} func Test_unmarshal(t *testing.T) { should := require.New(t)