| 1 |
package misc_tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/json-iterator/go" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
) |
| 11 |
|
| 12 |
func Test_empty_array(t *testing.T) { |
| 13 |
should := require.New(t) |
| 14 |
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[]`) |
| 15 |
cont := iter.ReadArray() |
| 16 |
should.False(cont) |
| 17 |
iter = jsoniter.ParseString(jsoniter.ConfigDefault, `[]`) |
| 18 |
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool { |
| 19 |
should.FailNow("should not call") |
| 20 |
return true |
| 21 |
}) |
| 22 |
} |
| 23 |
|
| 24 |
func Test_one_element(t *testing.T) { |
| 25 |
should := require.New(t) |
| 26 |
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[1]`) |
| 27 |
should.True(iter.ReadArray()) |
| 28 |
should.Equal(1, iter.ReadInt()) |
| 29 |
should.False(iter.ReadArray()) |
| 30 |
iter = jsoniter.ParseString(jsoniter.ConfigDefault, `[1]`) |
| 31 |
iter.ReadArrayCB(func(iter *jsoniter.Iterator) bool { |
| 32 |
should.Equal(1, iter.ReadInt()) |
| 33 |
return true |
| 34 |
}) |
| 35 |
} |
| 36 |
|
| 37 |
func Test_two_elements(t *testing.T) { |
| 38 |
should := require.New(t) |
| 39 |
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[1,2]`) |
| 40 |
should.True(iter.ReadArray()) |
| 41 |
should.Equal(int64(1), iter.ReadInt64()) |
| 42 |
should.True(iter.ReadArray()) |
| 43 |
should.Equal(int64(2), iter.ReadInt64()) |
| 44 |
should.False(iter.ReadArray()) |
| 45 |
iter = jsoniter.ParseString(jsoniter.ConfigDefault, `[1,2]`) |
| 46 |
should.Equal([]interface{}{float64(1), float64(2)}, iter.Read()) |
| 47 |
} |
| 48 |
|
| 49 |
func Test_whitespace_in_head(t *testing.T) { |
| 50 |
iter := jsoniter.ParseString(jsoniter.ConfigDefault, ` [1]`) |
| 51 |
cont := iter.ReadArray() |
| 52 |
if cont != true { |
| 53 |
t.FailNow() |
| 54 |
} |
| 55 |
if iter.ReadUint64() != 1 { |
| 56 |
t.FailNow() |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func Test_whitespace_after_array_start(t *testing.T) { |
| 61 |
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[ 1]`) |
| 62 |
cont := iter.ReadArray() |
| 63 |
if cont != true { |
| 64 |
t.FailNow() |
| 65 |
} |
| 66 |
if iter.ReadUint64() != 1 { |
| 67 |
t.FailNow() |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func Test_whitespace_before_array_end(t *testing.T) { |
| 72 |
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[1 ]`) |
| 73 |
cont := iter.ReadArray() |
| 74 |
if cont != true { |
| 75 |
t.FailNow() |
| 76 |
} |
| 77 |
if iter.ReadUint64() != 1 { |
| 78 |
t.FailNow() |
| 79 |
} |
| 80 |
cont = iter.ReadArray() |
| 81 |
if cont != false { |
| 82 |
t.FailNow() |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func Test_whitespace_before_comma(t *testing.T) { |
| 87 |
iter := jsoniter.ParseString(jsoniter.ConfigDefault, `[1 ,2]`) |
| 88 |
cont := iter.ReadArray() |
| 89 |
if cont != true { |
| 90 |
t.FailNow() |
| 91 |
} |
| 92 |
if iter.ReadUint64() != 1 { |
| 93 |
t.FailNow() |
| 94 |
} |
| 95 |
cont = iter.ReadArray() |
| 96 |
if cont != true { |
| 97 |
t.FailNow() |
| 98 |
} |
| 99 |
if iter.ReadUint64() != 2 { |
| 100 |
t.FailNow() |
| 101 |
} |
| 102 |
cont = iter.ReadArray() |
| 103 |
if cont != false { |
| 104 |
t.FailNow() |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func Test_write_array(t *testing.T) { |
| 109 |
should := require.New(t) |
| 110 |
buf := &bytes.Buffer{} |
| 111 |
stream := jsoniter.NewStream(jsoniter.Config{IndentionStep: 2}.Froze(), buf, 4096) |
| 112 |
stream.WriteArrayStart() |
| 113 |
stream.WriteInt(1) |
| 114 |
stream.WriteMore() |
| 115 |
stream.WriteInt(2) |
| 116 |
stream.WriteArrayEnd() |
| 117 |
stream.Flush() |
| 118 |
should.Nil(stream.Error) |
| 119 |
should.Equal("[\n 1,\n 2\n]", buf.String()) |
| 120 |
} |
| 121 |
|
| 122 |
func Test_write_val_array(t *testing.T) { |
| 123 |
should := require.New(t) |
| 124 |
val := []int{1, 2, 3} |
| 125 |
str, err := jsoniter.MarshalToString(&val) |
| 126 |
should.Nil(err) |
| 127 |
should.Equal("[1,2,3]", str) |
| 128 |
} |
| 129 |
|
| 130 |
func Test_write_val_empty_array(t *testing.T) { |
| 131 |
should := require.New(t) |
| 132 |
val := []int{} |
| 133 |
str, err := jsoniter.MarshalToString(val) |
| 134 |
should.Nil(err) |
| 135 |
should.Equal("[]", str) |
| 136 |
} |
| 137 |
|
| 138 |
func Test_write_array_of_interface_in_struct(t *testing.T) { |
| 139 |
should := require.New(t) |
| 140 |
type TestObject struct { |
| 141 |
Field []interface{} |
| 142 |
Field2 string |
| 143 |
} |
| 144 |
val := TestObject{[]interface{}{1, 2}, ""} |
| 145 |
str, err := jsoniter.MarshalToString(val) |
| 146 |
should.Nil(err) |
| 147 |
should.Contains(str, `"Field":[1,2]`) |
| 148 |
should.Contains(str, `"Field2":""`) |
| 149 |
} |
| 150 |
|
| 151 |
func Test_encode_byte_array(t *testing.T) { |
| 152 |
should := require.New(t) |
| 153 |
bytes, err := json.Marshal([]byte{1, 2, 3}) |
| 154 |
should.Nil(err) |
| 155 |
should.Equal(`"AQID"`, string(bytes)) |
| 156 |
bytes, err = jsoniter.Marshal([]byte{1, 2, 3}) |
| 157 |
should.Nil(err) |
| 158 |
should.Equal(`"AQID"`, string(bytes)) |
| 159 |
} |
| 160 |
|
| 161 |
func Test_encode_empty_byte_array(t *testing.T) { |
| 162 |
should := require.New(t) |
| 163 |
bytes, err := json.Marshal([]byte{}) |
| 164 |
should.Nil(err) |
| 165 |
should.Equal(`""`, string(bytes)) |
| 166 |
bytes, err = jsoniter.Marshal([]byte{}) |
| 167 |
should.Nil(err) |
| 168 |
should.Equal(`""`, string(bytes)) |
| 169 |
} |
| 170 |
|
| 171 |
func Test_encode_nil_byte_array(t *testing.T) { |
| 172 |
should := require.New(t) |
| 173 |
var nilSlice []byte |
| 174 |
bytes, err := json.Marshal(nilSlice) |
| 175 |
should.Nil(err) |
| 176 |
should.Equal(`null`, string(bytes)) |
| 177 |
bytes, err = jsoniter.Marshal(nilSlice) |
| 178 |
should.Nil(err) |
| 179 |
should.Equal(`null`, string(bytes)) |
| 180 |
} |
| 181 |
|
| 182 |
func Test_decode_byte_array_from_base64(t *testing.T) { |
| 183 |
should := require.New(t) |
| 184 |
data := []byte{} |
| 185 |
err := json.Unmarshal([]byte(`"AQID"`), &data) |
| 186 |
should.Nil(err) |
| 187 |
should.Equal([]byte{1, 2, 3}, data) |
| 188 |
err = jsoniter.Unmarshal([]byte(`"AQID"`), &data) |
| 189 |
should.Nil(err) |
| 190 |
should.Equal([]byte{1, 2, 3}, data) |
| 191 |
} |
| 192 |
|
| 193 |
func Test_decode_byte_array_from_base64_with_newlines(t *testing.T) { |
| 194 |
should := require.New(t) |
| 195 |
data := []byte{} |
| 196 |
err := json.Unmarshal([]byte(`"A\rQ\nID"`), &data) |
| 197 |
should.Nil(err) |
| 198 |
should.Equal([]byte{1, 2, 3}, data) |
| 199 |
err = jsoniter.Unmarshal([]byte(`"A\rQ\nID"`), &data) |
| 200 |
should.Nil(err) |
| 201 |
should.Equal([]byte{1, 2, 3}, data) |
| 202 |
} |
| 203 |
|
| 204 |
func Test_decode_byte_array_from_array(t *testing.T) { |
| 205 |
should := require.New(t) |
| 206 |
data := []byte{} |
| 207 |
err := json.Unmarshal([]byte(`[1,2,3]`), &data) |
| 208 |
should.Nil(err) |
| 209 |
should.Equal([]byte{1, 2, 3}, data) |
| 210 |
err = jsoniter.Unmarshal([]byte(`[1,2,3]`), &data) |
| 211 |
should.Nil(err) |
| 212 |
should.Equal([]byte{1, 2, 3}, data) |
| 213 |
} |
| 214 |
|
| 215 |
func Test_decode_slice(t *testing.T) { |
| 216 |
should := require.New(t) |
| 217 |
slice := make([]string, 0, 5) |
| 218 |
jsoniter.UnmarshalFromString(`["hello", "world"]`, &slice) |
| 219 |
should.Equal([]string{"hello", "world"}, slice) |
| 220 |
} |
| 221 |
|
| 222 |
func Test_decode_large_slice(t *testing.T) { |
| 223 |
should := require.New(t) |
| 224 |
slice := make([]int, 0, 1) |
| 225 |
jsoniter.UnmarshalFromString(`[1,2,3,4,5,6,7,8,9]`, &slice) |
| 226 |
should.Equal([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, slice) |
| 227 |
} |
| 228 |
|
| 229 |
func Benchmark_jsoniter_array(b *testing.B) { |
| 230 |
b.ReportAllocs() |
| 231 |
input := []byte(`[1,2,3,4,5,6,7,8,9]`) |
| 232 |
iter := jsoniter.ParseBytes(jsoniter.ConfigDefault, input) |
| 233 |
b.ResetTimer() |
| 234 |
for n := 0; n < b.N; n++ { |
| 235 |
iter.ResetBytes(input) |
| 236 |
for iter.ReadArray() { |
| 237 |
iter.ReadUint64() |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
func Benchmark_json_array(b *testing.B) { |
| 243 |
for n := 0; n < b.N; n++ { |
| 244 |
result := []interface{}{} |
| 245 |
json.Unmarshal([]byte(`[1,2,3]`), &result) |
| 246 |
} |
| 247 |
} |