2017-06-17 15:32:48 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/json-iterator/go/require"
|
2017-06-19 17:43:53 +02:00
|
|
|
"testing"
|
2017-06-17 15:32:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_read_object_as_any(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-06-18 16:21:54 +02:00
|
|
|
any := Get([]byte(`{"a":"b","c":"d"}`))
|
2017-06-17 15:32:48 +02:00
|
|
|
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()))
|
2017-06-18 16:21:54 +02:00
|
|
|
any = Get([]byte(`{"a":"b","c":"d"}`))
|
2017-06-17 15:32:48 +02:00
|
|
|
// full parse
|
|
|
|
should.Equal(2, len(any.Keys()))
|
|
|
|
should.Equal(2, any.Size())
|
|
|
|
should.True(any.ToBool())
|
|
|
|
should.Equal(1, any.ToInt())
|
2017-06-18 16:21:54 +02:00
|
|
|
should.Equal(Object, any.ValueType())
|
|
|
|
should.Nil(any.LastError())
|
|
|
|
should.Equal("b", any.GetObject()["a"].ToString())
|
2017-06-19 09:40:00 +02:00
|
|
|
obj := struct {
|
|
|
|
A string
|
|
|
|
}{}
|
|
|
|
any.ToVal(&obj)
|
|
|
|
should.Equal("b", obj.A)
|
2017-06-17 15:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_object_lazy_any_get(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-06-18 16:21:54 +02:00
|
|
|
any := Get([]byte(`{"a":{"b":{"c":"d"}}}`))
|
2017-06-17 15:32:48 +02:00
|
|
|
should.Equal("d", any.Get("a", "b", "c").ToString())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_object_lazy_any_get_all(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-06-18 16:21:54 +02:00
|
|
|
any := Get([]byte(`{"a":[0],"b":[1]}`))
|
2017-06-17 15:32:48 +02:00
|
|
|
should.Contains(any.Get('*', 0).ToString(), `"a":0`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_object_lazy_any_get_invalid(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-06-18 16:21:54 +02:00
|
|
|
any := Get([]byte(`{}`))
|
2017-06-17 15:32:48 +02:00
|
|
|
should.Equal(Invalid, any.Get("a", "b", "c").ValueType())
|
|
|
|
should.Equal(Invalid, any.Get(1).ValueType())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_wrap_object(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
type TestObject struct {
|
|
|
|
Field1 string
|
|
|
|
field2 string
|
|
|
|
}
|
|
|
|
any := Wrap(TestObject{"hello", "world"})
|
|
|
|
should.Equal("hello", any.Get("Field1").ToString())
|
|
|
|
any = Wrap(TestObject{"hello", "world"})
|
|
|
|
should.Equal(2, any.Size())
|
2017-06-18 17:09:30 +02:00
|
|
|
should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString())
|
2017-06-17 15:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_any_within_struct(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
type TestObject struct {
|
|
|
|
Field1 Any
|
|
|
|
Field2 Any
|
|
|
|
}
|
|
|
|
obj := TestObject{}
|
|
|
|
err := UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("hello", obj.Field1.ToString())
|
|
|
|
should.Equal("[1,2,3]", obj.Field2.ToString())
|
|
|
|
}
|