jsoniter_object_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package misc_tests
  2. import (
  3. "bytes"
  4. "reflect"
  5. "testing"
  6. "github.com/json-iterator/go"
  7. "github.com/stretchr/testify/require"
  8. "strings"
  9. "time"
  10. )
  11. func Test_empty_object(t *testing.T) {
  12. should := require.New(t)
  13. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{}`)
  14. field := iter.ReadObject()
  15. should.Equal("", field)
  16. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{}`)
  17. iter.ReadObjectCB(func(iter *jsoniter.Iterator, field string) bool {
  18. should.FailNow("should not call")
  19. return true
  20. })
  21. }
  22. func Test_one_field(t *testing.T) {
  23. should := require.New(t)
  24. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{"a": "stream"}`)
  25. field := iter.ReadObject()
  26. should.Equal("a", field)
  27. value := iter.ReadString()
  28. should.Equal("stream", value)
  29. field = iter.ReadObject()
  30. should.Equal("", field)
  31. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{"a": "stream"}`)
  32. should.True(iter.ReadObjectCB(func(iter *jsoniter.Iterator, field string) bool {
  33. should.Equal("a", field)
  34. iter.Skip()
  35. return true
  36. }))
  37. }
  38. func Test_two_field(t *testing.T) {
  39. should := require.New(t)
  40. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{ "a": "stream" , "c": "d" }`)
  41. field := iter.ReadObject()
  42. should.Equal("a", field)
  43. value := iter.ReadString()
  44. should.Equal("stream", value)
  45. field = iter.ReadObject()
  46. should.Equal("c", field)
  47. value = iter.ReadString()
  48. should.Equal("d", value)
  49. field = iter.ReadObject()
  50. should.Equal("", field)
  51. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{"field1": "1", "field2": 2}`)
  52. for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  53. switch field {
  54. case "field1":
  55. iter.ReadString()
  56. case "field2":
  57. iter.ReadInt64()
  58. default:
  59. iter.ReportError("bind object", "unexpected field")
  60. }
  61. }
  62. }
  63. func Test_write_object(t *testing.T) {
  64. should := require.New(t)
  65. buf := &bytes.Buffer{}
  66. stream := jsoniter.NewStream(jsoniter.Config{IndentionStep: 2}.Froze(), buf, 4096)
  67. stream.WriteObjectStart()
  68. stream.WriteObjectField("hello")
  69. stream.WriteInt(1)
  70. stream.WriteMore()
  71. stream.WriteObjectField("world")
  72. stream.WriteInt(2)
  73. stream.WriteObjectEnd()
  74. stream.Flush()
  75. should.Nil(stream.Error)
  76. should.Equal("{\n \"hello\": 1,\n \"world\": 2\n}", buf.String())
  77. }
  78. func Test_reader_and_load_more(t *testing.T) {
  79. should := require.New(t)
  80. type TestObject struct {
  81. CreatedAt time.Time
  82. }
  83. reader := strings.NewReader(`
  84. {
  85. "agency": null,
  86. "candidateId": 0,
  87. "candidate": "Blah Blah",
  88. "bookingId": 0,
  89. "shiftId": 1,
  90. "shiftTypeId": 0,
  91. "shift": "Standard",
  92. "bonus": 0,
  93. "bonusNI": 0,
  94. "days": [],
  95. "totalHours": 27,
  96. "expenses": [],
  97. "weekEndingDateSystem": "2016-10-09",
  98. "weekEndingDateClient": "2016-10-09",
  99. "submittedAt": null,
  100. "submittedById": null,
  101. "approvedAt": "2016-10-10T18:38:04Z",
  102. "approvedById": 0,
  103. "authorisedAt": "2016-10-10T18:38:04Z",
  104. "authorisedById": 0,
  105. "invoicedAt": "2016-10-10T20:00:00Z",
  106. "revokedAt": null,
  107. "revokedById": null,
  108. "revokeReason": null,
  109. "rejectedAt": null,
  110. "rejectedById": null,
  111. "rejectReasonCode": null,
  112. "rejectReason": null,
  113. "createdAt": "2016-10-03T00:00:00Z",
  114. "updatedAt": "2016-11-09T10:26:13Z",
  115. "updatedById": null,
  116. "overrides": [],
  117. "bookingApproverId": null,
  118. "bookingApprover": null,
  119. "status": "approved"
  120. }
  121. `)
  122. decoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(reader)
  123. obj := TestObject{}
  124. should.Nil(decoder.Decode(&obj))
  125. }
  126. func Test_unmarshal_into_existing_value(t *testing.T) {
  127. should := require.New(t)
  128. type TestObject struct {
  129. Field1 int
  130. Field2 interface{}
  131. }
  132. var obj TestObject
  133. m := map[string]interface{}{}
  134. obj.Field2 = &m
  135. cfg := jsoniter.Config{UseNumber: true}.Froze()
  136. err := cfg.Unmarshal([]byte(`{"Field1":1,"Field2":{"k":"v"}}`), &obj)
  137. should.NoError(err)
  138. should.Equal(map[string]interface{}{
  139. "k": "v",
  140. }, m)
  141. }
  142. // for issue421
  143. func Test_unmarshal_anonymous_struct_invalid(t *testing.T) {
  144. should := require.New(t)
  145. t0 := struct {
  146. Field1 string
  147. }{}
  148. cfg := jsoniter.ConfigCompatibleWithStandardLibrary
  149. err := cfg.UnmarshalFromString(`{"Field1":`, &t0)
  150. should.NotNil(err)
  151. should.NotContains(err.Error(), reflect.TypeOf(t0).String())
  152. cfgCaseSensitive := jsoniter.Config{
  153. CaseSensitive: true,
  154. }.Froze()
  155. type TestObject1 struct {
  156. Field1 struct {
  157. InnerField1 string
  158. }
  159. }
  160. t1 := TestObject1{}
  161. err = cfgCaseSensitive.UnmarshalFromString(`{"Field1":{"InnerField1"`, &t1)
  162. should.NotNil(err)
  163. should.NotContains(err.Error(), reflect.TypeOf(t1.Field1).String())
  164. should.Contains(err.Error(), reflect.TypeOf(t1).String())
  165. type TestObject2 struct {
  166. Field1 int
  167. Field2 struct {
  168. InnerField1 string
  169. InnerField2 string
  170. }
  171. }
  172. t2 := TestObject2{}
  173. err = cfgCaseSensitive.UnmarshalFromString(`{"Field2":{"InnerField2"`, &t2)
  174. should.NotNil(err)
  175. should.NotContains(err.Error(), reflect.TypeOf(t2.Field2).String())
  176. should.Contains(err.Error(), reflect.TypeOf(t2).String())
  177. type TestObject3 struct {
  178. Field1 int
  179. Field2 int
  180. Field3 struct {
  181. InnerField1 string
  182. InnerField2 string
  183. InnerField3 string
  184. }
  185. }
  186. t3 := TestObject3{}
  187. err = cfgCaseSensitive.UnmarshalFromString(`{"Field3":{"InnerField3"`, &t3)
  188. should.NotNil(err)
  189. should.NotContains(err.Error(), reflect.TypeOf(t3.Field3).String())
  190. should.Contains(err.Error(), reflect.TypeOf(t3).String())
  191. type TestObject4 struct {
  192. Field1 int
  193. Field2 int
  194. Field3 int
  195. Field4 struct {
  196. InnerField1 string
  197. InnerField2 string
  198. InnerField3 string
  199. InnerField4 string
  200. }
  201. }
  202. t4 := TestObject4{}
  203. err = cfgCaseSensitive.UnmarshalFromString(`{"Field4":{"InnerField4"`, &t4)
  204. should.NotNil(err)
  205. should.NotContains(err.Error(), reflect.TypeOf(t4.Field4).String())
  206. should.Contains(err.Error(), reflect.TypeOf(t4).String())
  207. type TestObject5 struct {
  208. Field1 int
  209. Field2 int
  210. Field3 int
  211. Field4 int
  212. Field5 struct {
  213. InnerField1 string
  214. InnerField2 string
  215. InnerField3 string
  216. InnerField4 string
  217. InnerField5 string
  218. }
  219. }
  220. t5 := TestObject5{}
  221. err = cfgCaseSensitive.UnmarshalFromString(`{"Field5":{"InnerField5"`, &t5)
  222. should.NotNil(err)
  223. should.NotContains(err.Error(), reflect.TypeOf(t5.Field5).String())
  224. should.Contains(err.Error(), reflect.TypeOf(t5).String())
  225. type TestObject6 struct {
  226. Field1 int
  227. Field2 int
  228. Field3 int
  229. Field4 int
  230. Field5 int
  231. Field6 struct {
  232. InnerField1 string
  233. InnerField2 string
  234. InnerField3 string
  235. InnerField4 string
  236. InnerField5 string
  237. InnerField6 string
  238. }
  239. }
  240. t6 := TestObject6{}
  241. err = cfgCaseSensitive.UnmarshalFromString(`{"Field6":{"InnerField6"`, &t6)
  242. should.NotNil(err)
  243. should.NotContains(err.Error(), reflect.TypeOf(t6.Field6).String())
  244. should.Contains(err.Error(), reflect.TypeOf(t6).String())
  245. type TestObject7 struct {
  246. Field1 int
  247. Field2 int
  248. Field3 int
  249. Field4 int
  250. Field5 int
  251. Field6 int
  252. Field7 struct {
  253. InnerField1 string
  254. InnerField2 string
  255. InnerField3 string
  256. InnerField4 string
  257. InnerField5 string
  258. InnerField6 string
  259. InnerField7 string
  260. }
  261. }
  262. t7 := TestObject7{}
  263. err = cfgCaseSensitive.UnmarshalFromString(`{"Field7":{"InnerField7"`, &t7)
  264. should.NotNil(err)
  265. should.NotContains(err.Error(), reflect.TypeOf(t7.Field7).String())
  266. should.Contains(err.Error(), reflect.TypeOf(t7).String())
  267. type TestObject8 struct {
  268. Field1 int
  269. Field2 int
  270. Field3 int
  271. Field4 int
  272. Field5 int
  273. Field6 int
  274. Field7 int
  275. Field8 struct {
  276. InnerField1 string
  277. InnerField2 string
  278. InnerField3 string
  279. InnerField4 string
  280. InnerField5 string
  281. InnerField6 string
  282. InnerField7 string
  283. InnerField8 string
  284. }
  285. }
  286. t8 := TestObject8{}
  287. err = cfgCaseSensitive.UnmarshalFromString(`{"Field8":{"InnerField8"`, &t8)
  288. should.NotNil(err)
  289. should.NotContains(err.Error(), reflect.TypeOf(t8.Field8).String())
  290. should.Contains(err.Error(), reflect.TypeOf(t8).String())
  291. type TestObject9 struct {
  292. Field1 int
  293. Field2 int
  294. Field3 int
  295. Field4 int
  296. Field5 int
  297. Field6 int
  298. Field7 int
  299. Field8 int
  300. Field9 struct {
  301. InnerField1 string
  302. InnerField2 string
  303. InnerField3 string
  304. InnerField4 string
  305. InnerField5 string
  306. InnerField6 string
  307. InnerField7 string
  308. InnerField8 string
  309. InnerField9 string
  310. }
  311. }
  312. t9 := TestObject9{}
  313. err = cfgCaseSensitive.UnmarshalFromString(`{"Field9":{"InnerField9"`, &t9)
  314. should.NotNil(err)
  315. should.NotContains(err.Error(), reflect.TypeOf(t9.Field9).String())
  316. should.Contains(err.Error(), reflect.TypeOf(t9).String())
  317. type TestObject10 struct {
  318. Field1 int
  319. Field2 int
  320. Field3 int
  321. Field4 int
  322. Field5 int
  323. Field6 int
  324. Field7 int
  325. Field8 int
  326. Field9 int
  327. Field10 struct {
  328. InnerField1 string
  329. InnerField2 string
  330. InnerField3 string
  331. InnerField4 string
  332. InnerField5 string
  333. InnerField6 string
  334. InnerField7 string
  335. InnerField8 string
  336. InnerField9 string
  337. InnerField10 string
  338. }
  339. }
  340. t10 := TestObject10{}
  341. err = cfgCaseSensitive.UnmarshalFromString(`{"Field10":{"InnerField10"`, &t10)
  342. should.NotNil(err)
  343. should.NotContains(err.Error(), reflect.TypeOf(t10.Field10).String())
  344. should.Contains(err.Error(), reflect.TypeOf(t10).String())
  345. err = cfg.UnmarshalFromString(`{"Field10":{"InnerField10"`, &t10)
  346. should.NotNil(err)
  347. should.NotContains(err.Error(), reflect.TypeOf(t10.Field10).String())
  348. should.Contains(err.Error(), reflect.TypeOf(t10).String())
  349. }