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