...
1 package test
2
3 import (
4 "bytes"
5 "encoding/json"
6 "github.com/json-iterator/go"
7 "github.com/stretchr/testify/require"
8 "io/ioutil"
9 "testing"
10 )
11
12 func Test_disallowUnknownFields(t *testing.T) {
13 should := require.New(t)
14 type TestObject struct{}
15 var obj TestObject
16 decoder := jsoniter.NewDecoder(bytes.NewBufferString(`{"field1":100}`))
17 decoder.DisallowUnknownFields()
18 should.Error(decoder.Decode(&obj))
19 }
20
21 func Test_new_decoder(t *testing.T) {
22 should := require.New(t)
23 decoder1 := json.NewDecoder(bytes.NewBufferString(`[1][2]`))
24 decoder2 := jsoniter.NewDecoder(bytes.NewBufferString(`[1][2]`))
25 arr1 := []int{}
26 should.Nil(decoder1.Decode(&arr1))
27 should.Equal([]int{1}, arr1)
28 arr2 := []int{}
29 should.True(decoder1.More())
30 buffered, _ := ioutil.ReadAll(decoder1.Buffered())
31 should.Equal("[2]", string(buffered))
32 should.Nil(decoder2.Decode(&arr2))
33 should.Equal([]int{1}, arr2)
34 should.True(decoder2.More())
35 buffered, _ = ioutil.ReadAll(decoder2.Buffered())
36 should.Equal("[2]", string(buffered))
37
38 should.Nil(decoder1.Decode(&arr1))
39 should.Equal([]int{2}, arr1)
40 should.False(decoder1.More())
41 should.Nil(decoder2.Decode(&arr2))
42 should.Equal([]int{2}, arr2)
43 should.False(decoder2.More())
44 }
45
46 func Test_use_number(t *testing.T) {
47 should := require.New(t)
48 decoder1 := json.NewDecoder(bytes.NewBufferString(`123`))
49 decoder1.UseNumber()
50 decoder2 := jsoniter.NewDecoder(bytes.NewBufferString(`123`))
51 decoder2.UseNumber()
52 var obj1 interface{}
53 should.Nil(decoder1.Decode(&obj1))
54 should.Equal(json.Number("123"), obj1)
55 var obj2 interface{}
56 should.Nil(decoder2.Decode(&obj2))
57 should.Equal(json.Number("123"), obj2)
58 }
59
60 func Test_decoder_more(t *testing.T) {
61 should := require.New(t)
62 decoder := jsoniter.NewDecoder(bytes.NewBufferString("abcde"))
63 should.True(decoder.More())
64 }
65
View as plain text