gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 6bdffa7fb6682104b576778da2fb9bcccfebe695
parent b81a0e0acffd9c87cb191d245cc600eb997068c0
Author: francoispqt <francois@parquet.ninja>
Date:   Sun, 20 May 2018 01:06:27 +0800

update error handling in specific unmarshal funcs and add tests

Diffstat:
Mdecode_bool.go | 6+++---
Mdecode_number_float_test.go | 14++++++++++++++
Mdecode_number_int_test.go | 47++++++++++++++++++++++++++++++++++++++++++++++-
Mdecode_number_uint_test.go | 35+++++++++++++++++++++++++++++++++++
Mdecode_object_test.go | 25++++++++++++++++++-------
Mdecode_unsafe.go | 16++--------------
Merrors.go | 6+++++-
7 files changed, 123 insertions(+), 26 deletions(-)

diff --git a/decode_bool.go b/decode_bool.go @@ -83,7 +83,7 @@ func (dec *Decoder) assertTrue() error { if i == 3 { return nil } - return InvalidJSONError("Invalid JSON") + return dec.raiseInvalidJSONErr(dec.cursor) } func (dec *Decoder) assertNull() error { @@ -116,7 +116,7 @@ func (dec *Decoder) assertNull() error { if i == 3 { return nil } - return InvalidJSONError("Invalid JSON") + return dec.raiseInvalidJSONErr(dec.cursor) } func (dec *Decoder) assertFalse() error { @@ -153,5 +153,5 @@ func (dec *Decoder) assertFalse() error { if i == 4 { return nil } - return InvalidJSONError("Invalid JSON") + return dec.raiseInvalidJSONErr(dec.cursor) } diff --git a/decode_number_float_test.go b/decode_number_float_test.go @@ -46,6 +46,13 @@ func TestDecoderFloat64(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-null-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-exponent-positive-positive-exp4", json: "8e+005", expectedResult: 800000, @@ -236,6 +243,13 @@ func TestDecoderFloat32(t *testing.T) { }, { name: "basic-null-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "basic-null-err", json: "nxll", expectedResult: 0, err: true, diff --git a/decode_number_int_test.go b/decode_number_int_test.go @@ -45,6 +45,13 @@ func TestDecoderInt(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-big", json: "9223372036854775807", expectedResult: 9223372036854775807, @@ -284,6 +291,13 @@ func TestDecoderInt64(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-big", json: "9223372036854775807", expectedResult: 9223372036854775807, @@ -520,6 +534,13 @@ func TestDecoderInt32(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-negative2", json: "-2349557", expectedResult: -2349557, @@ -768,6 +789,13 @@ func TestDecoderInt16(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-negative2", json: "-2456", expectedResult: -2456, @@ -1022,6 +1050,13 @@ func TestDecoderInt8(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-negative2", json: "-123", expectedResult: -123, @@ -1108,7 +1143,17 @@ func TestDecoderInt8(t *testing.T) { }, { name: "basic-exponent-positive-negative-exp4", - json: "8e-005", + json: "8e-1 ", + expectedResult: 0, + }, + { + name: "basic-exponent-positive-negative-exp4", + json: "8e1 ", + expectedResult: 80, + }, + { + name: "basic-exponent-positive-negative-exp4", + json: "8e-1", expectedResult: 0, }, { diff --git a/decode_number_uint_test.go b/decode_number_uint_test.go @@ -45,6 +45,13 @@ func TestDecoderUint64(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-big", json: "18446744073709551615", expectedResult: 18446744073709551615, @@ -190,6 +197,13 @@ func TestDecoderUint32(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-negative2", json: "-2349557", expectedResult: 2349557, @@ -343,6 +357,20 @@ func TestDecoderUint16(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-overflow", json: "335346564", expectedResult: 0, @@ -517,6 +545,13 @@ func TestDecoderUint8(t *testing.T) { errType: InvalidJSONError(""), }, { + name: "basic-skip-data-err", + json: "trua", + expectedResult: 0, + err: true, + errType: InvalidJSONError(""), + }, + { name: "basic-negative2", json: "-234", expectedResult: 234, diff --git a/decode_object_test.go b/decode_object_test.go @@ -431,13 +431,6 @@ func TestDecodeObjectBasic0Keys(t *testing.T) { errType: InvalidJSONError(""), }, { - name: "basic-err-invalid-type", - json: ``, - expectedResult: testObject0Keys{}, - err: true, - errType: InvalidJSONError(""), - }, - { name: "basic-err", json: `{ "testStr": "hello world!", @@ -721,6 +714,24 @@ func TestDecodeObjectBasic0Keys(t *testing.T) { } }) } + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + s := testObject0Keys{} + err := UnmarshalJSONObject([]byte(testCase.json), &s) + if testCase.err { + t.Log(err) + assert.NotNil(t, err, "err should not be nil") + if testCase.errType != nil { + assert.IsType(t, testCase.errType, err, "err should be of the given type") + } + return + } + assert.Nil(t, err, "err should be nil") + if !testCase.skipCheckResult { + assert.Equal(t, testCase.expectedResult, s, "value at given index should be the same as expected results") + } + }) + } } func TestDecodeObjectComplex(t *testing.T) { diff --git a/decode_unsafe.go b/decode_unsafe.go @@ -19,13 +19,7 @@ func (u decUnsafe) UnmarshalJSONArray(data []byte, v UnmarshalerJSONArray) error dec.data = data dec.length = len(data) _, err := dec.decodeArray(v) - if err != nil { - return err - } - if dec.err != nil { - return dec.err - } - return nil + return err } func (u decUnsafe) UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) error { @@ -34,13 +28,7 @@ func (u decUnsafe) UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) err dec.data = data dec.length = len(data) _, err := dec.decodeObject(v) - if err != nil { - return err - } - if dec.err != nil { - return dec.err - } - return nil + return err } func (u decUnsafe) Unmarshal(data []byte, v interface{}) error { diff --git a/errors.go b/errors.go @@ -15,10 +15,14 @@ func (err InvalidJSONError) Error() string { } func (dec *Decoder) raiseInvalidJSONErr(pos int) error { + var c byte + if len(dec.data) > pos { + c = dec.data[pos] + } dec.err = InvalidJSONError( fmt.Sprintf( invalidJSONCharErrorMsg, - dec.data[pos], + c, pos, ), )