jsoniter_object_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "github.com/json-iterator/go/require"
  5. "testing"
  6. "fmt"
  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. }
  68. func Test_write_object(t *testing.T) {
  69. should := require.New(t)
  70. buf := &bytes.Buffer{}
  71. stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
  72. stream.WriteObjectStart()
  73. stream.WriteObjectField("hello")
  74. stream.WriteInt(1)
  75. stream.WriteMore()
  76. stream.WriteObjectField("world")
  77. stream.WriteInt(2)
  78. stream.WriteObjectEnd()
  79. stream.Flush()
  80. should.Nil(stream.Error)
  81. should.Equal("{\n \"hello\":1,\n \"world\":2\n}", buf.String())
  82. }
  83. func Test_decode_one_field_struct(t *testing.T) {
  84. should := require.New(t)
  85. type TestObject struct {
  86. Field1 string
  87. }
  88. obj := TestObject{}
  89. should.Nil(UnmarshalFromString(`{}`, &obj))
  90. should.Equal("", obj.Field1)
  91. should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
  92. should.Equal("hello", obj.Field1)
  93. }
  94. func Test_decode_two_fields_struct(t *testing.T) {
  95. should := require.New(t)
  96. type TestObject struct {
  97. Field1 string
  98. Field2 string
  99. }
  100. obj := TestObject{}
  101. should.Nil(UnmarshalFromString(`{}`, &obj))
  102. should.Equal("", obj.Field1)
  103. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b"}`, &obj))
  104. should.Equal("a", obj.Field1)
  105. should.Equal("b", obj.Field2)
  106. }
  107. func Test_decode_three_fields_struct(t *testing.T) {
  108. should := require.New(t)
  109. type TestObject struct {
  110. Field1 string
  111. Field2 string
  112. Field3 string
  113. }
  114. obj := TestObject{}
  115. should.Nil(UnmarshalFromString(`{}`, &obj))
  116. should.Equal("", obj.Field1)
  117. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c"}`, &obj))
  118. should.Equal("a", obj.Field1)
  119. should.Equal("b", obj.Field2)
  120. should.Equal("c", obj.Field3)
  121. }
  122. func Test_decode_four_fields_struct(t *testing.T) {
  123. should := require.New(t)
  124. type TestObject struct {
  125. Field1 string
  126. Field2 string
  127. Field3 string
  128. Field4 string
  129. }
  130. obj := TestObject{}
  131. should.Nil(UnmarshalFromString(`{}`, &obj))
  132. should.Equal("", obj.Field1)
  133. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d"}`, &obj))
  134. should.Equal("a", obj.Field1)
  135. should.Equal("b", obj.Field2)
  136. should.Equal("c", obj.Field3)
  137. should.Equal("d", obj.Field4)
  138. }
  139. func Test_decode_five_fields_struct(t *testing.T) {
  140. should := require.New(t)
  141. type TestObject struct {
  142. Field1 string
  143. Field2 string
  144. Field3 string
  145. Field4 string
  146. Field5 string
  147. }
  148. obj := TestObject{}
  149. should.Nil(UnmarshalFromString(`{}`, &obj))
  150. should.Equal("", obj.Field1)
  151. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
  152. should.Equal("a", obj.Field1)
  153. should.Equal("b", obj.Field2)
  154. should.Equal("c", obj.Field3)
  155. should.Equal("d", obj.Field4)
  156. should.Equal("e", obj.Field5)
  157. }
  158. func Test_decode_ten_fields_struct(t *testing.T) {
  159. should := require.New(t)
  160. type TestObject struct {
  161. Field1 string
  162. Field2 string
  163. Field3 string
  164. Field4 string
  165. Field5 string
  166. Field6 string
  167. Field7 string
  168. Field8 string
  169. Field9 string
  170. Field10 string
  171. }
  172. obj := TestObject{}
  173. should.Nil(UnmarshalFromString(`{}`, &obj))
  174. should.Equal("", obj.Field1)
  175. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
  176. should.Equal("a", obj.Field1)
  177. should.Equal("b", obj.Field2)
  178. should.Equal("c", obj.Field3)
  179. should.Equal("d", obj.Field4)
  180. should.Equal("e", obj.Field5)
  181. }
  182. func Test_decode_struct_field_with_tag(t *testing.T) {
  183. should := require.New(t)
  184. type TestObject struct {
  185. Field1 string `json:"field-1"`
  186. Field2 string `json:"-"`
  187. Field3 int `json:",string"`
  188. }
  189. obj := TestObject{Field2: "world"}
  190. UnmarshalFromString(`{"field-1": "hello", "field2": "", "Field3": "100"}`, &obj)
  191. should.Equal("hello", obj.Field1)
  192. should.Equal("world", obj.Field2)
  193. should.Equal(100, obj.Field3)
  194. }
  195. func Test_write_val_zero_field_struct(t *testing.T) {
  196. should := require.New(t)
  197. type TestObject struct {
  198. }
  199. obj := TestObject{}
  200. str, err := MarshalToString(obj)
  201. should.Nil(err)
  202. should.Equal(`{}`, str)
  203. }
  204. func Test_write_val_one_field_struct(t *testing.T) {
  205. should := require.New(t)
  206. type TestObject struct {
  207. Field1 string `json:"field-1"`
  208. }
  209. obj := TestObject{"hello"}
  210. str, err := MarshalToString(obj)
  211. should.Nil(err)
  212. should.Equal(`{"field-1":"hello"}`, str)
  213. }
  214. func Test_mixed(t *testing.T) {
  215. should := require.New(t)
  216. type AA struct {
  217. ID int `json:"id"`
  218. Payload map[string]interface{} `json:"payload"`
  219. buf *bytes.Buffer `json:"-"`
  220. }
  221. aa := AA{}
  222. err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
  223. should.Nil(err)
  224. should.Equal(1, aa.ID)
  225. should.Equal("123", aa.Payload["account"])
  226. }
  227. func Test_omit_empty(t *testing.T) {
  228. should := require.New(t)
  229. type TestObject struct {
  230. Field1 string `json:"field-1,omitempty"`
  231. Field2 string `json:"field-2,omitempty"`
  232. Field3 string `json:"field-3,omitempty"`
  233. }
  234. obj := TestObject{}
  235. obj.Field2 = "hello"
  236. str, err := MarshalToString(&obj)
  237. should.Nil(err)
  238. should.Equal(`{"field-2":"hello"}`, str)
  239. }
  240. func Test_recursive_struct(t *testing.T) {
  241. should := require.New(t)
  242. type TestObject struct {
  243. Field1 string
  244. Me *TestObject
  245. }
  246. obj := TestObject{}
  247. str, err := MarshalToString(obj)
  248. should.Nil(err)
  249. should.Contains(str, `"Field1":""`)
  250. should.Contains(str, `"Me":null`)
  251. err = UnmarshalFromString(str, &obj)
  252. should.Nil(err)
  253. }
  254. func Test_one_field_struct(t *testing.T) {
  255. should := require.New(t)
  256. type YetYetAnotherObject struct {
  257. Field string
  258. }
  259. type YetAnotherObject struct {
  260. Field *YetYetAnotherObject
  261. }
  262. type AnotherObject struct {
  263. Field *YetAnotherObject
  264. }
  265. type TestObject struct {
  266. Me *AnotherObject
  267. }
  268. obj := TestObject{&AnotherObject{&YetAnotherObject{&YetYetAnotherObject{"abc"}}}}
  269. str, err := MarshalToString(obj)
  270. should.Nil(err)
  271. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  272. str, err = MarshalToString(&obj)
  273. should.Nil(err)
  274. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  275. }
  276. func Test_anonymous_struct_marshal(t *testing.T) {
  277. should := require.New(t)
  278. type TestObject struct {
  279. Field string
  280. }
  281. str, err := MarshalToString(struct {
  282. TestObject
  283. Field int
  284. }{
  285. Field: 100,
  286. })
  287. should.Nil(err)
  288. should.Equal(`{"Field":100}`, str)
  289. }
  290. func Test_decode_nested(t *testing.T) {
  291. type StructOfString struct {
  292. Field1 string
  293. Field2 string
  294. }
  295. iter := ParseString(ConfigDefault, `[{"field1": "hello"}, null, {"field2": "world"}]`)
  296. slice := []*StructOfString{}
  297. iter.ReadVal(&slice)
  298. if len(slice) != 3 {
  299. fmt.Println(iter.Error)
  300. t.Fatal(len(slice))
  301. }
  302. if slice[0].Field1 != "hello" {
  303. fmt.Println(iter.Error)
  304. t.Fatal(slice[0])
  305. }
  306. if slice[1] != nil {
  307. fmt.Println(iter.Error)
  308. t.Fatal(slice[1])
  309. }
  310. if slice[2].Field2 != "world" {
  311. fmt.Println(iter.Error)
  312. t.Fatal(slice[2])
  313. }
  314. }