2016-12-02 05:22:20 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-01-07 06:28:16 +02:00
|
|
|
"bytes"
|
2017-06-13 10:58:53 +02:00
|
|
|
"encoding/json"
|
2017-06-06 17:27:00 +02:00
|
|
|
"github.com/json-iterator/go/require"
|
|
|
|
"testing"
|
2016-12-02 05:22:20 +02:00
|
|
|
)
|
|
|
|
|
2017-01-21 10:09:38 +02:00
|
|
|
func Test_read_null(t *testing.T) {
|
|
|
|
should := require.New(t)
|
2017-06-15 17:54:43 +02:00
|
|
|
iter := ParseString(ConfigOfDefault, `null`)
|
2017-01-21 10:09:38 +02:00
|
|
|
should.True(iter.ReadNil())
|
2017-06-15 17:54:43 +02:00
|
|
|
iter = ParseString(ConfigOfDefault, `null`)
|
2017-01-21 10:09:38 +02:00
|
|
|
should.Nil(iter.Read())
|
2017-06-15 17:54:43 +02:00
|
|
|
iter = ParseString(ConfigOfDefault, `null`)
|
2017-01-22 17:29:48 +02:00
|
|
|
any, err := UnmarshalAnyFromString(`null`)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal(0, any.ToInt())
|
|
|
|
should.Equal(float64(0), any.ToFloat64())
|
|
|
|
should.Equal("", any.ToString())
|
|
|
|
should.False(any.ToBool())
|
2016-12-02 05:22:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 06:28:16 +02:00
|
|
|
func Test_write_null(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
buf := &bytes.Buffer{}
|
2017-06-15 17:54:43 +02:00
|
|
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
2017-01-18 17:33:40 +02:00
|
|
|
stream.WriteNil()
|
2017-01-07 06:28:16 +02:00
|
|
|
stream.Flush()
|
|
|
|
should.Nil(stream.Error)
|
|
|
|
should.Equal("null", buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_encode_null(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
str, err := MarshalToString(nil)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_decode_null_object(t *testing.T) {
|
2017-05-23 12:46:11 +02:00
|
|
|
should := require.New(t)
|
2017-06-15 17:54:43 +02:00
|
|
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
2016-12-02 05:22:20 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadObject() != "" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "a" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-05-23 12:46:11 +02:00
|
|
|
type TestObject struct {
|
|
|
|
Field string
|
|
|
|
}
|
|
|
|
objs := []TestObject{}
|
|
|
|
should.Nil(UnmarshalFromString("[null]", &objs))
|
|
|
|
should.Len(objs, 1)
|
2016-12-02 05:22:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 06:28:16 +02:00
|
|
|
func Test_decode_null_array(t *testing.T) {
|
2017-06-15 17:54:43 +02:00
|
|
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
2016-12-02 05:22:20 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadArray() != false {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "a" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 06:28:16 +02:00
|
|
|
func Test_decode_null_string(t *testing.T) {
|
2017-01-18 17:33:40 +02:00
|
|
|
should := require.New(t)
|
2017-06-15 17:54:43 +02:00
|
|
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
2017-01-18 17:33:40 +02:00
|
|
|
should.True(iter.ReadArray())
|
2017-05-23 12:46:11 +02:00
|
|
|
should.Equal("", iter.ReadString())
|
2017-01-18 17:33:40 +02:00
|
|
|
should.True(iter.ReadArray())
|
|
|
|
should.Equal("a", iter.ReadString())
|
2016-12-02 05:22:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 06:28:16 +02:00
|
|
|
func Test_decode_null_skip(t *testing.T) {
|
2017-06-15 17:54:43 +02:00
|
|
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
2016-12-02 05:22:20 +02:00
|
|
|
iter.ReadArray()
|
|
|
|
iter.Skip()
|
|
|
|
iter.ReadArray()
|
|
|
|
if iter.ReadString() != "a" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-01-05 15:23:08 +02:00
|
|
|
}
|
2017-06-13 03:14:19 +02:00
|
|
|
|
|
|
|
func Test_encode_nil_map(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
type Ttest map[string]string
|
|
|
|
var obj1 Ttest
|
|
|
|
output, err := json.Marshal(obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
output, err = json.Marshal(&obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
output, err = Marshal(obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
output, err = Marshal(&obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_encode_nil_array(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
type Ttest []string
|
|
|
|
var obj1 Ttest
|
|
|
|
output, err := json.Marshal(obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
output, err = json.Marshal(&obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
output, err = Marshal(obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
output, err = Marshal(&obj1)
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal("null", string(output))
|
|
|
|
}
|