2016-12-01 10:11:07 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-01-05 15:23:08 +02:00
|
|
|
"testing"
|
2017-01-07 16:08:45 +02:00
|
|
|
"github.com/json-iterator/go/require"
|
|
|
|
"bytes"
|
2016-12-01 10:11:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_empty_object(t *testing.T) {
|
2017-01-20 06:56:49 +02:00
|
|
|
should := require.New(t)
|
2016-12-01 10:11:07 +02:00
|
|
|
iter := ParseString(`{}`)
|
|
|
|
field := iter.ReadObject()
|
2017-01-20 06:56:49 +02:00
|
|
|
should.Equal("", field)
|
|
|
|
iter = ParseString(`{}`)
|
|
|
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
|
|
|
should.FailNow("should not call")
|
|
|
|
return true
|
|
|
|
})
|
2016-12-01 10:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_one_field(t *testing.T) {
|
2017-01-20 06:56:49 +02:00
|
|
|
should := require.New(t)
|
2016-12-01 10:11:07 +02:00
|
|
|
iter := ParseString(`{"a": "b"}`)
|
|
|
|
field := iter.ReadObject()
|
2017-01-20 06:56:49 +02:00
|
|
|
should.Equal("a", field)
|
2016-12-01 10:11:07 +02:00
|
|
|
value := iter.ReadString()
|
2017-01-20 06:56:49 +02:00
|
|
|
should.Equal("b", value)
|
2016-12-01 10:11:07 +02:00
|
|
|
field = iter.ReadObject()
|
2017-01-20 06:56:49 +02:00
|
|
|
should.Equal("", field)
|
|
|
|
iter = ParseString(`{"a": "b"}`)
|
|
|
|
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
|
|
|
should.Equal("a", field)
|
|
|
|
return true
|
|
|
|
}))
|
2016-12-01 10:11:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_two_field(t *testing.T) {
|
2017-01-23 18:23:07 +02:00
|
|
|
should := require.New(t)
|
2016-12-01 10:11:07 +02:00
|
|
|
iter := ParseString(`{ "a": "b" , "c": "d" }`)
|
|
|
|
field := iter.ReadObject()
|
2017-01-23 18:23:07 +02:00
|
|
|
should.Equal("a", field)
|
2016-12-01 10:11:07 +02:00
|
|
|
value := iter.ReadString()
|
2017-01-23 18:23:07 +02:00
|
|
|
should.Equal("b", value)
|
2016-12-01 10:11:07 +02:00
|
|
|
field = iter.ReadObject()
|
2017-01-23 18:23:07 +02:00
|
|
|
should.Equal("c", field)
|
2016-12-01 10:11:07 +02:00
|
|
|
value = iter.ReadString()
|
2017-01-23 18:23:07 +02:00
|
|
|
should.Equal("d", value)
|
2016-12-01 10:11:07 +02:00
|
|
|
field = iter.ReadObject()
|
2017-01-23 18:23:07 +02:00
|
|
|
should.Equal("", field)
|
2016-12-01 10:11:07 +02:00
|
|
|
iter = ParseString(`{"field1": "1", "field2": 2}`)
|
|
|
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
|
|
|
switch field {
|
|
|
|
case "field1":
|
|
|
|
iter.ReadString()
|
|
|
|
case "field2":
|
|
|
|
iter.ReadInt64()
|
|
|
|
default:
|
2017-01-05 15:23:08 +02:00
|
|
|
iter.reportError("bind object", "unexpected field")
|
2016-12-01 10:11:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:23:07 +02:00
|
|
|
func Test_read_object_as_any(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
any, err := UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal(`{"a":"b","c":"d"}`, any.ToString())
|
|
|
|
// partial parse
|
|
|
|
should.Equal("b", any.Get("a").ToString())
|
|
|
|
should.Equal("d", any.Get("c").ToString())
|
|
|
|
should.Equal(2, len(any.Keys()))
|
|
|
|
any, err = UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
|
|
|
|
// full parse
|
|
|
|
should.Equal(2, len(any.Keys()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_object_any_lazy_iterator(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
any, err := UnmarshalAnyFromString(`{"a":"b","c":"d"}`)
|
|
|
|
should.Nil(err)
|
|
|
|
// iterator parse
|
|
|
|
vals := map[string]string{}
|
|
|
|
var k string
|
|
|
|
var v Any
|
|
|
|
next, hasNext := any.IterateObject()
|
|
|
|
should.True(hasNext)
|
|
|
|
|
|
|
|
k, v, hasNext = next()
|
|
|
|
should.True(hasNext)
|
|
|
|
vals[k] = v.ToString()
|
|
|
|
|
|
|
|
// trigger full parse
|
|
|
|
should.Equal(2, len(any.Keys()))
|
|
|
|
|
|
|
|
k, v, hasNext = next()
|
|
|
|
should.False(hasNext)
|
|
|
|
vals[k] = v.ToString()
|
|
|
|
|
|
|
|
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
|
|
|
|
vals = map[string]string{}
|
|
|
|
for next, hasNext := any.IterateObject(); hasNext; k, v, hasNext = next() {
|
|
|
|
vals[k] = v.ToString()
|
|
|
|
}
|
|
|
|
should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Test_object_any_with_two_lazy_iterators(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
any, err := UnmarshalAnyFromString(`{"a":"b","c":"d","e":"f"}`)
|
|
|
|
should.Nil(err)
|
|
|
|
var k string
|
|
|
|
var v Any
|
|
|
|
next1, hasNext1 := any.IterateObject()
|
|
|
|
next2, hasNext2 := any.IterateObject()
|
|
|
|
should.True(hasNext1)
|
|
|
|
k, v, hasNext1 = next1()
|
|
|
|
should.True(hasNext1)
|
|
|
|
should.Equal("a", k)
|
|
|
|
should.Equal("b", v.ToString())
|
|
|
|
|
|
|
|
should.True(hasNext2)
|
|
|
|
k, v, hasNext2 = next2()
|
|
|
|
should.True(hasNext2)
|
|
|
|
should.Equal("a", k)
|
|
|
|
should.Equal("b", v.ToString())
|
|
|
|
|
|
|
|
k, v, hasNext1 = next1()
|
|
|
|
should.True(hasNext1)
|
|
|
|
should.Equal("c", k)
|
|
|
|
should.Equal("d", v.ToString())
|
|
|
|
|
|
|
|
k, v, hasNext2 = next2()
|
|
|
|
should.True(hasNext2)
|
|
|
|
should.Equal("c", k)
|
|
|
|
should.Equal("d", v.ToString())
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:08:45 +02:00
|
|
|
func Test_write_object(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
stream := NewStream(buf, 4096)
|
|
|
|
stream.IndentionStep = 2
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
stream.WriteObjectField("hello")
|
|
|
|
stream.WriteInt(1)
|
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField("world")
|
|
|
|
stream.WriteInt(2)
|
|
|
|
stream.WriteObjectEnd()
|
|
|
|
stream.Flush()
|
|
|
|
should.Nil(stream.Error)
|
2017-01-20 06:40:52 +02:00
|
|
|
should.Equal("{\n \"hello\":1,\n \"world\":2\n}", buf.String())
|
2017-01-07 16:08:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 10:11:07 +02:00
|
|
|
type TestObj struct {
|
|
|
|
Field1 string
|
|
|
|
Field2 uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_jsoniter_object(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
2016-12-06 15:10:09 +02:00
|
|
|
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
|
|
|
obj := TestObj{}
|
|
|
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
|
|
|
switch field {
|
|
|
|
case "field1":
|
|
|
|
obj.Field1 = iter.ReadString()
|
|
|
|
case "field2":
|
|
|
|
obj.Field2 = iter.ReadUint64()
|
|
|
|
default:
|
2017-01-05 15:23:08 +02:00
|
|
|
iter.reportError("bind object", "unexpected field")
|
2016-12-06 15:10:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-04 05:06:38 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 15:10:09 +02:00
|
|
|
|
2016-12-01 10:11:07 +02:00
|
|
|
func Benchmark_json_object(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
result := TestObj{}
|
|
|
|
json.Unmarshal([]byte(`{"field1": "1", "field2": 2}`), &result)
|
|
|
|
}
|
|
|
|
}
|