jsoniter_interface_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. err = UnmarshalFromString(`1e1`, &val)
  74. should.Nil(err)
  75. should.Equal(float64(10), val)
  76. err = UnmarshalFromString(`1.0e1`, &val)
  77. should.Nil(err)
  78. should.Equal(float64(10), val)
  79. err = json.Unmarshal([]byte(`1.0e1`), &val)
  80. should.Nil(err)
  81. should.Equal(float64(10), val)
  82. }
  83. func Test_read_custom_interface(t *testing.T) {
  84. should := require.New(t)
  85. var val MyInterface
  86. RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *Iterator) {
  87. *((*MyInterface)(ptr)) = MyString(iter.ReadString())
  88. })
  89. err := UnmarshalFromString(`"hello"`, &val)
  90. should.Nil(err)
  91. should.Equal("hello", val.Hello())
  92. }
  93. func Test_decode_object_contain_empty_interface(t *testing.T) {
  94. type TestObject struct {
  95. Field interface{}
  96. }
  97. should := require.New(t)
  98. obj := TestObject{}
  99. obj.Field = 1024
  100. should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
  101. should.Equal("hello", obj.Field)
  102. }
  103. func Test_decode_object_contain_non_empty_interface(t *testing.T) {
  104. type TestObject struct {
  105. Field MyInterface
  106. }
  107. should := require.New(t)
  108. obj := TestObject{}
  109. obj.Field = MyString("abc")
  110. should.Nil(UnmarshalFromString(`{"Field": "hello"}`, &obj))
  111. should.Equal(MyString("hello"), obj.Field)
  112. }
  113. func Test_encode_object_contain_empty_interface(t *testing.T) {
  114. type TestObject struct {
  115. Field interface{}
  116. }
  117. should := require.New(t)
  118. obj := TestObject{}
  119. obj.Field = 1024
  120. str, err := MarshalToString(obj)
  121. should.Nil(err)
  122. should.Equal(`{"Field":1024}`, str)
  123. }
  124. func Test_encode_object_contain_non_empty_interface(t *testing.T) {
  125. type TestObject struct {
  126. Field MyInterface
  127. }
  128. should := require.New(t)
  129. obj := TestObject{}
  130. obj.Field = MyString("hello")
  131. str, err := MarshalToString(obj)
  132. should.Nil(err)
  133. should.Equal(`{"Field":"hello"}`, str)
  134. }
  135. func Test_nil_non_empty_interface(t *testing.T) {
  136. ConfigDefault.(*frozenConfig).cleanEncoders()
  137. ConfigDefault.(*frozenConfig).cleanDecoders()
  138. type TestObject struct {
  139. Field []MyInterface
  140. }
  141. should := require.New(t)
  142. obj := TestObject{}
  143. b := []byte(`{"Field":["AAA"]}`)
  144. should.NotNil(json.Unmarshal(b, &obj))
  145. should.NotNil(Unmarshal(b, &obj))
  146. }
  147. func Test_read_large_number_as_interface(t *testing.T) {
  148. should := require.New(t)
  149. var val interface{}
  150. err := Config{UseNumber: true}.Froze().UnmarshalFromString(`123456789123456789123456789`, &val)
  151. should.Nil(err)
  152. output, err := MarshalToString(val)
  153. should.Nil(err)
  154. should.Equal(`123456789123456789123456789`, output)
  155. }
  156. func Test_nested_one_field_struct(t *testing.T) {
  157. should := require.New(t)
  158. type YetYetAnotherObject struct {
  159. Field string
  160. }
  161. type YetAnotherObject struct {
  162. Field *YetYetAnotherObject
  163. }
  164. type AnotherObject struct {
  165. Field *YetAnotherObject
  166. }
  167. type TestObject struct {
  168. Me *AnotherObject
  169. }
  170. obj := TestObject{&AnotherObject{&YetAnotherObject{&YetYetAnotherObject{"abc"}}}}
  171. str, err := MarshalToString(obj)
  172. should.Nil(err)
  173. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  174. str, err = MarshalToString(&obj)
  175. should.Nil(err)
  176. should.Equal(`{"Me":{"Field":{"Field":{"Field":"abc"}}}}`, str)
  177. }
  178. func Test_struct_with_embedded_ptr_with_tag(t *testing.T) {
  179. type O1 struct {
  180. O1F string
  181. }
  182. type Option struct {
  183. O1 *O1
  184. }
  185. type T struct {
  186. Option `json:","`
  187. }
  188. var obj T
  189. should := require.New(t)
  190. output, err := MarshalToString(obj)
  191. should.Nil(err)
  192. should.Equal(`{"O1":null}`, output)
  193. }
  194. func Test_struct_with_one_nil(t *testing.T) {
  195. type TestObject struct {
  196. F *float64
  197. }
  198. var obj TestObject
  199. should := require.New(t)
  200. output, err := MarshalToString(obj)
  201. should.Nil(err)
  202. should.Equal(`{"F":null}`, output)
  203. }
  204. func Test_struct_with_one_nil_embedded(t *testing.T) {
  205. type Parent struct {
  206. Field1 string
  207. Field2 string
  208. }
  209. type TestObject struct {
  210. *Parent
  211. }
  212. obj := TestObject{}
  213. should := require.New(t)
  214. bytes, err := json.Marshal(obj)
  215. should.Nil(err)
  216. should.Equal("{}", string(bytes))
  217. output, err := MarshalToString(obj)
  218. should.Nil(err)
  219. should.Equal(`{}`, output)
  220. }
  221. func Test_struct_with_not_nil_embedded(t *testing.T) {
  222. type Parent struct {
  223. Field0 string
  224. Field1 []string
  225. Field2 map[string]interface{}
  226. }
  227. type TestObject struct {
  228. *Parent
  229. }
  230. should := require.New(t)
  231. var obj TestObject
  232. err := UnmarshalFromString(`{"Field0":"1","Field1":null,"Field2":{"K":"V"}}`, &obj)
  233. should.Nil(err)
  234. should.Nil(obj.Field1)
  235. should.Equal(map[string]interface{}{"K": "V"}, obj.Field2)
  236. should.Equal("1", obj.Field0)
  237. }
  238. func Test_array_with_one_nil_ptr(t *testing.T) {
  239. obj := [1]*float64{nil}
  240. should := require.New(t)
  241. output, err := MarshalToString(obj)
  242. should.Nil(err)
  243. should.Equal(`[null]`, output)
  244. }
  245. func Test_array_with_one_not_nil_ptr(t *testing.T) {
  246. two := float64(2)
  247. obj := [1]*float64{&two}
  248. should := require.New(t)
  249. output, err := MarshalToString(obj)
  250. should.Nil(err)
  251. should.Equal(`[2]`, output)
  252. }
  253. func Test_embedded_array_with_one_nil(t *testing.T) {
  254. type TestObject struct {
  255. Field1 int
  256. Field2 [1]*float64
  257. }
  258. var obj TestObject
  259. should := require.New(t)
  260. output, err := MarshalToString(obj)
  261. should.Nil(err)
  262. should.Contains(output, `"Field2":[null]`)
  263. }
  264. func Test_array_with_nothing(t *testing.T) {
  265. var obj [2]*float64
  266. should := require.New(t)
  267. output, err := MarshalToString(obj)
  268. should.Nil(err)
  269. should.Equal(`[null,null]`, output)
  270. }