commit 299e79b1f53ac49117aab7bc7790134d7e53a6e0
parent 8a6a58d6ae768be5203aef1e639c27c4ddd3b943
Author: francoispqt <francois@parquet.ninja>
Date: Sun, 26 Aug 2018 21:40:26 +0800
add tests for null decoding
Diffstat:
5 files changed, 278 insertions(+), 270 deletions(-)
diff --git a/decode_object_test.go b/decode_object_test.go
@@ -3,12 +3,20 @@ package gojay
import (
"fmt"
"io"
+ "reflect"
"strings"
"testing"
+ "github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)
+func makePointer(v interface{}) interface{} {
+ var ptr = reflect.New(reflect.TypeOf(v))
+ ptr.Elem().Set(reflect.ValueOf(v))
+ return ptr.Interface()
+}
+
func TestDecodeObjectBasic(t *testing.T) {
testCases := []struct {
name string
@@ -22,33 +30,62 @@ func TestDecodeObjectBasic(t *testing.T) {
name: "basic",
json: `{
"testStr": "hello world!",
+ "testStrNull": "hello world!",
"testInt": 4535,
+ "testIntNull": 4535,
"testBool": true,
+ "testBoolNull": true,
"testFloat32": 2.345,
+ "testFloat32Null": 2.345,
"testFloat64": 123.677,
+ "testFloat64Null": 123.677,
"testInt8": 23,
+ "testInt8Null": 23,
"testInt16": 1245,
+ "testInt16Null": 1245,
"testInt32": 456778,
+ "testInt32Null": 456778,
"testInt64": 1446685358,
+ "testInt64Null": 1446685358,
"testUint8": 255,
+ "testUint8Null": 255,
"testUint16": 3455,
+ "testUint16Null": 3455,
"testUint32": 343443,
- "testUint64": 545665757
+ "testUint32Null": 343443,
+ "testUint64": 545665757,
+ "testUint64Null": 545665757,
+ "testSubObjectNull": {
+ "testStr": "1"
+ }
}`,
expectedResult: testObject{
- testStr: "hello world!",
- testInt: 4535,
- testBool: true,
- testFloat32: 2.345,
- testFloat64: 123.677,
- testInt8: 23,
- testInt16: 1245,
- testInt32: 456778,
- testInt64: 1446685358,
- testUint8: 255,
- testUint16: 3455,
- testUint32: 343443,
- testUint64: 545665757,
+ testStr: "hello world!",
+ testStrNull: makePointer("hello world!").(*string),
+ testInt: 4535,
+ testIntNull: makePointer(4535).(*int),
+ testBool: true,
+ testBoolNull: makePointer(true).(*bool),
+ testFloat32: 2.345,
+ testFloat32Null: makePointer(float32(2.345)).(*float32),
+ testFloat64: 123.677,
+ testFloat64Null: makePointer(float64(123.677)).(*float64),
+ testInt8: 23,
+ testInt8Null: makePointer(int8(23)).(*int8),
+ testInt16: 1245,
+ testInt16Null: makePointer(int16(1245)).(*int16),
+ testInt32: 456778,
+ testInt32Null: makePointer(int32(456778)).(*int32),
+ testInt64: 1446685358,
+ testInt64Null: makePointer(int64(1446685358)).(*int64),
+ testUint8: 255,
+ testUint8Null: makePointer(uint8(255)).(*uint8),
+ testUint16: 3455,
+ testUint16Null: makePointer(uint16(3455)).(*uint16),
+ testUint32: 343443,
+ testUint32Null: makePointer(uint32(343443)).(*uint32),
+ testUint64: 545665757,
+ testUint64Null: makePointer(uint64(545665757)).(*uint64),
},
err: false,
},
@@ -460,6 +497,7 @@ func TestDecodeObjectBasic(t *testing.T) {
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
err := dec.Decode(&s)
+ spew.Dump(s)
if testCase.err {
t.Log(err)
assert.NotNil(t, err, "err should not be nil")
diff --git a/decode_test.go b/decode_test.go
@@ -1,8 +1,8 @@
package gojay
import (
+ "bytes"
"fmt"
- "io"
"reflect"
"testing"
@@ -35,14 +35,15 @@ func (t *testDecodeSlice) UnmarshalJSONArray(dec *Decoder) error {
return nil
}
-// Unmarshal tests
-func TestUnmarshalAllTypes(t *testing.T) {
- testCases := []struct {
- name string
- v interface{}
- d []byte
- expectations func(err error, v interface{}, t *testing.T)
- }{
+type allTypeDecodeTestCase struct {
+ name string
+ v interface{}
+ d []byte
+ expectations func(err error, v interface{}, t *testing.T)
+}
+
+func allTypesTestCases() []allTypeDecodeTestCase {
+ return []allTypeDecodeTestCase{
{
v: new(string),
d: []byte(`"test string"`),
@@ -54,6 +55,16 @@ func TestUnmarshalAllTypes(t *testing.T) {
},
},
{
+ v: new(*string),
+ d: []byte(`"test string"`),
+ name: "test decode string",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(**string)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, "test string", **vt, "v must be equal to 1")
+ },
+ },
+ {
v: new(string),
d: []byte(`null`),
name: "test decode string null",
@@ -64,336 +75,219 @@ func TestUnmarshalAllTypes(t *testing.T) {
},
},
{
- v: new(int),
- d: []byte(`1`),
- name: "test decode int",
+ v: new(*string),
+ d: []byte(`null`),
+ name: "test decode string null",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*int)
+ vt := v.(**string)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, 1, *vt, "v must be equal to 1")
+ assert.Nil(t, *vt, "v must be nil")
},
},
{
- v: new(int64),
+
+ v: new(int),
d: []byte(`1`),
- name: "test decode int64",
+ name: "test decode int",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*int64)
+ vt := v.(*int)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, int64(1), *vt, "v must be equal to 1")
+ assert.Equal(t, 1, *vt, "v must be equal to 1")
},
},
{
- v: new(uint64),
+ v: new(*int),
d: []byte(`1`),
- name: "test decode uint64",
+ name: "test decode int",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint64)
+ vt := v.(**int)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, uint64(1), *vt, "v must be equal to 1")
- },
- },
- {
- v: new(uint64),
- d: []byte(`-1`),
- name: "test decode uint64 negative",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint64)
- assert.NotNil(t, err, "err must not be nil")
- assert.Equal(t, uint64(0), *vt, "v must be equal to 1")
+ assert.Equal(t, 1, **vt, "v must be equal to 1")
},
},
{
- v: new(int32),
+ v: new(*int8),
d: []byte(`1`),
- name: "test decode int32",
+ name: "test decode int",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*int32)
+ vt := v.(**int8)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, int32(1), *vt, "v must be equal to 1")
+ assert.Equal(t, int8(1), **vt, "v must be equal to 1")
},
},
{
- v: new(uint32),
+ v: new(*int16),
d: []byte(`1`),
- name: "test decode uint32",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint32)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, uint32(1), *vt, "v must be equal to 1")
- },
- },
- {
- v: new(uint32),
- d: []byte(`-1`),
- name: "test decode uint32 negative",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint32)
- assert.NotNil(t, err, "err must not be nil")
- assert.Equal(t, uint32(0), *vt, "v must be equal to 1")
- },
- },
- {
- v: new(float64),
- d: []byte(`1.15`),
- name: "test decode float64",
+ name: "test decode int",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*float64)
+ vt := v.(**int16)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, float64(1.15), *vt, "v must be equal to 1")
+ assert.Equal(t, int16(1), **vt, "v must be equal to 1")
},
},
{
- v: new(float64),
- d: []byte(`null`),
- name: "test decode float64 null",
+ v: new(int64),
+ d: []byte(`1`),
+ name: "test decode int64",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*float64)
+ vt := v.(*int64)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, float64(0), *vt, "v must be equal to 1")
+ assert.Equal(t, int64(1), *vt, "v must be equal to 1")
},
},
{
- v: new(bool),
- d: []byte(`true`),
- name: "test decode bool true",
+ v: new(*int64),
+ d: []byte(`1`),
+ name: "test decode int64",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*bool)
+ vt := v.(**int64)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, true, *vt, "v must be equal to 1")
+ assert.Equal(t, int64(1), **vt, "v must be equal to 1")
},
},
{
- v: new(bool),
- d: []byte(`false`),
- name: "test decode bool false",
+ v: new(uint64),
+ d: []byte(`1`),
+ name: "test decode uint64",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*bool)
+ vt := v.(*uint64)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, false, *vt, "v must be equal to 1")
+ assert.Equal(t, uint64(1), *vt, "v must be equal to 1")
},
},
{
- v: new(bool),
- d: []byte(`null`),
- name: "test decode bool null",
+ v: new(*uint64),
+ d: []byte(`1`),
+ name: "test decode uint64",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*bool)
+ vt := v.(**uint64)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, false, *vt, "v must be equal to 1")
+ assert.Equal(t, uint64(1), **vt, "v must be equal to 1")
},
},
{
- v: new(testDecodeObj),
- d: []byte(`{"test":"test"}`),
- name: "test decode object",
+ v: new(uint64),
+ d: []byte(`-1`),
+ name: "test decode uint64 negative",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*testDecodeObj)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, "test", vt.test, "v.test must be equal to 'test'")
+ vt := v.(*uint64)
+ assert.NotNil(t, err, "err must not be nil")
+ assert.Equal(t, uint64(0), *vt, "v must be equal to 1")
},
},
{
- v: new(testDecodeObj),
- d: []byte(`{"test":null}`),
- name: "test decode object null key",
+ v: new(int32),
+ d: []byte(`1`),
+ name: "test decode int32",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*testDecodeObj)
+ vt := v.(*int32)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
+ assert.Equal(t, int32(1), *vt, "v must be equal to 1")
},
},
{
- v: new(testDecodeObj),
- d: []byte(`null`),
- name: "test decode object null",
+ v: new(*int32),
+ d: []byte(`1`),
+ name: "test decode int32",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*testDecodeObj)
+ vt := v.(**int32)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, "", vt.test, "v.test must be equal to 'test'")
+ assert.Equal(t, int32(1), **vt, "v must be equal to 1")
},
},
{
- v: new(testDecodeSlice),
- d: []byte(`[{"test":"test"}]`),
- name: "test decode slice",
+ v: new(uint32),
+ d: []byte(`1`),
+ name: "test decode uint32",
expectations: func(err error, v interface{}, t *testing.T) {
- vtPtr := v.(*testDecodeSlice)
- vt := *vtPtr
+ vt := v.(*uint32)
assert.Nil(t, err, "err must be nil")
- assert.Len(t, vt, 1, "len of vt must be 1")
- assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
+ assert.Equal(t, uint32(1), *vt, "v must be equal to 1")
},
},
{
- v: new(testDecodeSlice),
- d: []byte(`[{"test":"test"},{"test":"test2"}]`),
- name: "test decode slice",
+ v: new(*uint32),
+ d: []byte(`1`),
+ name: "test decode uint32",
expectations: func(err error, v interface{}, t *testing.T) {
- vtPtr := v.(*testDecodeSlice)
- vt := *vtPtr
+ vt := v.(**uint32)
assert.Nil(t, err, "err must be nil")
- assert.Len(t, vt, 2, "len of vt must be 2")
- assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
- assert.Equal(t, "test2", vt[1].test, "vt[1].test must be equal to 'test2'")
+ assert.Equal(t, uint32(1), **vt, "v must be equal to 1")
},
},
{
- v: new(struct{}),
- d: []byte(`{"test":"test"}`),
- name: "test decode invalid type",
+ v: new(uint32),
+ d: []byte(`-1`),
+ name: "test decode uint32 negative",
expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(*uint32)
assert.NotNil(t, err, "err must not be nil")
- assert.IsType(t, InvalidUnmarshalError(""), err, "err must be of type InvalidUnmarshalError")
- assert.Equal(t, fmt.Sprintf(invalidUnmarshalErrorMsg, reflect.TypeOf(v).String()), err.Error(), "err message should be equal to invalidUnmarshalErrorMsg")
- },
- },
- }
- for _, testCase := range testCases {
- testCase := testCase
- t.Run(testCase.name, func(*testing.T) {
- err := Unmarshal(testCase.d, testCase.v)
- testCase.expectations(err, testCase.v, t)
- })
- }
-}
-
-// Decode tests
-
-type TestReader struct {
- data string
- done bool
-}
-
-func (r *TestReader) Read(b []byte) (int, error) {
- if !r.done {
- n := copy(b, r.data)
- r.done = true
- return n, nil
- }
- return 0, io.EOF
-}
-
-func TestDecodeAllTypes(t *testing.T) {
- testCases := []struct {
- name string
- v interface{}
- r TestReader
- expectations func(err error, v interface{}, t *testing.T)
- }{
- {
- v: new(string),
- r: TestReader{`"test string"`, false},
- name: "test decode string",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*string)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, "test string", *vt, "v must be equal to 1")
- },
- },
- {
- v: new(string),
- r: TestReader{`null`, false},
- name: "test decode string null",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*string)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, "", *vt, "v must be equal to 1")
- },
- },
- {
- v: new(int),
- r: TestReader{`1`, false},
- name: "test decode int",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*int)
- assert.Nil(t, err, "err must be nil")
- assert.Equal(t, 1, *vt, "v must be equal to 1")
+ assert.Equal(t, uint32(0), *vt, "v must be equal to 1")
},
},
{
- v: new(int64),
- r: TestReader{`1`, false},
- name: "test decode int64",
+ v: new(*uint16),
+ d: []byte(`1`),
+ name: "test decode uint16",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*int64)
+ vt := v.(**uint16)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, int64(1), *vt, "v must be equal to 1")
+ assert.Equal(t, uint16(1), **vt, "v must be equal to 1")
},
},
{
- v: new(uint64),
- r: TestReader{`1`, false},
- name: "test decode uint64",
+ v: new(*uint8),
+ d: []byte(`1`),
+ name: "test decode uint8",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint64)
+ vt := v.(**uint8)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, uint64(1), *vt, "v must be equal to 1")
+ assert.Equal(t, uint8(1), **vt, "v must be equal to 1")
},
},
{
- v: new(uint64),
- r: TestReader{`-1`, false},
- name: "test decode uint64 negative",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint64)
- assert.NotNil(t, err, "err must not be nil")
- assert.Equal(t, uint64(0), *vt, "v must be equal to 1")
- },
- },
- {
- v: new(int32),
- r: TestReader{`1`, false},
- name: "test decode int32",
+ v: new(float64),
+ d: []byte(`1.15`),
+ name: "test decode float64",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*int32)
+ vt := v.(*float64)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, int32(1), *vt, "v must be equal to 1")
+ assert.Equal(t, float64(1.15), *vt, "v must be equal to 1")
},
},
{
- v: new(uint32),
- r: TestReader{`1`, false},
- name: "test decode uint32",
+ v: new(*float64),
+ d: []byte(`1.15`),
+ name: "test decode float64",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint32)
+ vt := v.(**float64)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, uint32(1), *vt, "v must be equal to 1")
- },
- },
- {
- v: new(uint32),
- r: TestReader{`-1`, false},
- name: "test decode uint32 negative",
- expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*uint32)
- assert.NotNil(t, err, "err must not be nil")
- assert.Equal(t, uint32(0), *vt, "v must be equal to 1")
+ assert.Equal(t, float64(1.15), **vt, "v must be equal to 1")
},
},
{
v: new(float64),
- r: TestReader{`1.15`, false},
- name: "test decode float64",
+ d: []byte(`null`),
+ name: "test decode float64 null",
expectations: func(err error, v interface{}, t *testing.T) {
vt := v.(*float64)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, float64(1.15), *vt, "v must be equal to 1")
+ assert.Equal(t, float64(0), *vt, "v must be equal to 1")
},
},
{
- v: new(float64),
- r: TestReader{`null`, false},
+ v: new(*float32),
+ d: []byte(`1.15`),
name: "test decode float64 null",
expectations: func(err error, v interface{}, t *testing.T) {
- vt := v.(*float64)
+ vt := v.(**float32)
assert.Nil(t, err, "err must be nil")
- assert.Equal(t, float64(0), *vt, "v must be equal to 1")
+ assert.Equal(t, float32(1.15), **vt, "v must be equal to 1")
},
},
{
v: new(bool),
- r: TestReader{`true`, false},
+ d: []byte(`true`),
name: "test decode bool true",
expectations: func(err error, v interface{}, t *testing.T) {
vt := v.(*bool)
@@ -402,8 +296,18 @@ func TestDecodeAllTypes(t *testing.T) {
},
},
{
+ v: new(*bool),
+ d: []byte(`true`),
+ name: "test decode bool true",
+ expectations: func(err error, v interface{}, t *testing.T) {
+ vt := v.(**bool)
+ assert.Nil(t, err, "err must be nil")
+ assert.Equal(t, true, **vt, "v must be equal to 1")
+ },
+ },
+ {
v: new(bool),
- r: TestReader{`false`, false},
+ d: []byte(`false`),
name: "test decode bool false",
expectations: func(err error, v interface{}, t *testing.T) {
vt := v.(*bool)
@@ -413,7 +317,7 @@ func TestDecodeAllTypes(t *testing.T) {
},
{
v: new(bool),
- r: TestReader{`null`, false},
+ d: []byte(`null`),
name: "test decode bool null",
expectations: func(err error, v interface{}, t *testing.T) {
vt := v.(*bool)
@@ -423,7 +327,7 @@ func TestDecodeAllTypes(t *testing.T) {
},
{
v: new(testDecodeObj),
- r: TestReader{`{"test":"test"}`, false},
+ d: []byte(`{"test":"test"}`),
name: "test decode object",
expectations: func(err error, v interface{}, t *testing.T) {
vt := v.(*testDecodeObj)
@@ -433,7 +337,7 @@ func TestDecodeAllTypes(t *testing.T) {
},
{
v: new(testDecodeObj),
- r: TestReader{`{"test":null}`, false},
+ d: []byte(`{"test":null}`),
name: "test decode object null key",
expectations: func(err error, v interface{}, t *testing.T) {
vt := v.(*testDecodeObj)
@@ -443,7 +347,7 @@ func TestDecodeAllTypes(t *testing.T) {
},
{
v: new(testDecodeObj),
- r: TestReader{`null`, false},
+ d: []byte(`null`),
name: "test decode object null",
expectations: func(err error, v interface{}, t *testing.T) {
vt := v.(*testDecodeObj)
@@ -453,7 +357,7 @@ func TestDecodeAllTypes(t *testing.T) {
},
{
v: new(testDecodeSlice),
- r: TestReader{`[{"test":"test"}]`, false},
+ d: []byte(`[{"test":"test"}]`),
name: "test decode slice",
expectations: func(err error, v interface{}, t *testing.T) {
vtPtr := v.(*testDecodeSlice)
@@ -465,7 +369,7 @@ func TestDecodeAllTypes(t *testing.T) {
},
{
v: new(testDecodeSlice),
- r: TestReader{`[{"test":"test"},{"test":"test2"}]`, false},
+ d: []byte(`[{"test":"test"},{"test":"test2"}]`),
name: "test decode slice",
expectations: func(err error, v interface{}, t *testing.T) {
vtPtr := v.(*testDecodeSlice)
@@ -478,7 +382,7 @@ func TestDecodeAllTypes(t *testing.T) {
},
{
v: new(struct{}),
- r: TestReader{`{"test":"test"}`, false},
+ d: []byte(`{"test":"test"}`),
name: "test decode invalid type",
expectations: func(err error, v interface{}, t *testing.T) {
assert.NotNil(t, err, "err must not be nil")
@@ -487,10 +391,25 @@ func TestDecodeAllTypes(t *testing.T) {
},
},
}
- for _, testCase := range testCases {
+}
+
+// Unmarshal tests
+func TestUnmarshalAllTypes(t *testing.T) {
+ for _, testCase := range allTypesTestCases() {
+ testCase := testCase
+ t.Run(testCase.name, func(*testing.T) {
+ err := Unmarshal(testCase.d, testCase.v)
+ testCase.expectations(err, testCase.v, t)
+ })
+ }
+}
+
+// Decode tests
+func TestDecodeAllTypes(t *testing.T) {
+ for _, testCase := range allTypesTestCases() {
testCase := testCase
t.Run(testCase.name, func(*testing.T) {
- dec := NewDecoder(&testCase.r)
+ dec := NewDecoder(bytes.NewReader(testCase.d))
err := dec.Decode(testCase.v)
testCase.expectations(err, testCase.v, t)
})
diff --git a/encode_interface_test.go b/encode_interface_test.go
@@ -112,7 +112,11 @@ var encoderTestCases = []struct {
},
},
{
- v: &testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true, &testObject{}, testSliceInts{}},
+ v: &testObject{
+ "漢字", nil, 1, nil, 1, nil, 1, nil, 1, nil, 1, nil,
+ 1, nil, 1, nil, 1, nil, 1, nil, 1.1, nil, 1.1, nil, true, nil,
+ &testObject{}, testSliceInts{},
+ },
expectations: func(t *testing.T, b string, err error) {
assert.Nil(t, err, "err should be nil")
assert.Equal(t, `{"testStr":"漢字","testInt":1,"testInt64":1,"testInt32":1,"testInt16":1,"testInt8":1,"testUint64":1,"testUint32":1,"testUint16":1,"testUint8":1,"testFloat64":1.1,"testFloat32":1.1,"testBool":true}`, string(b), `string(b) should equal {"testStr":"漢字","testInt":1,"testInt64":1,"testInt32":1,"testInt16":1,"testInt8":1,"testUint64":1,"testUint32":1,"testUint16":1,"testUint8":1,"testFloat64":1.1,"testFloat32":1.1,"testBool":true}`)
diff --git a/encode_object_test.go b/encode_object_test.go
@@ -83,7 +83,11 @@ func TestEncoderObjectEncodeAPI(t *testing.T) {
t.Run("encode-basic", func(t *testing.T) {
builder := &strings.Builder{}
enc := NewEncoder(builder)
- err := enc.EncodeObject(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true, &testObject{}, testSliceInts{}})
+ err := enc.EncodeObject(&testObject{
+ "漢字", nil, 1, nil, 1, nil, 1, nil, 1, nil, 1, nil, 1, nil, 1, nil,
+ 1, nil, 1, nil, 1.1, nil, 1.1, nil, true, nil,
+ &testObject{}, testSliceInts{},
+ })
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
@@ -96,7 +100,11 @@ func TestEncoderObjectEncodeAPI(t *testing.T) {
func TestEncoderObjectMarshalAPI(t *testing.T) {
t.Run("marshal-basic", func(t *testing.T) {
- r, err := Marshal(&testObject{"漢字", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.1, 1.1, true, &testObject{}, testSliceInts{}})
+ r, err := Marshal(&testObject{
+ "漢字", nil, 1, nil, 1, nil, 1, nil, 1, nil, 1, nil, 1, nil, 1,
+ nil, 1, nil, 1, nil, 1.1, nil, 1.1, nil, true, nil,
+ &testObject{}, testSliceInts{},
+ })
assert.Nil(t, err, "Error should be nil")
assert.Equal(
t,
diff --git a/gojay_test.go b/gojay_test.go
@@ -1,21 +1,34 @@
package gojay
type testObject struct {
- testStr string
- testInt int
- testInt64 int64
- testInt32 int32
- testInt16 int16
- testInt8 int8
- testUint64 uint64
- testUint32 uint32
- testUint16 uint16
- testUint8 uint8
- testFloat64 float64
- testFloat32 float32
- testBool bool
- testSubObject *testObject
- testSubArray testSliceInts
+ testStr string
+ testStrNull *string
+ testInt int
+ testIntNull *int
+ testInt64 int64
+ testInt64Null *int64
+ testInt32 int32
+ testInt32Null *int32
+ testInt16 int16
+ testInt16Null *int16
+ testInt8 int8
+ testInt8Null *int8
+ testUint64 uint64
+ testUint64Null *uint64
+ testUint32 uint32
+ testUint32Null *uint32
+ testUint16 uint16
+ testUint16Null *uint16
+ testUint8 uint8
+ testUint8Null *uint8
+ testFloat64 float64
+ testFloat64Null *float64
+ testFloat32 float32
+ testFloat32Null *float32
+ testBool bool
+ testBoolNull *bool
+ testSubObject *testObject
+ testSubArray testSliceInts
}
// make sure it implements interfaces
@@ -46,36 +59,62 @@ func (t *testObject) UnmarshalJSONObject(dec *Decoder, k string) error {
switch k {
case "testStr":
return dec.AddString(&t.testStr)
+ case "testStrNull":
+ return dec.AddStringNull(&t.testStrNull)
case "testInt":
return dec.AddInt(&t.testInt)
+ case "testIntNull":
+ return dec.AddIntNull(&t.testIntNull)
case "testInt64":
return dec.AddInt64(&t.testInt64)
+ case "testInt64Null":
+ return dec.AddInt64Null(&t.testInt64Null)
case "testInt32":
return dec.AddInt32(&t.testInt32)
+ case "testInt32Null":
+ return dec.AddInt32Null(&t.testInt32Null)
case "testInt16":
return dec.AddInt16(&t.testInt16)
+ case "testInt16Null":
+ return dec.AddInt16Null(&t.testInt16Null)
case "testInt8":
return dec.AddInt8(&t.testInt8)
+ case "testInt8Null":
+ return dec.AddInt8Null(&t.testInt8Null)
case "testUint64":
return dec.AddUint64(&t.testUint64)
+ case "testUint64Null":
+ return dec.AddUint64Null(&t.testUint64Null)
case "testUint32":
return dec.AddUint32(&t.testUint32)
+ case "testUint32Null":
+ return dec.AddUint32Null(&t.testUint32Null)
case "testUint16":
return dec.AddUint16(&t.testUint16)
+ case "testUint16Null":
+ return dec.AddUint16Null(&t.testUint16Null)
case "testUint8":
return dec.AddUint8(&t.testUint8)
+ case "testUint8Null":
+ return dec.AddUint8Null(&t.testUint8Null)
case "testFloat64":
return dec.AddFloat(&t.testFloat64)
+ case "testFloat64Null":
+ return dec.AddFloat64Null(&t.testFloat64Null)
case "testFloat32":
return dec.AddFloat32(&t.testFloat32)
+ case "testFloat32Null":
+ return dec.AddFloat32Null(&t.testFloat32Null)
case "testBool":
return dec.AddBool(&t.testBool)
+ case "testBoolNull":
+ return dec.AddBoolNull(&t.testBoolNull)
}
return nil
}
func (t *testObject) NKeys() int {
- return 13
+ return 28
}
type testObject0Keys struct {