jsoniter_object_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/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": "stream"}`)
  22. field := iter.ReadObject()
  23. should.Equal("a", field)
  24. value := iter.ReadString()
  25. should.Equal("stream", value)
  26. field = iter.ReadObject()
  27. should.Equal("", field)
  28. iter = ParseString(ConfigDefault, `{"a": "stream"}`)
  29. should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
  30. should.Equal("a", field)
  31. iter.Skip()
  32. return true
  33. }))
  34. }
  35. func Test_two_field(t *testing.T) {
  36. should := require.New(t)
  37. iter := ParseString(ConfigDefault, `{ "a": "stream" , "c": "d" }`)
  38. field := iter.ReadObject()
  39. should.Equal("a", field)
  40. value := iter.ReadString()
  41. should.Equal("stream", value)
  42. field = iter.ReadObject()
  43. should.Equal("c", field)
  44. value = iter.ReadString()
  45. should.Equal("d", value)
  46. field = iter.ReadObject()
  47. should.Equal("", field)
  48. iter = ParseString(ConfigDefault, `{"field1": "1", "field2": 2}`)
  49. for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
  50. switch field {
  51. case "field1":
  52. iter.ReadString()
  53. case "field2":
  54. iter.ReadInt64()
  55. default:
  56. iter.ReportError("bind object", "unexpected field")
  57. }
  58. }
  59. }
  60. func Test_object_wrapper_any_get_all(t *testing.T) {
  61. should := require.New(t)
  62. type TestObject struct {
  63. Field1 []int
  64. Field2 []int
  65. }
  66. any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
  67. should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
  68. should.Contains(any.Keys(), "Field1")
  69. should.Contains(any.Keys(), "Field2")
  70. should.NotContains(any.Keys(), "Field3")
  71. //should.Contains(any.GetObject()["Field1"].GetArray()[0], 1)
  72. }
  73. func Test_write_object(t *testing.T) {
  74. should := require.New(t)
  75. buf := &bytes.Buffer{}
  76. stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
  77. stream.WriteObjectStart()
  78. stream.WriteObjectField("hello")
  79. stream.WriteInt(1)
  80. stream.WriteMore()
  81. stream.WriteObjectField("world")
  82. stream.WriteInt(2)
  83. stream.WriteObjectEnd()
  84. stream.Flush()
  85. should.Nil(stream.Error)
  86. should.Equal("{\n \"hello\": 1,\n \"world\": 2\n}", buf.String())
  87. }
  88. func Test_write_val_zero_field_struct(t *testing.T) {
  89. should := require.New(t)
  90. type TestObject struct {
  91. }
  92. obj := TestObject{}
  93. str, err := MarshalToString(obj)
  94. should.Nil(err)
  95. should.Equal(`{}`, str)
  96. }
  97. func Test_write_val_one_field_struct(t *testing.T) {
  98. should := require.New(t)
  99. type TestObject struct {
  100. Field1 string `json:"field-1"`
  101. }
  102. obj := TestObject{"hello"}
  103. str, err := MarshalToString(obj)
  104. should.Nil(err)
  105. should.Equal(`{"field-1":"hello"}`, str)
  106. }
  107. func Test_mixed(t *testing.T) {
  108. should := require.New(t)
  109. type AA struct {
  110. ID int `json:"id"`
  111. Payload map[string]interface{} `json:"payload"`
  112. buf *bytes.Buffer
  113. }
  114. aa := AA{}
  115. err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
  116. should.Nil(err)
  117. should.Equal(1, aa.ID)
  118. should.Equal("123", aa.Payload["account"])
  119. }
  120. func Test_omit_empty(t *testing.T) {
  121. should := require.New(t)
  122. type TestObject struct {
  123. Field1 string `json:"field-1,omitempty"`
  124. Field2 string `json:"field-2,omitempty"`
  125. Field3 string `json:"field-3,omitempty"`
  126. }
  127. obj := TestObject{}
  128. obj.Field2 = "hello"
  129. str, err := MarshalToString(&obj)
  130. should.Nil(err)
  131. should.Equal(`{"field-2":"hello"}`, str)
  132. }
  133. func Test_ignore_field_on_not_valid_type(t *testing.T) {
  134. should := require.New(t)
  135. type TestObject struct {
  136. Field1 string `json:"field-1,omitempty"`
  137. Field2 func() `json:"-"`
  138. }
  139. obj := TestObject{}
  140. obj.Field1 = "hello world"
  141. obj.Field2 = func() {}
  142. str, err := MarshalToString(&obj)
  143. should.Nil(err)
  144. should.Equal(`{"field-1":"hello world"}`, str)
  145. }
  146. func Test_nested_field_omit_empty(t *testing.T) {
  147. should := require.New(t)
  148. type S1 struct {
  149. F1 string `json:",omitempty"`
  150. }
  151. type S2 struct {
  152. *S1
  153. F2 string `json:",omitempty"`
  154. }
  155. s1 := &S1{
  156. //F1: "abc",
  157. }
  158. s2 := &S2{
  159. S1: s1,
  160. F2: "123",
  161. }
  162. str, err := MarshalToString(s2)
  163. should.Nil(err)
  164. should.Equal(`{"F2":"123"}`, str)
  165. }
  166. func Test_recursive_struct(t *testing.T) {
  167. should := require.New(t)
  168. type TestObject struct {
  169. Field1 string
  170. Me *TestObject
  171. }
  172. obj := TestObject{}
  173. str, err := MarshalToString(obj)
  174. should.Nil(err)
  175. should.Contains(str, `"Field1":""`)
  176. should.Contains(str, `"Me":null`)
  177. err = UnmarshalFromString(str, &obj)
  178. should.Nil(err)
  179. }
  180. func Test_encode_anonymous_struct(t *testing.T) {
  181. should := require.New(t)
  182. type TestObject struct {
  183. Field string
  184. }
  185. str, err := MarshalToString(struct {
  186. TestObject
  187. Field int
  188. }{
  189. Field: 100,
  190. })
  191. should.Nil(err)
  192. should.Equal(`{"Field":100}`, str)
  193. }
  194. func Test_decode_anonymous_struct(t *testing.T) {
  195. should := require.New(t)
  196. type Inner struct {
  197. Key string `json:"key"`
  198. }
  199. type Outer struct {
  200. Inner
  201. }
  202. var outer Outer
  203. j := []byte("{\"key\":\"value\"}")
  204. should.Nil(Unmarshal(j, &outer))
  205. should.Equal("value", outer.Key)
  206. }
  207. func Test_multiple_level_anonymous_struct(t *testing.T) {
  208. type Level1 struct {
  209. Field1 string
  210. }
  211. type Level2 struct {
  212. Level1
  213. Field2 string
  214. }
  215. type Level3 struct {
  216. Level2
  217. Field3 string
  218. }
  219. should := require.New(t)
  220. obj := Level3{Level2{Level1{"1"}, "2"}, "3"}
  221. output, err := MarshalToString(obj)
  222. should.Nil(err)
  223. should.Equal(`{"Field1":"1","Field2":"2","Field3":"3"}`, output)
  224. }
  225. func Test_multiple_level_anonymous_struct_with_ptr(t *testing.T) {
  226. type Level1 struct {
  227. Field1 string
  228. Field2 string
  229. Field4 string
  230. }
  231. type Level2 struct {
  232. *Level1
  233. Field2 string
  234. Field3 string
  235. }
  236. type Level3 struct {
  237. *Level2
  238. Field3 string
  239. }
  240. should := require.New(t)
  241. obj := Level3{&Level2{&Level1{"1", "", "4"}, "2", ""}, "3"}
  242. output, err := MarshalToString(obj)
  243. should.Nil(err)
  244. should.Contains(output, `"Field1":"1"`)
  245. should.Contains(output, `"Field2":"2"`)
  246. should.Contains(output, `"Field3":"3"`)
  247. should.Contains(output, `"Field4":"4"`)
  248. }
  249. func Test_shadow_struct_field(t *testing.T) {
  250. should := require.New(t)
  251. type omit *struct{}
  252. type CacheItem struct {
  253. Key string `json:"key"`
  254. MaxAge int `json:"cacheAge"`
  255. }
  256. output, err := MarshalToString(struct {
  257. *CacheItem
  258. // Omit bad keys
  259. OmitMaxAge omit `json:"cacheAge,omitempty"`
  260. // Add nice keys
  261. MaxAge int `json:"max_age"`
  262. }{
  263. CacheItem: &CacheItem{
  264. Key: "value",
  265. MaxAge: 100,
  266. },
  267. MaxAge: 20,
  268. })
  269. should.Nil(err)
  270. should.Contains(output, `"key":"value"`)
  271. should.Contains(output, `"max_age":20`)
  272. }
  273. func Test_embedded_order(t *testing.T) {
  274. type A struct {
  275. Field2 string
  276. }
  277. type C struct {
  278. Field5 string
  279. }
  280. type B struct {
  281. Field4 string
  282. C
  283. Field6 string
  284. }
  285. type TestObject struct {
  286. Field1 string
  287. A
  288. Field3 string
  289. B
  290. Field7 string
  291. }
  292. should := require.New(t)
  293. s := TestObject{}
  294. output, err := MarshalToString(s)
  295. should.Nil(err)
  296. should.Equal(`{"Field1":"","Field2":"","Field3":"","Field4":"","Field5":"","Field6":"","Field7":""}`, output)
  297. }
  298. func Test_decode_nested(t *testing.T) {
  299. type StructOfString struct {
  300. Field1 string
  301. Field2 string
  302. }
  303. iter := ParseString(ConfigDefault, `[{"field1": "hello"}, null, {"field2": "world"}]`)
  304. slice := []*StructOfString{}
  305. iter.ReadVal(&slice)
  306. if len(slice) != 3 {
  307. fmt.Println(iter.Error)
  308. t.Fatal(len(slice))
  309. }
  310. if slice[0].Field1 != "hello" {
  311. fmt.Println(iter.Error)
  312. t.Fatal(slice[0])
  313. }
  314. if slice[1] != nil {
  315. fmt.Println(iter.Error)
  316. t.Fatal(slice[1])
  317. }
  318. if slice[2].Field2 != "world" {
  319. fmt.Println(iter.Error)
  320. t.Fatal(slice[2])
  321. }
  322. }
  323. func Test_decode_field_with_escape(t *testing.T) {
  324. should := require.New(t)
  325. type TestObject struct {
  326. Field1 string
  327. }
  328. var obj TestObject
  329. should.Nil(ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(`{"Field\"1":"hello"}`), &obj))
  330. should.Equal("", obj.Field1)
  331. should.Nil(ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(`{"\u0046ield1":"hello"}`), &obj))
  332. should.Equal("hello", obj.Field1)
  333. }