jsoniter_object_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package misc_tests
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/json-iterator/go"
  6. "github.com/stretchr/testify/require"
  7. "strings"
  8. "time"
  9. )
  10. func Test_empty_object(t *testing.T) {
  11. should := require.New(t)
  12. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{}`)
  13. field := iter.ReadObject()
  14. should.Equal("", field)
  15. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{}`)
  16. iter.ReadObjectCB(func(iter *jsoniter.Iterator, field string) bool {
  17. should.FailNow("should not call")
  18. return true
  19. })
  20. }
  21. func Test_one_field(t *testing.T) {
  22. should := require.New(t)
  23. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{"a": "stream"}`)
  24. field := iter.ReadObject()
  25. should.Equal("a", field)
  26. value := iter.ReadString()
  27. should.Equal("stream", value)
  28. field = iter.ReadObject()
  29. should.Equal("", field)
  30. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{"a": "stream"}`)
  31. should.True(iter.ReadObjectCB(func(iter *jsoniter.Iterator, field string) bool {
  32. should.Equal("a", field)
  33. iter.Skip()
  34. return true
  35. }))
  36. }
  37. func Test_two_field(t *testing.T) {
  38. should := require.New(t)
  39. iter := jsoniter.ParseString(jsoniter.ConfigDefault, `{ "a": "stream" , "c": "d" }`)
  40. field := iter.ReadObject()
  41. should.Equal("a", field)
  42. value := iter.ReadString()
  43. should.Equal("stream", value)
  44. field = iter.ReadObject()
  45. should.Equal("c", field)
  46. value = iter.ReadString()
  47. should.Equal("d", value)
  48. field = iter.ReadObject()
  49. should.Equal("", field)
  50. iter = jsoniter.ParseString(jsoniter.ConfigDefault, `{"field1": "1", "field2": 2}`)
  51. for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  52. switch field {
  53. case "field1":
  54. iter.ReadString()
  55. case "field2":
  56. iter.ReadInt64()
  57. default:
  58. iter.ReportError("bind object", "unexpected field")
  59. }
  60. }
  61. }
  62. func Test_write_object(t *testing.T) {
  63. should := require.New(t)
  64. buf := &bytes.Buffer{}
  65. stream := jsoniter.NewStream(jsoniter.Config{IndentionStep: 2}.Froze(), buf, 4096)
  66. stream.WriteObjectStart()
  67. stream.WriteObjectField("hello")
  68. stream.WriteInt(1)
  69. stream.WriteMore()
  70. stream.WriteObjectField("world")
  71. stream.WriteInt(2)
  72. stream.WriteObjectEnd()
  73. stream.Flush()
  74. should.Nil(stream.Error)
  75. should.Equal("{\n \"hello\": 1,\n \"world\": 2\n}", buf.String())
  76. }
  77. func Test_reader_and_load_more(t *testing.T) {
  78. should := require.New(t)
  79. type TestObject struct {
  80. CreatedAt time.Time
  81. }
  82. reader := strings.NewReader(`
  83. {
  84. "agency": null,
  85. "candidateId": 0,
  86. "candidate": "Blah Blah",
  87. "bookingId": 0,
  88. "shiftId": 1,
  89. "shiftTypeId": 0,
  90. "shift": "Standard",
  91. "bonus": 0,
  92. "bonusNI": 0,
  93. "days": [],
  94. "totalHours": 27,
  95. "expenses": [],
  96. "weekEndingDateSystem": "2016-10-09",
  97. "weekEndingDateClient": "2016-10-09",
  98. "submittedAt": null,
  99. "submittedById": null,
  100. "approvedAt": "2016-10-10T18:38:04Z",
  101. "approvedById": 0,
  102. "authorisedAt": "2016-10-10T18:38:04Z",
  103. "authorisedById": 0,
  104. "invoicedAt": "2016-10-10T20:00:00Z",
  105. "revokedAt": null,
  106. "revokedById": null,
  107. "revokeReason": null,
  108. "rejectedAt": null,
  109. "rejectedById": null,
  110. "rejectReasonCode": null,
  111. "rejectReason": null,
  112. "createdAt": "2016-10-03T00:00:00Z",
  113. "updatedAt": "2016-11-09T10:26:13Z",
  114. "updatedById": null,
  115. "overrides": [],
  116. "bookingApproverId": null,
  117. "bookingApprover": null,
  118. "status": "approved"
  119. }
  120. `)
  121. decoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(reader)
  122. obj := TestObject{}
  123. should.Nil(decoder.Decode(&obj))
  124. }