jsoniter_any_object_test.go 3.0 KB

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