jsoniter_interface_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/stretchr/testify/require"
  5. "testing"
  6. "unsafe"
  7. )
  8. func Test_write_array_of_interface(t *testing.T) {
  9. should := require.New(t)
  10. array := []interface{}{"hello"}
  11. str, err := MarshalToString(array)
  12. should.Nil(err)
  13. should.Equal(`["hello"]`, str)
  14. }
  15. func Test_write_map_of_interface(t *testing.T) {
  16. should := require.New(t)
  17. val := map[string]interface{}{"hello": "world"}
  18. str, err := MarshalToString(val)
  19. should.Nil(err)
  20. should.Equal(`{"hello":"world"}`, str)
  21. }
  22. func Test_write_map_of_interface_in_struct(t *testing.T) {
  23. type TestObject struct {
  24. Field map[string]interface{}
  25. }
  26. should := require.New(t)
  27. val := TestObject{map[string]interface{}{"hello": "world"}}
  28. str, err := MarshalToString(val)
  29. should.Nil(err)
  30. should.Equal(`{"Field":{"hello":"world"}}`, str)
  31. }
  32. func Test_write_map_of_interface_in_struct_with_two_fields(t *testing.T) {
  33. type TestObject struct {
  34. Field map[string]interface{}
  35. Field2 string
  36. }
  37. should := require.New(t)
  38. val := TestObject{map[string]interface{}{"hello": "world"}, ""}
  39. str, err := MarshalToString(val)
  40. should.Nil(err)
  41. should.Contains(str, `"Field":{"hello":"world"}`)
  42. }
  43. type MyInterface interface {
  44. Hello() string
  45. }
  46. type MyString string
  47. func (ms MyString) Hello() string {
  48. return string(ms)
  49. }
  50. func Test_write_map_of_custom_interface(t *testing.T) {
  51. should := require.New(t)
  52. myStr := MyString("world")
  53. should.Equal("world", myStr.Hello())
  54. val := map[string]MyInterface{"hello": myStr}
  55. str, err := MarshalToString(val)
  56. should.Nil(err)
  57. should.Equal(`{"hello":"world"}`, str)
  58. }
  59. func Test_write_interface(t *testing.T) {
  60. should := require.New(t)
  61. var val interface{}
  62. val = "hello"
  63. str, err := MarshalToString(val)
  64. should.Nil(err)
  65. should.Equal(`"hello"`, str)
  66. }
  67. func Test_read_interface(t *testing.T) {
  68. should := require.New(t)
  69. var val interface{}
  70. err := UnmarshalFromString(`"hello"`, &val)
  71. should.Nil(err)
  72. should.Equal("hello", val)
  73. }
  74. func Test_read_custom_interface(t *testing.T) {
  75. should := require.New(t)
  76. var val MyInterface
  77. RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
  78. *((*MyInterface)(ptr)) = MyString(iter.ReadString())
  79. })
  80. err := UnmarshalFromString(`"hello"`, &val)
  81. should.Nil(err)
  82. should.Equal("hello", val.Hello())
  83. }
  84. func Test_decode_object_contain_empty_interface(t *testing.T) {
  85. type TestObject struct {
  86. Field interface{}
  87. }
  88. should := require.New(t)
  89. obj := TestObject{}
  90. obj.Field = 1024
  91. should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
  92. should.Equal("hello", obj.Field)
  93. }
  94. func Test_decode_object_contain_non_empty_interface(t *testing.T) {
  95. type TestObject struct {
  96. Field MyInterface
  97. }
  98. should := require.New(t)
  99. obj := TestObject{}
  100. obj.Field = MyString("abc")
  101. should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
  102. should.Equal(MyString("hello"), obj.Field)
  103. }
  104. func Test_encode_object_contain_empty_interface(t *testing.T) {
  105. type TestObject struct {
  106. Field interface{}
  107. }
  108. should := require.New(t)
  109. obj := TestObject{}
  110. obj.Field = 1024
  111. str, err := MarshalToString(obj)
  112. should.Nil(err)
  113. should.Equal(`{"Field":1024}`, str)
  114. }
  115. func Test_encode_object_contain_non_empty_interface(t *testing.T) {
  116. type TestObject struct {
  117. Field MyInterface
  118. }
  119. should := require.New(t)
  120. obj := TestObject{}
  121. obj.Field = MyString("hello")
  122. str, err := MarshalToString(obj)
  123. should.Nil(err)
  124. should.Equal(`{"Field":"hello"}`, str)
  125. }
  126. func Test_nil_non_empty_interface(t *testing.T) {
  127. ConfigDefault.cleanEncoders()
  128. ConfigDefault.cleanDecoders()
  129. type TestObject struct {
  130. Field []MyInterface
  131. }
  132. should := require.New(t)
  133. obj := TestObject{}
  134. b := []byte(`{"Field":["AAA"]}`)
  135. should.NotNil(json.Unmarshal(b, &obj))
  136. should.NotNil(Unmarshal(b, &obj))
  137. }
  138. func Test_read_large_number_as_interface(t *testing.T) {
  139. should := require.New(t)
  140. var val interface{}
  141. err := Config{UseNumber: true}.Froze().UnmarshalFromString(`123456789123456789123456789`, &val)
  142. should.Nil(err)
  143. output, err := MarshalToString(val)
  144. should.Nil(err)
  145. should.Equal(`123456789123456789123456789`, output)
  146. }
  147. func Test_nested_one_field_struct(t *testing.T) {
  148. should := require.New(t)
  149. type YetYetAnotherObject struct {
  150. Field string
  151. }
  152. type YetAnotherObject struct {
  153. Field *YetYetAnotherObject
  154. }
  155. type AnotherObject struct {
  156. Field *YetAnotherObject
  157. }
  158. type TestObject struct {
  159. Me *AnotherObject
  160. }
  161. obj := TestObject{&AnotherObject{&YetAnotherObject{&YetYetAnotherObject{"abc"}}}}
  162. str, err := MarshalToString(obj)
  163. should.Nil(err)
  164. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  165. str, err = MarshalToString(&obj)
  166. should.Nil(err)
  167. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  168. }
  169. func Test_struct_with_embedded_ptr_with_tag(t *testing.T) {
  170. type O1 struct {
  171. O1F string
  172. }
  173. type Option struct {
  174. O1 *O1
  175. }
  176. type T struct {
  177. Option `json:","`
  178. }
  179. var obj T
  180. should := require.New(t)
  181. output, err := MarshalToString(obj)
  182. should.Nil(err)
  183. should.Equal(`{"O1":null}`, output)
  184. }
  185. func Test_struct_with_one_nil(t *testing.T) {
  186. type TestObject struct {
  187. F *float64
  188. }
  189. var obj TestObject
  190. should := require.New(t)
  191. output, err := MarshalToString(obj)
  192. should.Nil(err)
  193. should.Equal(`{"F":null}`, output)
  194. }
  195. func Test_struct_with_one_nil_embedded(t *testing.T) {
  196. type Parent struct {
  197. Field1 string
  198. Field2 string
  199. }
  200. type TestObject struct {
  201. *Parent
  202. }
  203. obj := TestObject{}
  204. should := require.New(t)
  205. bytes, err := json.Marshal(obj)
  206. should.Nil(err)
  207. should.Equal("{}", string(bytes))
  208. output, err := MarshalToString(obj)
  209. should.Nil(err)
  210. should.Equal(`{}`, output)
  211. }
  212. func Test_struct_with_not_nil_embedded(t *testing.T) {
  213. type Parent struct {
  214. Field0 string
  215. Field1 []string
  216. Field2 map[string]interface{}
  217. }
  218. type TestObject struct {
  219. *Parent
  220. }
  221. should := require.New(t)
  222. var obj TestObject
  223. err := UnmarshalFromString(`{"Field0":"1","Field1":null,"Field2":{"K":"V"}}`, &obj)
  224. should.Nil(err)
  225. should.Nil(obj.Field1)
  226. should.Equal(map[string]interface{}{"K": "V"}, obj.Field2)
  227. should.Equal("1", obj.Field0)
  228. }
  229. func Test_array_with_one_nil_ptr(t *testing.T) {
  230. obj := [1]*float64{nil}
  231. should := require.New(t)
  232. output, err := MarshalToString(obj)
  233. should.Nil(err)
  234. should.Equal(`[null]`, output)
  235. }
  236. func Test_array_with_one_not_nil_ptr(t *testing.T) {
  237. two := float64(2)
  238. obj := [1]*float64{&two}
  239. should := require.New(t)
  240. output, err := MarshalToString(obj)
  241. should.Nil(err)
  242. should.Equal(`[2]`, output)
  243. }
  244. func Test_embedded_array_with_one_nil(t *testing.T) {
  245. type TestObject struct {
  246. Field1 int
  247. Field2 [1]*float64
  248. }
  249. var obj TestObject
  250. should := require.New(t)
  251. output, err := MarshalToString(obj)
  252. should.Nil(err)
  253. should.Contains(output, `"Field2":[null]`)
  254. }
  255. func Test_array_with_nothing(t *testing.T) {
  256. var obj [2]*float64
  257. should := require.New(t)
  258. output, err := MarshalToString(obj)
  259. should.Nil(err)
  260. should.Equal(`[null,null]`, output)
  261. }