jsoniter_object_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/json-iterator/go/require"
  6. "testing"
  7. )
  8. func Test_empty_object(t *testing.T) {
  9. should := require.New(t)
  10. iter := ParseString(`{}`)
  11. field := iter.ReadObject()
  12. should.Equal("", field)
  13. iter = ParseString(`{}`)
  14. iter.ReadObjectCB(func(iter *Iterator, field string) bool {
  15. should.FailNow("should not call")
  16. return true
  17. })
  18. }
  19. func Test_one_field(t *testing.T) {
  20. should := require.New(t)
  21. iter := ParseString(`{"a": "b"}`)
  22. field := iter.ReadObject()
  23. should.Equal("a", field)
  24. value := iter.ReadString()
  25. should.Equal("b", value)
  26. field = iter.ReadObject()
  27. should.Equal("", field)
  28. iter = ParseString(`{"a": "b"}`)
  29. should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
  30. should.Equal("a", field)
  31. return true
  32. }))
  33. }
  34. func Test_two_field(t *testing.T) {
  35. should := require.New(t)
  36. iter := ParseString(`{ "a": "b" , "c": "d" }`)
  37. field := iter.ReadObject()
  38. should.Equal("a", field)
  39. value := iter.ReadString()
  40. should.Equal("b", value)
  41. field = iter.ReadObject()
  42. should.Equal("c", field)
  43. value = iter.ReadString()
  44. should.Equal("d", value)
  45. field = iter.ReadObject()
  46. should.Equal("", field)
  47. iter = ParseString(`{"field1": "1", "field2": 2}`)
  48. for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  49. switch field {
  50. case "field1":
  51. iter.ReadString()
  52. case "field2":
  53. iter.ReadInt64()
  54. default:
  55. iter.reportError("bind object", "unexpected field")
  56. }
  57. }
  58. }
  59. func Test_read_object_as_any(t *testing.T) {
  60. should := require.New(t)
  61. any, err := UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
  62. should.Nil(err)
  63. should.Equal(`{"a":"b","c":"d"}`, any.ToString())
  64. // partial parse
  65. should.Equal("b", any.Get("a").ToString())
  66. should.Equal("d", any.Get("c").ToString())
  67. should.Equal(2, len(any.Keys()))
  68. any, err = UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
  69. // full parse
  70. should.Equal(2, len(any.Keys()))
  71. should.Equal(2, any.Size())
  72. should.True(any.ToBool())
  73. should.Equal(1, any.ToInt())
  74. }
  75. func Test_object_any_lazy_iterator(t *testing.T) {
  76. should := require.New(t)
  77. any, err := UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
  78. should.Nil(err)
  79. // iterator parse
  80. vals := map[string]string{}
  81. var k string
  82. var v Any
  83. next, hasNext := any.IterateObject()
  84. should.True(hasNext)
  85. k, v, hasNext = next()
  86. should.True(hasNext)
  87. vals[k] = v.ToString()
  88. // trigger full parse
  89. should.Equal(2, len(any.Keys()))
  90. k, v, hasNext = next()
  91. should.False(hasNext)
  92. vals[k] = v.ToString()
  93. should.Equal(map[string]string{"a": "b", "c": "d"}, vals)
  94. vals = map[string]string{}
  95. for next, hasNext := any.IterateObject(); hasNext; {
  96. k, v, hasNext = next()
  97. if v.ValueType() == String {
  98. vals[k] = v.ToString()
  99. }
  100. }
  101. should.Equal(map[string]string{"a": "b", "c": "d"}, vals)
  102. }
  103. func Test_object_any_with_two_lazy_iterators(t *testing.T) {
  104. should := require.New(t)
  105. any, err := UnmarshalAnyFromString(`{"a":"b","c":"d","e":"f"}`)
  106. should.Nil(err)
  107. var k string
  108. var v Any
  109. next1, hasNext1 := any.IterateObject()
  110. next2, hasNext2 := any.IterateObject()
  111. should.True(hasNext1)
  112. k, v, hasNext1 = next1()
  113. should.True(hasNext1)
  114. should.Equal("a", k)
  115. should.Equal("b", v.ToString())
  116. should.True(hasNext2)
  117. k, v, hasNext2 = next2()
  118. should.True(hasNext2)
  119. should.Equal("a", k)
  120. should.Equal("b", v.ToString())
  121. k, v, hasNext1 = next1()
  122. should.True(hasNext1)
  123. should.Equal("c", k)
  124. should.Equal("d", v.ToString())
  125. k, v, hasNext2 = next2()
  126. should.True(hasNext2)
  127. should.Equal("c", k)
  128. should.Equal("d", v.ToString())
  129. }
  130. func Test_object_lazy_any_get(t *testing.T) {
  131. should := require.New(t)
  132. any, err := UnmarshalAnyFromString(`{"a":{"b":{"c":"d"}}}`)
  133. should.Nil(err)
  134. should.Equal("d", any.Get("a", "b", "c").ToString())
  135. }
  136. func Test_object_lazy_any_get_all(t *testing.T) {
  137. should := require.New(t)
  138. any, err := UnmarshalAnyFromString(`{"a":[0],"b":[1]}`)
  139. should.Nil(err)
  140. should.Contains(any.Get('*', 0).ToString(), `"a":0`)
  141. }
  142. func Test_object_lazy_any_get_invalid(t *testing.T) {
  143. should := require.New(t)
  144. any, err := UnmarshalAnyFromString(`{}`)
  145. should.Nil(err)
  146. should.Equal(Invalid, any.Get("a", "b", "c").ValueType())
  147. should.Equal(Invalid, any.Get(1).ValueType())
  148. }
  149. func Test_object_lazy_any_set(t *testing.T) {
  150. should := require.New(t)
  151. any, err := UnmarshalAnyFromString(`{"a":{"b":{"c":"d"}}}`)
  152. should.Nil(err)
  153. any.GetObject()["a"] = WrapInt64(1)
  154. str, err := MarshalToString(any)
  155. should.Nil(err)
  156. should.Equal(`{"a":1}`, str)
  157. }
  158. func Test_wrap_object(t *testing.T) {
  159. should := require.New(t)
  160. type TestObject struct {
  161. Field1 string
  162. field2 string
  163. }
  164. any := Wrap(TestObject{"hello", "world"})
  165. should.Equal("hello", any.Get("Field1").ToString())
  166. any = Wrap(TestObject{"hello", "world"})
  167. should.Equal(2, any.Size())
  168. any = Wrap(TestObject{"hello", "world"})
  169. vals := map[string]string{}
  170. var k string
  171. var v Any
  172. for next, hasNext := any.IterateObject(); hasNext; {
  173. k, v, hasNext = next()
  174. if v.ValueType() == String {
  175. vals[k] = v.ToString()
  176. }
  177. }
  178. should.Equal(map[string]string{"Field1": "hello"}, vals)
  179. }
  180. func Test_object_wrapper_any_get_all(t *testing.T) {
  181. should := require.New(t)
  182. type TestObject struct {
  183. Field1 []int
  184. Field2 []int
  185. }
  186. any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
  187. should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
  188. }
  189. func Test_write_object(t *testing.T) {
  190. should := require.New(t)
  191. buf := &bytes.Buffer{}
  192. stream := NewStream(buf, 4096)
  193. stream.IndentionStep = 2
  194. stream.WriteObjectStart()
  195. stream.WriteObjectField("hello")
  196. stream.WriteInt(1)
  197. stream.WriteMore()
  198. stream.WriteObjectField("world")
  199. stream.WriteInt(2)
  200. stream.WriteObjectEnd()
  201. stream.Flush()
  202. should.Nil(stream.Error)
  203. should.Equal("{\n \"hello\":1,\n \"world\":2\n}", buf.String())
  204. }
  205. func Benchmark_jsoniter_object(b *testing.B) {
  206. type TestObj struct {
  207. Field1 string
  208. Field2 uint64
  209. }
  210. for n := 0; n < b.N; n++ {
  211. iter := ParseString(`{"field1": "1", "field2": 2}`)
  212. obj := TestObj{}
  213. for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  214. switch field {
  215. case "field1":
  216. obj.Field1 = iter.ReadString()
  217. case "field2":
  218. obj.Field2 = iter.ReadUint64()
  219. default:
  220. iter.reportError("bind object", "unexpected field")
  221. }
  222. }
  223. }
  224. }
  225. func Benchmark_json_object(b *testing.B) {
  226. type TestObj struct {
  227. Field1 string
  228. Field2 uint64
  229. }
  230. for n := 0; n < b.N; n++ {
  231. result := TestObj{}
  232. json.Unmarshal([]byte(`{"field1": "1", "field2": 2}`), &result)
  233. }
  234. }