jsoniter_any_object_test.go 3.5 KB

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