You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-15 22:50:24 +02:00
consolidate more tests
This commit is contained in:
123
any_tests/jsoniter_any_array_test.go
Normal file
123
any_tests/jsoniter_any_array_test.go
Normal file
@ -0,0 +1,123 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_read_empty_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[]"))
|
||||
should.Equal(jsoniter.ArrayValue, any.Get().ValueType())
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(0.3).ValueType())
|
||||
should.Equal(0, any.Size())
|
||||
should.Equal(jsoniter.ArrayValue, any.ValueType())
|
||||
should.Nil(any.LastError())
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(int32(0), any.ToInt32())
|
||||
should.Equal(int64(0), any.ToInt64())
|
||||
should.Equal(uint(0), any.ToUint())
|
||||
should.Equal(uint32(0), any.ToUint32())
|
||||
should.Equal(uint64(0), any.ToUint64())
|
||||
should.Equal(float32(0), any.ToFloat32())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
}
|
||||
|
||||
func Test_read_one_element_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[1]"))
|
||||
should.Equal(1, any.Size())
|
||||
}
|
||||
|
||||
func Test_read_two_element_array_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[1,2]"))
|
||||
should.Equal(1, any.Get(0).ToInt())
|
||||
should.Equal(2, any.Size())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(1, any.ToInt())
|
||||
should.Equal([]interface{}{float64(1), float64(2)}, any.GetInterface())
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("[1,2]", string(stream.Buffer()))
|
||||
arr := []int{}
|
||||
any.ToVal(&arr)
|
||||
should.Equal([]int{1, 2}, arr)
|
||||
}
|
||||
|
||||
func Test_wrap_array_and_convert_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap([]int{1, 2, 3})
|
||||
any2 := jsoniter.Wrap([]int{})
|
||||
|
||||
should.Equal("[1,2,3]", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
should.False(any2.ToBool())
|
||||
|
||||
should.Equal(1, any.ToInt())
|
||||
should.Equal(0, any2.ToInt())
|
||||
should.Equal(int32(1), any.ToInt32())
|
||||
should.Equal(int32(0), any2.ToInt32())
|
||||
should.Equal(int64(1), any.ToInt64())
|
||||
should.Equal(int64(0), any2.ToInt64())
|
||||
should.Equal(uint(1), any.ToUint())
|
||||
should.Equal(uint(0), any2.ToUint())
|
||||
should.Equal(uint32(1), any.ToUint32())
|
||||
should.Equal(uint32(0), any2.ToUint32())
|
||||
should.Equal(uint64(1), any.ToUint64())
|
||||
should.Equal(uint64(0), any2.ToUint64())
|
||||
should.Equal(float32(1), any.ToFloat32())
|
||||
should.Equal(float32(0), any2.ToFloat32())
|
||||
should.Equal(float64(1), any.ToFloat64())
|
||||
should.Equal(float64(0), any2.ToFloat64())
|
||||
should.Equal(3, any.Size())
|
||||
should.Equal(0, any2.Size())
|
||||
|
||||
var i interface{} = []int{1, 2, 3}
|
||||
should.Equal(i, any.GetInterface())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[1,[2,3],4]"))
|
||||
should.Equal(3, any.Get(1, 1).ToInt())
|
||||
should.Equal("[1,[2,3],4]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[[1],[2],[3,4]]"))
|
||||
should.Equal("[1,2,3]", any.Get('*', 0).ToString())
|
||||
any = jsoniter.Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0)
|
||||
should.Equal("[1,2,3]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_wrapper_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap([][]int{
|
||||
{1, 2},
|
||||
{3, 4},
|
||||
{5, 6},
|
||||
})
|
||||
should.Equal("[1,3,5]", any.Get('*', 0).ToString())
|
||||
should.Equal(jsoniter.ArrayValue, any.ValueType())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(1, any.Get(0, 0).ToInt())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("[]"))
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(1, 1).ValueType())
|
||||
should.NotNil(any.Get(1, 1).LastError())
|
||||
should.Equal(jsoniter.InvalidValue, any.Get("1").ValueType())
|
||||
should.NotNil(any.Get("1").LastError())
|
||||
}
|
||||
|
||||
func Test_invalid_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("["), 0)
|
||||
should.Equal(jsoniter.InvalidValue, any.ValueType())
|
||||
}
|
65
any_tests/jsoniter_any_bool_test.go
Normal file
65
any_tests/jsoniter_any_bool_test.go
Normal file
@ -0,0 +1,65 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var boolConvertMap = map[string]bool{
|
||||
"null": false,
|
||||
"true": true,
|
||||
"false": false,
|
||||
|
||||
`"true"`: true,
|
||||
`"false"`: true,
|
||||
|
||||
"123": true,
|
||||
`"123"`: true,
|
||||
"0": false,
|
||||
`"0"`: false,
|
||||
"-1": true,
|
||||
`"-1"`: true,
|
||||
|
||||
"1.1": true,
|
||||
"0.0": false,
|
||||
"-1.1": true,
|
||||
`""`: false,
|
||||
"[1,2]": true,
|
||||
"[]": false,
|
||||
"{}": true,
|
||||
`{"abc":1}`: true,
|
||||
}
|
||||
|
||||
func Test_read_bool_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
|
||||
var any jsoniter.Any
|
||||
for k, v := range boolConvertMap {
|
||||
any = jsoniter.Get([]byte(k))
|
||||
if v {
|
||||
should.True(any.ToBool(), fmt.Sprintf("origin val is %v", k))
|
||||
} else {
|
||||
should.False(any.ToBool(), fmt.Sprintf("origin val is %v", k))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_write_bool_to_stream(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("true"))
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("true", string(stream.Buffer()))
|
||||
should.Equal(any.ValueType(), jsoniter.BoolValue)
|
||||
|
||||
any = jsoniter.Get([]byte("false"))
|
||||
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("false", string(stream.Buffer()))
|
||||
|
||||
should.Equal(any.ValueType(), jsoniter.BoolValue)
|
||||
}
|
103
any_tests/jsoniter_any_float_test.go
Normal file
103
any_tests/jsoniter_any_float_test.go
Normal file
@ -0,0 +1,103 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var floatConvertMap = map[string]float64{
|
||||
"null": 0,
|
||||
"true": 1,
|
||||
"false": 0,
|
||||
|
||||
`"true"`: 0,
|
||||
`"false"`: 0,
|
||||
|
||||
"1e1": 10,
|
||||
"1e+1": 10,
|
||||
"1e-1": .1,
|
||||
"1E1": 10,
|
||||
"1E+1": 10,
|
||||
"1E-1": .1,
|
||||
|
||||
"-1e1": -10,
|
||||
"-1e+1": -10,
|
||||
"-1e-1": -.1,
|
||||
"-1E1": -10,
|
||||
"-1E+1": -10,
|
||||
"-1E-1": -.1,
|
||||
|
||||
`"1e1"`: 10,
|
||||
`"1e+1"`: 10,
|
||||
`"1e-1"`: .1,
|
||||
`"1E1"`: 10,
|
||||
`"1E+1"`: 10,
|
||||
`"1E-1"`: .1,
|
||||
|
||||
`"-1e1"`: -10,
|
||||
`"-1e+1"`: -10,
|
||||
`"-1e-1"`: -.1,
|
||||
`"-1E1"`: -10,
|
||||
`"-1E+1"`: -10,
|
||||
`"-1E-1"`: -.1,
|
||||
|
||||
"123": 123,
|
||||
`"123true"`: 123,
|
||||
`"+"`: 0,
|
||||
`"-"`: 0,
|
||||
|
||||
`"-123true"`: -123,
|
||||
`"-99.9true"`: -99.9,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
"-1": -1,
|
||||
|
||||
"1.1": 1.1,
|
||||
"0.0": 0,
|
||||
"-1.1": -1.1,
|
||||
`"+1.1"`: 1.1,
|
||||
`""`: 0,
|
||||
"[1,2]": 1,
|
||||
"[]": 0,
|
||||
"{}": 0,
|
||||
`{"abc":1}`: 0,
|
||||
}
|
||||
|
||||
func Test_read_any_to_float(t *testing.T) {
|
||||
should := require.New(t)
|
||||
for k, v := range floatConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(float64(v), any.ToFloat64(), "the original val is "+k)
|
||||
}
|
||||
|
||||
for k, v := range floatConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(float32(v), any.ToFloat32(), "the original val is "+k)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_float_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapFloat64(12.3)
|
||||
anyFloat64 := float64(12.3)
|
||||
//negaAnyFloat64 := float64(-1.1)
|
||||
any2 := jsoniter.WrapFloat64(-1.1)
|
||||
should.Equal(float64(12.3), any.ToFloat64())
|
||||
//should.Equal("12.3", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(float32(anyFloat64), any.ToFloat32())
|
||||
should.Equal(int(anyFloat64), any.ToInt())
|
||||
should.Equal(int32(anyFloat64), any.ToInt32())
|
||||
should.Equal(int64(anyFloat64), any.ToInt64())
|
||||
should.Equal(uint(anyFloat64), any.ToUint())
|
||||
should.Equal(uint32(anyFloat64), any.ToUint32())
|
||||
should.Equal(uint64(anyFloat64), any.ToUint64())
|
||||
should.Equal(uint(0), any2.ToUint())
|
||||
should.Equal(uint32(0), any2.ToUint32())
|
||||
should.Equal(uint64(0), any2.ToUint64())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
|
||||
should.Equal("1.23E+01", any.ToString())
|
||||
}
|
198
any_tests/jsoniter_any_int_test.go
Normal file
198
any_tests/jsoniter_any_int_test.go
Normal file
@ -0,0 +1,198 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var intConvertMap = map[string]int{
|
||||
"null": 0,
|
||||
"321.1": 321,
|
||||
"-321.1": -321,
|
||||
`"1.1"`: 1,
|
||||
`"-321.1"`: -321,
|
||||
"0.0": 0,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
`"0.0"`: 0,
|
||||
"-1.1": -1,
|
||||
"true": 1,
|
||||
"false": 0,
|
||||
`"true"`: 0,
|
||||
`"false"`: 0,
|
||||
`"true123"`: 0,
|
||||
`"123true"`: 123,
|
||||
`"-123true"`: -123,
|
||||
`"1.2332e6"`: 1,
|
||||
`""`: 0,
|
||||
"+": 0,
|
||||
"-": 0,
|
||||
"[]": 0,
|
||||
"[1,2]": 1,
|
||||
`["1","2"]`: 1,
|
||||
// object in php cannot convert to int
|
||||
"{}": 0,
|
||||
}
|
||||
|
||||
func Test_read_any_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
|
||||
// int
|
||||
for k, v := range intConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
// int32
|
||||
for k, v := range intConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k))
|
||||
}
|
||||
|
||||
// int64
|
||||
for k, v := range intConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var uintConvertMap = map[string]int{
|
||||
"null": 0,
|
||||
"321.1": 321,
|
||||
`"1.1"`: 1,
|
||||
`"-123.1"`: 0,
|
||||
"0.0": 0,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
`"0.0"`: 0,
|
||||
`"00.0"`: 0,
|
||||
"true": 1,
|
||||
"false": 0,
|
||||
`"true"`: 0,
|
||||
`"false"`: 0,
|
||||
`"true123"`: 0,
|
||||
`"+1"`: 1,
|
||||
`"123true"`: 123,
|
||||
`"-123true"`: 0,
|
||||
`"1.2332e6"`: 1,
|
||||
`""`: 0,
|
||||
"+": 0,
|
||||
"-": 0,
|
||||
".": 0,
|
||||
"[]": 0,
|
||||
"[1,2]": 1,
|
||||
"{}": 0,
|
||||
"{1,2}": 0,
|
||||
"-1.1": 0,
|
||||
"-321.1": 0,
|
||||
}
|
||||
|
||||
func Test_read_any_to_uint(t *testing.T) {
|
||||
should := require.New(t)
|
||||
|
||||
for k, v := range uintConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
for k, v := range uintConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
for k, v := range uintConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_read_int64_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapInt64(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
}
|
||||
func Test_read_int32_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapInt32(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
}
|
||||
|
||||
func Test_read_uint32_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapUint32(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
}
|
||||
|
||||
func Test_read_uint64_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.WrapUint64(12345)
|
||||
should.Equal(12345, any.ToInt())
|
||||
should.Equal(int32(12345), any.ToInt32())
|
||||
should.Equal(int64(12345), any.ToInt64())
|
||||
should.Equal(uint(12345), any.ToUint())
|
||||
should.Equal(uint32(12345), any.ToUint32())
|
||||
should.Equal(uint64(12345), any.ToUint64())
|
||||
should.Equal(float32(12345), any.ToFloat32())
|
||||
should.Equal(float64(12345), any.ToFloat64())
|
||||
should.Equal("12345", any.ToString())
|
||||
should.Equal(true, any.ToBool())
|
||||
should.Equal(any.ValueType(), jsoniter.NumberValue)
|
||||
stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
any.WriteTo(stream)
|
||||
should.Equal("12345", string(stream.Buffer()))
|
||||
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
|
||||
stream.WriteUint(uint(123))
|
||||
should.Equal("123", string(stream.Buffer()))
|
||||
}
|
||||
|
||||
func Test_int_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("1234"))
|
||||
// panic!!
|
||||
//should.Equal(any.LastError(), io.EOF)
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(1, "2").ValueType())
|
||||
}
|
15
any_tests/jsoniter_any_map_test.go
Normal file
15
any_tests/jsoniter_any_map_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_wrap_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap(map[string]string{"Field1": "hello"})
|
||||
should.Equal("hello", any.Get("Field1").ToString())
|
||||
any = jsoniter.Wrap(map[string]string{"Field1": "hello"})
|
||||
should.Equal(1, any.Size())
|
||||
}
|
16
any_tests/jsoniter_any_null_test.go
Normal file
16
any_tests/jsoniter_any_null_test.go
Normal file
@ -0,0 +1,16 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_read_null_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`null`))
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
should.Equal("", any.ToString())
|
||||
should.False(any.ToBool())
|
||||
}
|
108
any_tests/jsoniter_any_object_test.go
Normal file
108
any_tests/jsoniter_any_object_test.go
Normal file
@ -0,0 +1,108 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_read_object_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{"a":"stream","c":"d"}`))
|
||||
should.Equal(`{"a":"stream","c":"d"}`, any.ToString())
|
||||
// partial parse
|
||||
should.Equal("stream", any.Get("a").ToString())
|
||||
should.Equal("d", any.Get("c").ToString())
|
||||
should.Equal(2, len(any.Keys()))
|
||||
any = jsoniter.Get([]byte(`{"a":"stream","c":"d"}`))
|
||||
// full parse
|
||||
should.Equal(2, len(any.Keys()))
|
||||
should.Equal(2, any.Size())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(jsoniter.ObjectValue, any.ValueType())
|
||||
should.Nil(any.LastError())
|
||||
obj := struct {
|
||||
A string
|
||||
}{}
|
||||
any.ToVal(&obj)
|
||||
should.Equal("stream", obj.A)
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{"a":{"stream":{"c":"d"}}}`))
|
||||
should.Equal("d", any.Get("a", "stream", "c").ToString())
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{"a":[0],"stream":[1]}`))
|
||||
should.Contains(any.Get('*', 0).ToString(), `"a":0`)
|
||||
}
|
||||
|
||||
func Test_object_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`{}`))
|
||||
should.Equal(jsoniter.InvalidValue, any.Get("a", "stream", "c").ValueType())
|
||||
should.Equal(jsoniter.InvalidValue, any.Get(1).ValueType())
|
||||
}
|
||||
|
||||
func Test_wrap_map_and_convert_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Wrap(map[string]interface{}{"a": 1})
|
||||
should.True(any.ToBool())
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(int32(0), any.ToInt32())
|
||||
should.Equal(int64(0), any.ToInt64())
|
||||
should.Equal(float32(0), any.ToFloat32())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
should.Equal(uint(0), any.ToUint())
|
||||
should.Equal(uint32(0), any.ToUint32())
|
||||
should.Equal(uint64(0), any.ToUint64())
|
||||
}
|
||||
|
||||
func Test_wrap_object_and_convert_to_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 string
|
||||
field2 string
|
||||
}
|
||||
any := jsoniter.Wrap(TestObject{"hello", "world"})
|
||||
should.Equal("hello", any.Get("Field1").ToString())
|
||||
any = jsoniter.Wrap(TestObject{"hello", "world"})
|
||||
should.Equal(2, any.Size())
|
||||
should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString())
|
||||
|
||||
should.Equal(0, any.ToInt())
|
||||
should.Equal(int32(0), any.ToInt32())
|
||||
should.Equal(int64(0), any.ToInt64())
|
||||
should.Equal(float32(0), any.ToFloat32())
|
||||
should.Equal(float64(0), any.ToFloat64())
|
||||
should.Equal(uint(0), any.ToUint())
|
||||
should.Equal(uint32(0), any.ToUint32())
|
||||
should.Equal(uint64(0), any.ToUint64())
|
||||
should.True(any.ToBool())
|
||||
should.Equal(`{"Field1":"hello"}`, any.ToString())
|
||||
|
||||
// cannot pass!
|
||||
//stream := NewStream(ConfigDefault, nil, 32)
|
||||
//any.WriteTo(stream)
|
||||
//should.Equal(`{"Field1":"hello"}`, string(stream.Buffer()))
|
||||
// cannot pass!
|
||||
|
||||
}
|
||||
|
||||
func Test_any_within_struct(t *testing.T) {
|
||||
should := require.New(t)
|
||||
type TestObject struct {
|
||||
Field1 jsoniter.Any
|
||||
Field2 jsoniter.Any
|
||||
}
|
||||
obj := TestObject{}
|
||||
err := jsoniter.UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj)
|
||||
should.Nil(err)
|
||||
should.Equal("hello", obj.Field1.ToString())
|
||||
should.Equal("[1,2,3]", obj.Field2.ToString())
|
||||
}
|
58
any_tests/jsoniter_any_string_test.go
Normal file
58
any_tests/jsoniter_any_string_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package any_tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var stringConvertMap = map[string]string{
|
||||
"null": "",
|
||||
"321.1": "321.1",
|
||||
`"1.1"`: "1.1",
|
||||
`"-123.1"`: "-123.1",
|
||||
"0.0": "0.0",
|
||||
"0": "0",
|
||||
`"0"`: "0",
|
||||
`"0.0"`: "0.0",
|
||||
`"00.0"`: "00.0",
|
||||
"true": "true",
|
||||
"false": "false",
|
||||
`"true"`: "true",
|
||||
`"false"`: "false",
|
||||
`"true123"`: "true123",
|
||||
`"+1"`: "+1",
|
||||
"[]": "[]",
|
||||
"[1,2]": "[1,2]",
|
||||
"{}": "{}",
|
||||
`{"a":1, "stream":true}`: `{"a":1, "stream":true}`,
|
||||
}
|
||||
|
||||
func Test_read_any_to_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
for k, v := range stringConvertMap {
|
||||
any := jsoniter.Get([]byte(k))
|
||||
should.Equal(v, any.ToString(), "original val "+k)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_read_string_as_any(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte(`"hello"`))
|
||||
should.Equal("hello", any.ToString())
|
||||
should.True(any.ToBool())
|
||||
any = jsoniter.Get([]byte(`" "`))
|
||||
should.False(any.ToBool())
|
||||
any = jsoniter.Get([]byte(`"false"`))
|
||||
should.True(any.ToBool())
|
||||
any = jsoniter.Get([]byte(`"123"`))
|
||||
should.Equal(123, any.ToInt())
|
||||
}
|
||||
|
||||
func Test_wrap_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any := jsoniter.Get([]byte("-32000")).MustBeValid()
|
||||
should.Equal(-32000, any.ToInt())
|
||||
should.NoError(any.LastError())
|
||||
}
|
Reference in New Issue
Block a user