jsoniter_object_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/json-iterator/go/require"
  7. )
  8. func Test_empty_object(t *testing.T) {
  9. should := require.New(t)
  10. iter := ParseString(ConfigDefault, `{}`)
  11. field := iter.ReadObject()
  12. should.Equal("", field)
  13. iter = ParseString(ConfigDefault, `{}`)
  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(ConfigDefault, `{"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(ConfigDefault, `{"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(ConfigDefault, `{ "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(ConfigDefault, `{"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_object_wrapper_any_get_all(t *testing.T) {
  60. should := require.New(t)
  61. type TestObject struct {
  62. Field1 []int
  63. Field2 []int
  64. }
  65. any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
  66. should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
  67. should.Contains(any.Keys(), "Field1")
  68. should.Contains(any.Keys(), "Field2")
  69. should.NotContains(any.Keys(), "Field3")
  70. //should.Contains(any.GetObject()["Field1"].GetArray()[0], 1)
  71. }
  72. func Test_write_object(t *testing.T) {
  73. should := require.New(t)
  74. buf := &bytes.Buffer{}
  75. stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
  76. stream.WriteObjectStart()
  77. stream.WriteObjectField("hello")
  78. stream.WriteInt(1)
  79. stream.WriteMore()
  80. stream.WriteObjectField("world")
  81. stream.WriteInt(2)
  82. stream.WriteObjectEnd()
  83. stream.Flush()
  84. should.Nil(stream.Error)
  85. should.Equal("{\n \"hello\": 1,\n \"world\": 2\n}", buf.String())
  86. }
  87. func Test_decode_one_field_struct(t *testing.T) {
  88. should := require.New(t)
  89. type TestObject struct {
  90. Field1 string
  91. }
  92. obj := TestObject{}
  93. should.Nil(UnmarshalFromString(`{}`, &obj))
  94. should.Equal("", obj.Field1)
  95. should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
  96. should.Equal("hello", obj.Field1)
  97. }
  98. func Test_decode_two_fields_struct(t *testing.T) {
  99. should := require.New(t)
  100. type TestObject struct {
  101. Field1 string
  102. Field2 string
  103. }
  104. obj := TestObject{}
  105. should.Nil(UnmarshalFromString(`{}`, &obj))
  106. should.Equal("", obj.Field1)
  107. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b"}`, &obj))
  108. should.Equal("a", obj.Field1)
  109. should.Equal("b", obj.Field2)
  110. }
  111. func Test_decode_three_fields_struct(t *testing.T) {
  112. should := require.New(t)
  113. type TestObject struct {
  114. Field1 string
  115. Field2 string
  116. Field3 string
  117. }
  118. obj := TestObject{}
  119. should.Nil(UnmarshalFromString(`{}`, &obj))
  120. should.Equal("", obj.Field1)
  121. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c"}`, &obj))
  122. should.Equal("a", obj.Field1)
  123. should.Equal("b", obj.Field2)
  124. should.Equal("c", obj.Field3)
  125. }
  126. func Test_decode_four_fields_struct(t *testing.T) {
  127. should := require.New(t)
  128. type TestObject struct {
  129. Field1 string
  130. Field2 string
  131. Field3 string
  132. Field4 string
  133. }
  134. obj := TestObject{}
  135. should.Nil(UnmarshalFromString(`{}`, &obj))
  136. should.Equal("", obj.Field1)
  137. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d"}`, &obj))
  138. should.Equal("a", obj.Field1)
  139. should.Equal("b", obj.Field2)
  140. should.Equal("c", obj.Field3)
  141. should.Equal("d", obj.Field4)
  142. }
  143. func Test_decode_five_fields_struct(t *testing.T) {
  144. should := require.New(t)
  145. type TestObject struct {
  146. Field1 string
  147. Field2 string
  148. Field3 string
  149. Field4 string
  150. Field5 string
  151. }
  152. obj := TestObject{}
  153. should.Nil(UnmarshalFromString(`{}`, &obj))
  154. should.Equal("", obj.Field1)
  155. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
  156. should.Equal("a", obj.Field1)
  157. should.Equal("b", obj.Field2)
  158. should.Equal("c", obj.Field3)
  159. should.Equal("d", obj.Field4)
  160. should.Equal("e", obj.Field5)
  161. }
  162. func Test_decode_ten_fields_struct(t *testing.T) {
  163. should := require.New(t)
  164. type TestObject struct {
  165. Field1 string
  166. Field2 string
  167. Field3 string
  168. Field4 string
  169. Field5 string
  170. Field6 string
  171. Field7 string
  172. Field8 string
  173. Field9 string
  174. Field10 string
  175. }
  176. obj := TestObject{}
  177. should.Nil(UnmarshalFromString(`{}`, &obj))
  178. should.Equal("", obj.Field1)
  179. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
  180. should.Equal("a", obj.Field1)
  181. should.Equal("b", obj.Field2)
  182. should.Equal("c", obj.Field3)
  183. should.Equal("d", obj.Field4)
  184. should.Equal("e", obj.Field5)
  185. }
  186. func Test_decode_struct_field_with_tag(t *testing.T) {
  187. should := require.New(t)
  188. type TestObject struct {
  189. Field1 string `json:"field-1"`
  190. Field2 string `json:"-"`
  191. Field3 int `json:",string"`
  192. }
  193. obj := TestObject{Field2: "world"}
  194. UnmarshalFromString(`{"field-1": "hello", "field2": "", "Field3": "100"}`, &obj)
  195. should.Equal("hello", obj.Field1)
  196. should.Equal("world", obj.Field2)
  197. should.Equal(100, obj.Field3)
  198. }
  199. func Test_decode_struct_field_with_tag_string(t *testing.T) {
  200. should := require.New(t)
  201. type TestObject struct {
  202. Field1 int `json:",string"`
  203. }
  204. obj := TestObject{Field1: 100}
  205. should.Nil(UnmarshalFromString(`{"Field1": "100"}`, &obj))
  206. should.Equal(100, obj.Field1)
  207. }
  208. func Test_write_val_zero_field_struct(t *testing.T) {
  209. should := require.New(t)
  210. type TestObject struct {
  211. }
  212. obj := TestObject{}
  213. str, err := MarshalToString(obj)
  214. should.Nil(err)
  215. should.Equal(`{}`, str)
  216. }
  217. func Test_write_val_one_field_struct(t *testing.T) {
  218. should := require.New(t)
  219. type TestObject struct {
  220. Field1 string `json:"field-1"`
  221. }
  222. obj := TestObject{"hello"}
  223. str, err := MarshalToString(obj)
  224. should.Nil(err)
  225. should.Equal(`{"field-1":"hello"}`, str)
  226. }
  227. func Test_mixed(t *testing.T) {
  228. should := require.New(t)
  229. type AA struct {
  230. ID int `json:"id"`
  231. Payload map[string]interface{} `json:"payload"`
  232. buf *bytes.Buffer `json:"-"`
  233. }
  234. aa := AA{}
  235. err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
  236. should.Nil(err)
  237. should.Equal(1, aa.ID)
  238. should.Equal("123", aa.Payload["account"])
  239. }
  240. func Test_omit_empty(t *testing.T) {
  241. should := require.New(t)
  242. type TestObject struct {
  243. Field1 string `json:"field-1,omitempty"`
  244. Field2 string `json:"field-2,omitempty"`
  245. Field3 string `json:"field-3,omitempty"`
  246. }
  247. obj := TestObject{}
  248. obj.Field2 = "hello"
  249. str, err := MarshalToString(&obj)
  250. should.Nil(err)
  251. should.Equal(`{"field-2":"hello"}`, str)
  252. }
  253. func Test_recursive_struct(t *testing.T) {
  254. should := require.New(t)
  255. type TestObject struct {
  256. Field1 string
  257. Me *TestObject
  258. }
  259. obj := TestObject{}
  260. str, err := MarshalToString(obj)
  261. should.Nil(err)
  262. should.Contains(str, `"Field1":""`)
  263. should.Contains(str, `"Me":null`)
  264. err = UnmarshalFromString(str, &obj)
  265. should.Nil(err)
  266. }
  267. func Test_encode_anonymous_struct(t *testing.T) {
  268. should := require.New(t)
  269. type TestObject struct {
  270. Field string
  271. }
  272. str, err := MarshalToString(struct {
  273. TestObject
  274. Field int
  275. }{
  276. Field: 100,
  277. })
  278. should.Nil(err)
  279. should.Equal(`{"Field":100}`, str)
  280. }
  281. func Test_decode_anonymous_struct(t *testing.T) {
  282. should := require.New(t)
  283. type Inner struct {
  284. Key string `json:"key"`
  285. }
  286. type Outer struct {
  287. Inner
  288. }
  289. var outer Outer
  290. j := []byte("{\"key\":\"value\"}")
  291. should.Nil(Unmarshal(j, &outer))
  292. should.Equal("value", outer.Key)
  293. }
  294. func Test_multiple_level_anonymous_struct(t *testing.T) {
  295. type Level1 struct {
  296. Field1 string
  297. }
  298. type Level2 struct {
  299. Level1
  300. Field2 string
  301. }
  302. type Level3 struct {
  303. Level2
  304. Field3 string
  305. }
  306. should := require.New(t)
  307. obj := Level3{Level2{Level1{"1"}, "2"}, "3"}
  308. output, err := MarshalToString(obj)
  309. should.Nil(err)
  310. should.Equal(`{"Field1":"1","Field2":"2","Field3":"3"}`, output)
  311. }
  312. func Test_multiple_level_anonymous_struct_with_ptr(t *testing.T) {
  313. type Level1 struct {
  314. Field1 string
  315. Field2 string
  316. Field4 string
  317. }
  318. type Level2 struct {
  319. *Level1
  320. Field2 string
  321. Field3 string
  322. }
  323. type Level3 struct {
  324. *Level2
  325. Field3 string
  326. }
  327. should := require.New(t)
  328. obj := Level3{&Level2{&Level1{"1", "", "4"}, "2", ""}, "3"}
  329. output, err := MarshalToString(obj)
  330. should.Nil(err)
  331. should.Contains(output, `"Field1":"1"`)
  332. should.Contains(output, `"Field2":"2"`)
  333. should.Contains(output, `"Field3":"3"`)
  334. should.Contains(output, `"Field4":"4"`)
  335. }
  336. func Test_shadow_struct_field(t *testing.T) {
  337. should := require.New(t)
  338. type omit *struct{}
  339. type CacheItem struct {
  340. Key string `json:"key"`
  341. MaxAge int `json:"cacheAge"`
  342. }
  343. output, err := MarshalToString(struct {
  344. *CacheItem
  345. // Omit bad keys
  346. OmitMaxAge omit `json:"cacheAge,omitempty"`
  347. // Add nice keys
  348. MaxAge int `json:"max_age"`
  349. }{
  350. CacheItem: &CacheItem{
  351. Key: "value",
  352. MaxAge: 100,
  353. },
  354. MaxAge: 20,
  355. })
  356. should.Nil(err)
  357. should.Contains(output, `"key":"value"`)
  358. should.Contains(output, `"max_age":20`)
  359. }
  360. func Test_embedded_order(t *testing.T) {
  361. type A struct {
  362. Field2 string
  363. }
  364. type C struct {
  365. Field5 string
  366. }
  367. type B struct {
  368. Field4 string
  369. C
  370. Field6 string
  371. }
  372. type TestObject struct {
  373. Field1 string
  374. A
  375. Field3 string
  376. B
  377. Field7 string
  378. }
  379. should := require.New(t)
  380. s := TestObject{}
  381. output, err := MarshalToString(s)
  382. should.Nil(err)
  383. should.Equal(`{"Field1":"","Field2":"","Field3":"","Field4":"","Field5":"","Field6":"","Field7":""}`, output)
  384. }
  385. func Test_decode_nested(t *testing.T) {
  386. type StructOfString struct {
  387. Field1 string
  388. Field2 string
  389. }
  390. iter := ParseString(ConfigDefault, `[{"field1": "hello"}, null, {"field2": "world"}]`)
  391. slice := []*StructOfString{}
  392. iter.ReadVal(&slice)
  393. if len(slice) != 3 {
  394. fmt.Println(iter.Error)
  395. t.Fatal(len(slice))
  396. }
  397. if slice[0].Field1 != "hello" {
  398. fmt.Println(iter.Error)
  399. t.Fatal(slice[0])
  400. }
  401. if slice[1] != nil {
  402. fmt.Println(iter.Error)
  403. t.Fatal(slice[1])
  404. }
  405. if slice[2].Field2 != "world" {
  406. fmt.Println(iter.Error)
  407. t.Fatal(slice[2])
  408. }
  409. }