invalid_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/json-iterator/go"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "io"
  9. "testing"
  10. )
  11. func Test_missing_object_end(t *testing.T) {
  12. should := require.New(t)
  13. type TestObject struct {
  14. Metric string `json:"metric"`
  15. Tags map[string]interface{} `json:"tags"`
  16. }
  17. obj := TestObject{}
  18. should.NotNil(jsoniter.UnmarshalFromString(`{"metric": "sys.777","tags": {"a":"123"}`, &obj))
  19. }
  20. func Test_missing_array_end(t *testing.T) {
  21. should := require.New(t)
  22. should.NotNil(jsoniter.UnmarshalFromString(`[1,2,3`, &[]int{}))
  23. }
  24. func Test_invalid_any(t *testing.T) {
  25. should := require.New(t)
  26. any := jsoniter.Get([]byte("[]"))
  27. should.Equal(jsoniter.InvalidValue, any.Get(0.3).ValueType())
  28. // is nil correct ?
  29. should.Equal(nil, any.Get(0.3).GetInterface())
  30. any = any.Get(0.3)
  31. should.Equal(false, any.ToBool())
  32. should.Equal(int(0), any.ToInt())
  33. should.Equal(int32(0), any.ToInt32())
  34. should.Equal(int64(0), any.ToInt64())
  35. should.Equal(uint(0), any.ToUint())
  36. should.Equal(uint32(0), any.ToUint32())
  37. should.Equal(uint64(0), any.ToUint64())
  38. should.Equal(float32(0), any.ToFloat32())
  39. should.Equal(float64(0), any.ToFloat64())
  40. should.Equal("", any.ToString())
  41. should.Equal(jsoniter.InvalidValue, any.Get(0.1).Get(1).ValueType())
  42. }
  43. func Test_invalid_struct_input(t *testing.T) {
  44. should := require.New(t)
  45. type TestObject struct{}
  46. input := []byte{54, 141, 30}
  47. obj := TestObject{}
  48. should.NotNil(jsoniter.Unmarshal(input, &obj))
  49. }
  50. func Test_invalid_slice_input(t *testing.T) {
  51. should := require.New(t)
  52. type TestObject struct{}
  53. input := []byte{93}
  54. obj := []string{}
  55. should.NotNil(jsoniter.Unmarshal(input, &obj))
  56. }
  57. func Test_invalid_array_input(t *testing.T) {
  58. should := require.New(t)
  59. type TestObject struct{}
  60. input := []byte{93}
  61. obj := [0]string{}
  62. should.NotNil(jsoniter.Unmarshal(input, &obj))
  63. }
  64. func Test_invalid_float(t *testing.T) {
  65. inputs := []string{
  66. `1.e1`, // dot without following digit
  67. `1.`, // dot can not be the last char
  68. ``, // empty number
  69. `01`, // extra leading zero
  70. `-`, // negative without digit
  71. `--`, // double negative
  72. `--2`, // double negative
  73. }
  74. for _, input := range inputs {
  75. t.Run(input, func(t *testing.T) {
  76. should := require.New(t)
  77. iter := jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
  78. iter.Skip()
  79. should.NotEqual(io.EOF, iter.Error)
  80. should.NotNil(iter.Error)
  81. v := float64(0)
  82. should.NotNil(json.Unmarshal([]byte(input), &v))
  83. iter = jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
  84. iter.ReadFloat64()
  85. should.NotEqual(io.EOF, iter.Error)
  86. should.NotNil(iter.Error)
  87. iter = jsoniter.ParseString(jsoniter.ConfigDefault, input+",")
  88. iter.ReadFloat32()
  89. should.NotEqual(io.EOF, iter.Error)
  90. should.NotNil(iter.Error)
  91. })
  92. }
  93. }
  94. func Test_chan(t *testing.T) {
  95. type TestObject struct {
  96. MyChan chan bool
  97. MyField int
  98. }
  99. obj := TestObject{}
  100. t.Run("Encode channel", func(t *testing.T) {
  101. should := require.New(t)
  102. str, err := jsoniter.Marshal(obj)
  103. should.NotNil(err)
  104. should.Nil(str)
  105. })
  106. t.Run("Encode channel using compatible configuration", func(t *testing.T) {
  107. should := require.New(t)
  108. str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(obj)
  109. should.NotNil(err)
  110. should.Nil(str)
  111. })
  112. }
  113. func Test_invalid_in_map(t *testing.T) {
  114. testMap := map[string]interface{}{"chan": make(chan interface{})}
  115. t.Run("Encode map with invalid content", func(t *testing.T) {
  116. should := require.New(t)
  117. str, err := jsoniter.Marshal(testMap)
  118. should.NotNil(err)
  119. should.Nil(str)
  120. })
  121. t.Run("Encode map with invalid content using compatible configuration", func(t *testing.T) {
  122. should := require.New(t)
  123. str, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(testMap)
  124. should.NotNil(err)
  125. should.Nil(str)
  126. })
  127. }
  128. func Test_invalid_number(t *testing.T) {
  129. type Message struct {
  130. Number int `json:"number"`
  131. }
  132. obj := Message{}
  133. decoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
  134. err := decoder.Decode(&obj)
  135. invalidStr := err.Error()
  136. result, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(invalidStr)
  137. should := require.New(t)
  138. should.Nil(err)
  139. result2, err := json.Marshal(invalidStr)
  140. should.Nil(err)
  141. should.Equal(string(result2), string(result))
  142. }
  143. func Test_valid(t *testing.T) {
  144. should := require.New(t)
  145. should.True(jsoniter.Valid([]byte(`{}`)))
  146. should.False(jsoniter.Valid([]byte(`{`)))
  147. }
  148. func Test_nil_pointer(t *testing.T) {
  149. should := require.New(t)
  150. data := []byte(`{"A":0}`)
  151. type T struct {
  152. X int
  153. }
  154. var obj *T
  155. err := jsoniter.Unmarshal(data, obj)
  156. should.NotNil(err)
  157. }
  158. func Test_func_pointer_type(t *testing.T) {
  159. type TestObject2 struct {
  160. F func()
  161. }
  162. type TestObject1 struct {
  163. Obj *TestObject2
  164. }
  165. t.Run("encode null is valid", func(t *testing.T) {
  166. should := require.New(t)
  167. output, err := json.Marshal(TestObject1{})
  168. should.Nil(err)
  169. should.Equal(`{"Obj":null}`, string(output))
  170. output, err = jsoniter.Marshal(TestObject1{})
  171. should.Nil(err)
  172. should.Equal(`{"Obj":null}`, string(output))
  173. })
  174. t.Run("encode not null is invalid", func(t *testing.T) {
  175. should := require.New(t)
  176. _, err := json.Marshal(TestObject1{Obj: &TestObject2{}})
  177. should.NotNil(err)
  178. _, err = jsoniter.Marshal(TestObject1{Obj: &TestObject2{}})
  179. should.NotNil(err)
  180. })
  181. t.Run("decode null is valid", func(t *testing.T) {
  182. should := require.New(t)
  183. var obj TestObject1
  184. should.Nil(json.Unmarshal([]byte(`{"Obj":{"F": null}}`), &obj))
  185. should.Nil(jsoniter.Unmarshal([]byte(`{"Obj":{"F": null}}`), &obj))
  186. })
  187. t.Run("decode not null is invalid", func(t *testing.T) {
  188. should := require.New(t)
  189. var obj TestObject1
  190. should.NotNil(json.Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
  191. should.NotNil(jsoniter.Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
  192. })
  193. }
  194. func TestEOF(t *testing.T) {
  195. var s string
  196. err := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
  197. assert.Equal(t, io.EOF, err)
  198. }
  199. func TestDecodeErrorType(t *testing.T) {
  200. should := require.New(t)
  201. var err error
  202. should.Nil(jsoniter.Unmarshal([]byte("null"), &err))
  203. should.NotNil(jsoniter.Unmarshal([]byte("123"), &err))
  204. }
  205. func Test_decode_slash(t *testing.T) {
  206. should := require.New(t)
  207. var obj interface{}
  208. should.NotNil(json.Unmarshal([]byte("\\"), &obj))
  209. should.NotNil(jsoniter.UnmarshalFromString("\\", &obj))
  210. }
  211. func Test_NilInput(t *testing.T) {
  212. var jb []byte // nil
  213. var out string
  214. err := jsoniter.Unmarshal(jb, &out)
  215. if err == nil {
  216. t.Errorf("Expected error")
  217. }
  218. }
  219. func Test_EmptyInput(t *testing.T) {
  220. jb := []byte("")
  221. var out string
  222. err := jsoniter.Unmarshal(jb, &out)
  223. if err == nil {
  224. t.Errorf("Expected error")
  225. }
  226. }
  227. type Foo struct {
  228. A jsoniter.Any
  229. }
  230. func Test_nil_any(t *testing.T) {
  231. should := require.New(t)
  232. data, _ := jsoniter.Marshal(&Foo{})
  233. should.Equal(`{"A":null}`, string(data))
  234. }