jsoniter_reflect_struct_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package jsoniter
  2. import (
  3. "bytes"
  4. "github.com/json-iterator/go/require"
  5. "testing"
  6. )
  7. func Test_decode_one_field_struct(t *testing.T) {
  8. should := require.New(t)
  9. type TestObject struct {
  10. Field1 string
  11. }
  12. obj := TestObject{}
  13. should.Nil(UnmarshalFromString(`{}`, &obj))
  14. should.Equal("", obj.Field1)
  15. should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
  16. should.Equal("hello", obj.Field1)
  17. }
  18. func Test_decode_two_fields_struct(t *testing.T) {
  19. should := require.New(t)
  20. type TestObject struct {
  21. Field1 string
  22. Field2 string
  23. }
  24. obj := TestObject{}
  25. should.Nil(UnmarshalFromString(`{}`, &obj))
  26. should.Equal("", obj.Field1)
  27. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b"}`, &obj))
  28. should.Equal("a", obj.Field1)
  29. should.Equal("b", obj.Field2)
  30. }
  31. func Test_decode_three_fields_struct(t *testing.T) {
  32. should := require.New(t)
  33. type TestObject struct {
  34. Field1 string
  35. Field2 string
  36. Field3 string
  37. }
  38. obj := TestObject{}
  39. should.Nil(UnmarshalFromString(`{}`, &obj))
  40. should.Equal("", obj.Field1)
  41. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c"}`, &obj))
  42. should.Equal("a", obj.Field1)
  43. should.Equal("b", obj.Field2)
  44. should.Equal("c", obj.Field3)
  45. }
  46. func Test_decode_four_fields_struct(t *testing.T) {
  47. should := require.New(t)
  48. type TestObject struct {
  49. Field1 string
  50. Field2 string
  51. Field3 string
  52. Field4 string
  53. }
  54. obj := TestObject{}
  55. should.Nil(UnmarshalFromString(`{}`, &obj))
  56. should.Equal("", obj.Field1)
  57. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d"}`, &obj))
  58. should.Equal("a", obj.Field1)
  59. should.Equal("b", obj.Field2)
  60. should.Equal("c", obj.Field3)
  61. should.Equal("d", obj.Field4)
  62. }
  63. func Test_decode_five_fields_struct(t *testing.T) {
  64. should := require.New(t)
  65. type TestObject struct {
  66. Field1 string
  67. Field2 string
  68. Field3 string
  69. Field4 string
  70. Field5 string
  71. }
  72. obj := TestObject{}
  73. should.Nil(UnmarshalFromString(`{}`, &obj))
  74. should.Equal("", obj.Field1)
  75. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
  76. should.Equal("a", obj.Field1)
  77. should.Equal("b", obj.Field2)
  78. should.Equal("c", obj.Field3)
  79. should.Equal("d", obj.Field4)
  80. should.Equal("e", obj.Field5)
  81. }
  82. func Test_decode_ten_fields_struct(t *testing.T) {
  83. should := require.New(t)
  84. type TestObject struct {
  85. Field1 string
  86. Field2 string
  87. Field3 string
  88. Field4 string
  89. Field5 string
  90. Field6 string
  91. Field7 string
  92. Field8 string
  93. Field9 string
  94. Field10 string
  95. }
  96. obj := TestObject{}
  97. should.Nil(UnmarshalFromString(`{}`, &obj))
  98. should.Equal("", obj.Field1)
  99. should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj))
  100. should.Equal("a", obj.Field1)
  101. should.Equal("b", obj.Field2)
  102. should.Equal("c", obj.Field3)
  103. should.Equal("d", obj.Field4)
  104. should.Equal("e", obj.Field5)
  105. }
  106. func Test_decode_struct_field_with_tag(t *testing.T) {
  107. should := require.New(t)
  108. type TestObject struct {
  109. Field1 string `json:"field-1"`
  110. Field2 string `json:"-"`
  111. Field3 int `json:",string"`
  112. }
  113. obj := TestObject{Field2: "world"}
  114. UnmarshalFromString(`{"field-1": "hello", "field2": "", "Field3": "100"}`, &obj)
  115. should.Equal("hello", obj.Field1)
  116. should.Equal("world", obj.Field2)
  117. should.Equal(100, obj.Field3)
  118. }
  119. func Test_write_val_zero_field_struct(t *testing.T) {
  120. should := require.New(t)
  121. type TestObject struct {
  122. }
  123. obj := TestObject{}
  124. str, err := MarshalToString(obj)
  125. should.Nil(err)
  126. should.Equal(`{}`, str)
  127. }
  128. func Test_write_val_one_field_struct(t *testing.T) {
  129. should := require.New(t)
  130. type TestObject struct {
  131. Field1 string `json:"field-1"`
  132. }
  133. obj := TestObject{"hello"}
  134. str, err := MarshalToString(obj)
  135. should.Nil(err)
  136. should.Equal(`{"field-1":"hello"}`, str)
  137. }
  138. func Test_mixed(t *testing.T) {
  139. should := require.New(t)
  140. type AA struct {
  141. ID int `json:"id"`
  142. Payload map[string]interface{} `json:"payload"`
  143. buf *bytes.Buffer `json:"-"`
  144. }
  145. aa := AA{}
  146. err := UnmarshalFromString(` {"id":1, "payload":{"account":"123","password":"456"}}`, &aa)
  147. should.Nil(err)
  148. should.Equal(1, aa.ID)
  149. should.Equal("123", aa.Payload["account"])
  150. }
  151. func Test_omit_empty(t *testing.T) {
  152. should := require.New(t)
  153. type TestObject struct {
  154. Field1 string `json:"field-1,omitempty"`
  155. Field2 string `json:"field-2,omitempty"`
  156. Field3 string `json:"field-3,omitempty"`
  157. }
  158. obj := TestObject{}
  159. obj.Field2 = "hello"
  160. str, err := MarshalToString(&obj)
  161. should.Nil(err)
  162. should.Equal(`{"field-2":"hello"}`, str)
  163. }
  164. func Test_any_within_struct(t *testing.T) {
  165. should := require.New(t)
  166. type TestObject struct {
  167. Field1 Any
  168. Field2 Any
  169. }
  170. obj := TestObject{}
  171. err := UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj)
  172. should.Nil(err)
  173. should.Equal("hello", obj.Field1.ToString())
  174. should.Equal("[1,2,3]", obj.Field2.ToString())
  175. }
  176. func Test_recursive_struct(t *testing.T) {
  177. should := require.New(t)
  178. type TestObject struct {
  179. Field1 string
  180. Me *TestObject
  181. }
  182. obj := TestObject{}
  183. str, err := MarshalToString(obj)
  184. should.Nil(err)
  185. should.Contains(str, `"Field1":""`)
  186. should.Contains(str, `"Me":null`)
  187. err = UnmarshalFromString(str, &obj)
  188. should.Nil(err)
  189. }
  190. func Test_one_field_struct(t *testing.T) {
  191. should := require.New(t)
  192. type YetYetAnotherObject struct {
  193. Field string
  194. }
  195. type YetAnotherObject struct {
  196. Field *YetYetAnotherObject
  197. }
  198. type AnotherObject struct {
  199. Field *YetAnotherObject
  200. }
  201. type TestObject struct {
  202. Me *AnotherObject
  203. }
  204. obj := TestObject{&AnotherObject{&YetAnotherObject{&YetYetAnotherObject{"abc"}}}}
  205. str, err := MarshalToString(obj)
  206. should.Nil(err)
  207. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  208. str, err = MarshalToString(&obj)
  209. should.Nil(err)
  210. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  211. }
  212. func Test_anonymous_struct_marshal(t *testing.T) {
  213. should := require.New(t)
  214. type TestObject struct {
  215. Field string
  216. }
  217. str, err := MarshalToString(struct {
  218. TestObject
  219. Field int
  220. }{
  221. Field: 100,
  222. })
  223. should.Nil(err)
  224. should.Equal(`{"Field":100}`, str)
  225. }