jsoniter_interface_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "github.com/json-iterator/go/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_one_nil(t *testing.T) {
  170. type TestObject struct {
  171. F *float64
  172. }
  173. var obj TestObject
  174. should := require.New(t)
  175. output, err := MarshalToString(obj)
  176. should.Nil(err)
  177. should.Equal(`{"F":null}`, output)
  178. }
  179. func Test_struct_with_one_nil_embedded(t *testing.T) {
  180. type Parent struct {
  181. Field1 string
  182. Field2 string
  183. }
  184. type TestObject struct {
  185. *Parent
  186. }
  187. obj := TestObject{}
  188. should := require.New(t)
  189. bytes, err := json.Marshal(obj)
  190. should.Nil(err)
  191. should.Equal("{}", string(bytes))
  192. output, err := MarshalToString(obj)
  193. should.Nil(err)
  194. should.Equal(`{}`, output)
  195. }
  196. func Test_struct_with_not_nil_embedded(t *testing.T) {
  197. type Parent struct {
  198. Field0 string
  199. Field1 []string
  200. Field2 map[string]interface{}
  201. }
  202. type TestObject struct {
  203. *Parent
  204. }
  205. should := require.New(t)
  206. var obj TestObject
  207. err := UnmarshalFromString(`{"Field0":"1","Field1":null,"Field2":{"K":"V"}}`, &obj)
  208. should.Nil(err)
  209. should.Nil(obj.Field1)
  210. should.Equal(map[string]interface{}{"K": "V"}, obj.Field2)
  211. should.Equal("1", obj.Field0)
  212. }
  213. func Test_array_with_one_nil_ptr(t *testing.T) {
  214. obj := [1]*float64{nil}
  215. should := require.New(t)
  216. output, err := MarshalToString(obj)
  217. should.Nil(err)
  218. should.Equal(`[null]`, output)
  219. }
  220. func Test_array_with_one_not_nil_ptr(t *testing.T) {
  221. two := float64(2)
  222. obj := [1]*float64{&two}
  223. should := require.New(t)
  224. output, err := MarshalToString(obj)
  225. should.Nil(err)
  226. should.Equal(`[2]`, output)
  227. }
  228. func Test_embedded_array_with_one_nil(t *testing.T) {
  229. type TestObject struct {
  230. Field1 int
  231. Field2 [1]*float64
  232. }
  233. var obj TestObject
  234. should := require.New(t)
  235. output, err := MarshalToString(obj)
  236. should.Nil(err)
  237. should.Contains(output, `"Field2":[null]`)
  238. }
  239. func Test_array_with_nothing(t *testing.T) {
  240. var obj [2]*float64
  241. should := require.New(t)
  242. output, err := MarshalToString(obj)
  243. should.Nil(err)
  244. should.Equal(`[null,null]`, output)
  245. }