invalid_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. t.Skip("do not support chan")
  96. type TestObject struct {
  97. MyChan chan bool
  98. MyField int
  99. }
  100. should := require.New(t)
  101. obj := TestObject{}
  102. str, err := json.Marshal(obj)
  103. should.Nil(err)
  104. should.Equal(``, str)
  105. }
  106. func Test_invalid_number(t *testing.T) {
  107. type Message struct {
  108. Number int `json:"number"`
  109. }
  110. obj := Message{}
  111. decoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(bytes.NewBufferString(`{"number":"5"}`))
  112. err := decoder.Decode(&obj)
  113. invalidStr := err.Error()
  114. result, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(invalidStr)
  115. should := require.New(t)
  116. should.Nil(err)
  117. result2, err := json.Marshal(invalidStr)
  118. should.Nil(err)
  119. should.Equal(string(result2), string(result))
  120. }
  121. func Test_valid(t *testing.T) {
  122. should := require.New(t)
  123. should.True(jsoniter.Valid([]byte(`{}`)))
  124. should.False(jsoniter.Valid([]byte(`{`)))
  125. }
  126. func Test_nil_pointer(t *testing.T) {
  127. should := require.New(t)
  128. data := []byte(`{"A":0}`)
  129. type T struct {
  130. X int
  131. }
  132. var obj *T
  133. err := jsoniter.Unmarshal(data, obj)
  134. should.NotNil(err)
  135. }
  136. func Test_func_pointer_type(t *testing.T) {
  137. type TestObject2 struct {
  138. F func()
  139. }
  140. type TestObject1 struct {
  141. Obj *TestObject2
  142. }
  143. t.Run("encode null is valid", func(t *testing.T) {
  144. should := require.New(t)
  145. output, err := json.Marshal(TestObject1{})
  146. should.Nil(err)
  147. should.Equal(`{"Obj":null}`, string(output))
  148. output, err = jsoniter.Marshal(TestObject1{})
  149. should.Nil(err)
  150. should.Equal(`{"Obj":null}`, string(output))
  151. })
  152. t.Run("encode not null is invalid", func(t *testing.T) {
  153. should := require.New(t)
  154. _, err := json.Marshal(TestObject1{Obj: &TestObject2{}})
  155. should.NotNil(err)
  156. _, err = jsoniter.Marshal(TestObject1{Obj: &TestObject2{}})
  157. should.NotNil(err)
  158. })
  159. t.Run("decode null is valid", func(t *testing.T) {
  160. should := require.New(t)
  161. var obj TestObject1
  162. should.Nil(json.Unmarshal([]byte(`{"Obj":{"F": null}}`), &obj))
  163. should.Nil(jsoniter.Unmarshal([]byte(`{"Obj":{"F": null}}`), &obj))
  164. })
  165. t.Run("decode not null is invalid", func(t *testing.T) {
  166. should := require.New(t)
  167. var obj TestObject1
  168. should.NotNil(json.Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
  169. should.NotNil(jsoniter.Unmarshal([]byte(`{"Obj":{"F": "hello"}}`), &obj))
  170. })
  171. }
  172. func TestEOF(t *testing.T) {
  173. var s string
  174. err := jsoniter.ConfigCompatibleWithStandardLibrary.NewDecoder(&bytes.Buffer{}).Decode(&s)
  175. assert.Equal(t, io.EOF, err)
  176. }
  177. func TestDecodeErrorType(t *testing.T) {
  178. should := require.New(t)
  179. var err error
  180. should.Nil(jsoniter.Unmarshal([]byte("null"), &err))
  181. should.NotNil(jsoniter.Unmarshal([]byte("123"), &err))
  182. }
  183. func Test_decode_slash(t *testing.T) {
  184. should := require.New(t)
  185. var obj interface{}
  186. should.NotNil(json.Unmarshal([]byte("\\"), &obj))
  187. should.NotNil(jsoniter.UnmarshalFromString("\\", &obj))
  188. }
  189. func Test_NilInput(t *testing.T) {
  190. var jb []byte // nil
  191. var out string
  192. err := jsoniter.Unmarshal(jb, &out)
  193. if err == nil {
  194. t.Errorf("Expected error")
  195. }
  196. }
  197. func Test_EmptyInput(t *testing.T) {
  198. jb := []byte("")
  199. var out string
  200. err := jsoniter.Unmarshal(jb, &out)
  201. if err == nil {
  202. t.Errorf("Expected error")
  203. }
  204. }
  205. type Foo struct {
  206. A jsoniter.Any
  207. }
  208. func Test_nil_any(t *testing.T) {
  209. should := require.New(t)
  210. data, _ := jsoniter.Marshal(&Foo{})
  211. should.Equal(`{"A":null}`, string(data))
  212. }