jsoniter_invalid_test.go 5.1 KB

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