| 1 |
package any_tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/json-iterator/go" |
| 7 |
"github.com/stretchr/testify/require" |
| 8 |
) |
| 9 |
|
| 10 |
func Test_read_object_as_any(t *testing.T) { |
| 11 |
should := require.New(t) |
| 12 |
any := jsoniter.Get([]byte(`{"a":"stream","c":"d"}`)) |
| 13 |
should.Equal(`{"a":"stream","c":"d"}`, any.ToString()) |
| 14 |
// partial parse |
| 15 |
should.Equal("stream", any.Get("a").ToString()) |
| 16 |
should.Equal("d", any.Get("c").ToString()) |
| 17 |
should.Equal(2, len(any.Keys())) |
| 18 |
any = jsoniter.Get([]byte(`{"a":"stream","c":"d"}`)) |
| 19 |
// full parse |
| 20 |
should.Equal(2, len(any.Keys())) |
| 21 |
should.Equal(2, any.Size()) |
| 22 |
should.True(any.ToBool()) |
| 23 |
should.Equal(0, any.ToInt()) |
| 24 |
should.Equal(jsoniter.ObjectValue, any.ValueType()) |
| 25 |
should.Nil(any.LastError()) |
| 26 |
obj := struct { |
| 27 |
A string |
| 28 |
}{} |
| 29 |
any.ToVal(&obj) |
| 30 |
should.Equal("stream", obj.A) |
| 31 |
} |
| 32 |
|
| 33 |
func Test_object_lazy_any_get(t *testing.T) { |
| 34 |
should := require.New(t) |
| 35 |
any := jsoniter.Get([]byte(`{"a":{"stream":{"c":"d"}}}`)) |
| 36 |
should.Equal("d", any.Get("a", "stream", "c").ToString()) |
| 37 |
} |
| 38 |
|
| 39 |
func Test_object_lazy_any_get_all(t *testing.T) { |
| 40 |
should := require.New(t) |
| 41 |
any := jsoniter.Get([]byte(`{"a":[0],"stream":[1]}`)) |
| 42 |
should.Contains(any.Get('*', 0).ToString(), `"a":0`) |
| 43 |
} |
| 44 |
|
| 45 |
func Test_object_lazy_any_get_invalid(t *testing.T) { |
| 46 |
should := require.New(t) |
| 47 |
any := jsoniter.Get([]byte(`{}`)) |
| 48 |
should.Equal(jsoniter.InvalidValue, any.Get("a", "stream", "c").ValueType()) |
| 49 |
should.Equal(jsoniter.InvalidValue, any.Get(1).ValueType()) |
| 50 |
} |
| 51 |
|
| 52 |
func Test_wrap_map_and_convert_to_any(t *testing.T) { |
| 53 |
should := require.New(t) |
| 54 |
any := jsoniter.Wrap(map[string]interface{}{"a": 1}) |
| 55 |
should.True(any.ToBool()) |
| 56 |
should.Equal(0, any.ToInt()) |
| 57 |
should.Equal(int32(0), any.ToInt32()) |
| 58 |
should.Equal(int64(0), any.ToInt64()) |
| 59 |
should.Equal(float32(0), any.ToFloat32()) |
| 60 |
should.Equal(float64(0), any.ToFloat64()) |
| 61 |
should.Equal(uint(0), any.ToUint()) |
| 62 |
should.Equal(uint32(0), any.ToUint32()) |
| 63 |
should.Equal(uint64(0), any.ToUint64()) |
| 64 |
} |
| 65 |
|
| 66 |
func Test_wrap_object_and_convert_to_any(t *testing.T) { |
| 67 |
should := require.New(t) |
| 68 |
type TestObject struct { |
| 69 |
Field1 string |
| 70 |
field2 string |
| 71 |
} |
| 72 |
any := jsoniter.Wrap(TestObject{"hello", "world"}) |
| 73 |
should.Equal("hello", any.Get("Field1").ToString()) |
| 74 |
any = jsoniter.Wrap(TestObject{"hello", "world"}) |
| 75 |
should.Equal(2, any.Size()) |
| 76 |
should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString()) |
| 77 |
|
| 78 |
should.Equal(0, any.ToInt()) |
| 79 |
should.Equal(int32(0), any.ToInt32()) |
| 80 |
should.Equal(int64(0), any.ToInt64()) |
| 81 |
should.Equal(float32(0), any.ToFloat32()) |
| 82 |
should.Equal(float64(0), any.ToFloat64()) |
| 83 |
should.Equal(uint(0), any.ToUint()) |
| 84 |
should.Equal(uint32(0), any.ToUint32()) |
| 85 |
should.Equal(uint64(0), any.ToUint64()) |
| 86 |
should.True(any.ToBool()) |
| 87 |
should.Equal(`{"Field1":"hello"}`, any.ToString()) |
| 88 |
|
| 89 |
// cannot pass! |
| 90 |
//stream := NewStream(ConfigDefault, nil, 32) |
| 91 |
//any.WriteTo(stream) |
| 92 |
//should.Equal(`{"Field1":"hello"}`, string(stream.Buffer())) |
| 93 |
// cannot pass! |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
func Test_any_within_struct(t *testing.T) { |
| 98 |
should := require.New(t) |
| 99 |
type TestObject struct { |
| 100 |
Field1 jsoniter.Any |
| 101 |
Field2 jsoniter.Any |
| 102 |
} |
| 103 |
obj := TestObject{} |
| 104 |
err := jsoniter.UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj) |
| 105 |
should.Nil(err) |
| 106 |
should.Equal("hello", obj.Field1.ToString()) |
| 107 |
should.Equal("[1,2,3]", obj.Field2.ToString()) |
| 108 |
} |
| 109 |
|
| 110 |
func Test_object_wrapper_any_get_all(t *testing.T) { |
| 111 |
should := require.New(t) |
| 112 |
type TestObject struct { |
| 113 |
Field1 []int |
| 114 |
Field2 []int |
| 115 |
} |
| 116 |
any := jsoniter.Wrap(TestObject{[]int{1, 2}, []int{3, 4}}) |
| 117 |
should.Contains(any.Get('*', 0).ToString(), `"Field2":3`) |
| 118 |
should.Contains(any.Keys(), "Field1") |
| 119 |
should.Contains(any.Keys(), "Field2") |
| 120 |
should.NotContains(any.Keys(), "Field3") |
| 121 |
} |