gojay

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

commit e9693678193be200f8e2edd3a5730cb86127c072
parent 346ac0d28bbe5a714bc66569339aa483a3585631
Author: francoispqt <francois@parquet.ninja>
Date:   Sun, 28 Oct 2018 14:17:23 +0800

move decode add values method to the decode_{type}.go file

Diffstat:
Mdecode.go | 696-------------------------------------------------------------------------------
Mdecode_array.go | 62+++++++++++++++++++++++++++++++++++++++++++++++++++++---------
Mdecode_bool.go | 44+++++++++++++++++++++++++++++++++++++++++++-
Mdecode_embedded_json.go | 6++++++
Mdecode_interface.go | 17+++++++++++++++++
Mdecode_number_float.go | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecode_number_int.go | 177+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecode_number_uint.go | 142+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecode_object.go | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecode_sqlnull.go | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdecode_string.go | 38++++++++++++++++++++++++++++++++++++++
Mdecode_time.go | 17+++++++++++++++++
12 files changed, 723 insertions(+), 706 deletions(-)

diff --git a/decode.go b/decode.go @@ -1,10 +1,8 @@ package gojay import ( - "database/sql" "fmt" "io" - "time" ) // UnmarshalJSONArray parses the JSON-encoded data and stores the result in the value pointed to by v. @@ -335,700 +333,6 @@ func (dec *Decoder) Decode(v interface{}) error { return dec.err } -// ADD VALUES FUNCTIONS - -// AddInt decodes the next key to an *int. -// If next key value overflows int, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddInt(v *int) error { - return dec.Int(v) -} - -// AddIntNull decodes the next key to an *int. -// If next key value overflows int, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddIntNull(v **int) error { - return dec.IntNull(v) -} - -// AddInt8 decodes the next key to an *int. -// If next key value overflows int8, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddInt8(v *int8) error { - return dec.Int8(v) -} - -// AddInt8Null decodes the next key to an *int. -// If next key value overflows int8, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddInt8Null(v **int8) error { - return dec.Int8Null(v) -} - -// AddInt16 decodes the next key to an *int. -// If next key value overflows int16, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddInt16(v *int16) error { - return dec.Int16(v) -} - -// AddInt16Null decodes the next key to an *int. -// If next key value overflows int16, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddInt16Null(v **int16) error { - return dec.Int16Null(v) -} - -// AddInt32 decodes the next key to an *int. -// If next key value overflows int32, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddInt32(v *int32) error { - return dec.Int32(v) -} - -// AddInt32Null decodes the next key to an *int. -// If next key value overflows int32, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddInt32Null(v **int32) error { - return dec.Int32Null(v) -} - -// AddInt64 decodes the next key to an *int. -// If next key value overflows int64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddInt64(v *int64) error { - return dec.Int64(v) -} - -// AddInt64Null decodes the next key to an *int. -// If next key value overflows int64, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddInt64Null(v **int64) error { - return dec.Int64Null(v) -} - -// AddUint8 decodes the next key to an *int. -// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddUint8(v *uint8) error { - return dec.Uint8(v) -} - -// AddUint8Null decodes the next key to an *int. -// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddUint8Null(v **uint8) error { - return dec.Uint8Null(v) -} - -// AddUint16 decodes the next key to an *int. -// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddUint16(v *uint16) error { - return dec.Uint16(v) -} - -// AddUint16Null decodes the next key to an *int. -// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddUint16Null(v **uint16) error { - return dec.Uint16Null(v) -} - -// AddUint32 decodes the next key to an *int. -// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddUint32(v *uint32) error { - return dec.Uint32(v) -} - -// AddUint32Null decodes the next key to an *int. -// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddUint32Null(v **uint32) error { - return dec.Uint32Null(v) -} - -// AddUint64 decodes the next key to an *int. -// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddUint64(v *uint64) error { - return dec.Uint64(v) -} - -// AddUint64Null decodes the next key to an *int. -// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddUint64Null(v **uint64) error { - return dec.Uint64Null(v) -} - -// AddFloat decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddFloat(v *float64) error { - return dec.Float64(v) -} - -// AddFloatNull decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddFloatNull(v **float64) error { - return dec.Float64Null(v) -} - -// AddFloat64 decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddFloat64(v *float64) error { - return dec.Float64(v) -} - -// AddFloat64Null decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddFloat64Null(v **float64) error { - return dec.Float64Null(v) -} - -// AddFloat32 decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) AddFloat32(v *float32) error { - return dec.Float32(v) -} - -// AddFloat32Null decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddFloat32Null(v **float32) error { - return dec.Float32Null(v) -} - -// AddBool decodes the next key to a *bool. -// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. -// If next key is null, bool will be false. -func (dec *Decoder) AddBool(v *bool) error { - return dec.Bool(v) -} - -// AddBoolNull decodes the next key to a *bool. -// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. -// If next key is null, bool will be false. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddBoolNull(v **bool) error { - return dec.BoolNull(v) -} - -// AddString decodes the next key to a *string. -// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. -func (dec *Decoder) AddString(v *string) error { - return dec.String(v) -} - -// AddStringNull decodes the next key to a *string. -// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) AddStringNull(v **string) error { - return dec.StringNull(v) -} - -// AddObject decodes the next key to a UnmarshalerJSONObject. -func (dec *Decoder) AddObject(v UnmarshalerJSONObject) error { - return dec.Object(v) -} - -// AddObjectNull decodes the next key to a UnmarshalerJSONObject. -func (dec *Decoder) AddObjectNull(v interface{}) error { - return dec.ObjectNull(v) -} - -// AddArray decodes the next key to a UnmarshalerJSONArray. -func (dec *Decoder) AddArray(v UnmarshalerJSONArray) error { - return dec.Array(v) -} - -// AddArrayNull decodes the next key to a UnmarshalerJSONArray. -func (dec *Decoder) AddArrayNull(v UnmarshalerJSONArray) error { - return dec.ArrayNull(v) -} - -// AddInterface decodes the next key to a interface{}. -func (dec *Decoder) AddInterface(v *interface{}) error { - return dec.Interface(v) -} - -// --- SQL types - -// AddSQLNullString decodes the next key to qn *sql.NullString -func (dec *Decoder) AddSQLNullString(v *sql.NullString) error { - return dec.SQLNullString(v) -} - -// SQLNullString decodes the next key to an *sql.NullString -func (dec *Decoder) SQLNullString(v *sql.NullString) error { - var b *string - if err := dec.StringNull(&b); err != nil { - return err - } - if b == nil { - v.Valid = false - } else { - v.String = *b - v.Valid = true - } - return nil -} - -// AddSQLNullInt64 decodes the next key to qn *sql.NullInt64 -func (dec *Decoder) AddSQLNullInt64(v *sql.NullInt64) error { - return dec.SQLNullInt64(v) -} - -// SQLNullInt64 decodes the next key to an *sql.NullInt64 -func (dec *Decoder) SQLNullInt64(v *sql.NullInt64) error { - var b *int64 - if err := dec.Int64Null(&b); err != nil { - return err - } - if b == nil { - v.Valid = false - } else { - v.Int64 = *b - v.Valid = true - } - return nil -} - -// AddSQLNullFloat64 decodes the next key to qn *sql.NullFloat64 -func (dec *Decoder) AddSQLNullFloat64(v *sql.NullFloat64) error { - return dec.SQLNullFloat64(v) -} - -// SQLNullFloat64 decodes the next key to an *sql.NullFloat64 -func (dec *Decoder) SQLNullFloat64(v *sql.NullFloat64) error { - var b *float64 - if err := dec.Float64Null(&b); err != nil { - return err - } - if b == nil { - v.Valid = false - } else { - v.Float64 = *b - v.Valid = true - } - return nil -} - -// AddSQLNullBool decodes the next key to an *sql.NullBool -func (dec *Decoder) AddSQLNullBool(v *sql.NullBool) error { - return dec.SQLNullBool(v) -} - -// SQLNullBool decodes the next key to an *sql.NullBool -func (dec *Decoder) SQLNullBool(v *sql.NullBool) error { - var b *bool - if err := dec.BoolNull(&b); err != nil { - return err - } - if b == nil { - v.Valid = false - } else { - v.Bool = *b - v.Valid = true - } - return nil -} - -// Int decodes the next key to an *int. -// If next key value overflows int, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int(v *int) error { - err := dec.decodeInt(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// IntNull decodes the next key to an *int. -// If next key value overflows int, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) IntNull(v **int) error { - err := dec.decodeIntNull(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int8 decodes the next key to an *int. -// If next key value overflows int8, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int8(v *int8) error { - err := dec.decodeInt8(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int8Null decodes the next key to an *int. -// If next key value overflows int8, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int8Null(v **int8) error { - err := dec.decodeInt8Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int16 decodes the next key to an *int. -// If next key value overflows int16, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int16(v *int16) error { - err := dec.decodeInt16(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int16Null decodes the next key to an *int. -// If next key value overflows int16, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int16Null(v **int16) error { - err := dec.decodeInt16Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int32 decodes the next key to an *int. -// If next key value overflows int32, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int32(v *int32) error { - err := dec.decodeInt32(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int32Null decodes the next key to an *int. -// If next key value overflows int32, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int32Null(v **int32) error { - err := dec.decodeInt32Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int64 decodes the next key to an *int. -// If next key value overflows int64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int64(v *int64) error { - err := dec.decodeInt64(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Int64Null decodes the next key to an *int. -// If next key value overflows int64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Int64Null(v **int64) error { - err := dec.decodeInt64Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint8 decodes the next key to an *int. -// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint8(v *uint8) error { - err := dec.decodeUint8(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint8Null decodes the next key to an *int. -// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint8Null(v **uint8) error { - err := dec.decodeUint8Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint16 decodes the next key to an *int. -// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint16(v *uint16) error { - err := dec.decodeUint16(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint16Null decodes the next key to an *int. -// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint16Null(v **uint16) error { - err := dec.decodeUint16Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint32 decodes the next key to an *int. -// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint32(v *uint32) error { - err := dec.decodeUint32(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint32Null decodes the next key to an *int. -// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint32Null(v **uint32) error { - err := dec.decodeUint32Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint64 decodes the next key to an *int. -// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint64(v *uint64) error { - err := dec.decodeUint64(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Uint64Null decodes the next key to an *int. -// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Uint64Null(v **uint64) error { - err := dec.decodeUint64Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Float decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Float(v *float64) error { - return dec.Float64(v) -} - -// FloatNull decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) FloatNull(v **float64) error { - return dec.Float64Null(v) -} - -// Float64 decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Float64(v *float64) error { - err := dec.decodeFloat64(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Float64Null decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Float64Null(v **float64) error { - err := dec.decodeFloat64Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Float32 decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Float32(v *float32) error { - err := dec.decodeFloat32(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Float32Null decodes the next key to a *float64. -// If next key value overflows float64, an InvalidUnmarshalError error will be returned. -func (dec *Decoder) Float32Null(v **float32) error { - err := dec.decodeFloat32Null(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Bool decodes the next key to a *bool. -// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. -// If next key is null, bool will be false. -func (dec *Decoder) Bool(v *bool) error { - err := dec.decodeBool(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// BoolNull decodes the next key to a *bool. -// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. -// If next key is null, bool will be false. -func (dec *Decoder) BoolNull(v **bool) error { - err := dec.decodeBoolNull(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// String decodes the next key to a *string. -// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. -func (dec *Decoder) String(v *string) error { - err := dec.decodeString(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// StringNull decodes the next key to a **string. -// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. -// If a `null` is encountered, gojay does not change the value of the pointer. -func (dec *Decoder) StringNull(v **string) error { - err := dec.decodeStringNull(v) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// AddTime decodes the next key to a *time.Time with the given format -func (dec *Decoder) AddTime(v *time.Time, format string) error { - return dec.Time(v, format) -} - -// Time decodes the next key to a *time.Time with the given format -func (dec *Decoder) Time(v *time.Time, format string) error { - err := dec.decodeTime(v, format) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Object decodes the next key to a UnmarshalerJSONObject. -func (dec *Decoder) Object(value UnmarshalerJSONObject) error { - initialKeysDone := dec.keysDone - initialChild := dec.child - dec.keysDone = 0 - dec.called = 0 - dec.child |= 1 - newCursor, err := dec.decodeObject(value) - if err != nil { - return err - } - dec.cursor = newCursor - dec.keysDone = initialKeysDone - dec.child = initialChild - dec.called |= 1 - return nil -} - -// ObjectNull decodes the next key to a UnmarshalerJSONObject. -// v should be a pointer to an UnmarshalerJSONObject, -// if `null` value is encountered in JSON, it will leave the value v untouched, -// else it will create a new instance of the UnmarshalerJSONObject behind v. -func (dec *Decoder) ObjectNull(v interface{}) error { - initialKeysDone := dec.keysDone - initialChild := dec.child - dec.keysDone = 0 - dec.called = 0 - dec.child |= 1 - newCursor, err := dec.decodeObjectNull(v) - if err != nil { - return err - } - dec.cursor = newCursor - dec.keysDone = initialKeysDone - dec.child = initialChild - dec.called |= 1 - return nil -} - -// Array decodes the next key to a UnmarshalerJSONArray. -func (dec *Decoder) Array(v UnmarshalerJSONArray) error { - newCursor, err := dec.decodeArray(v) - if err != nil { - return err - } - dec.cursor = newCursor - dec.called |= 1 - return nil -} - -// ArrayNull decodes the next key to a UnmarshalerJSONArray. -// v should be a pointer to an UnmarshalerJSONArray, -// if `null` value is encountered in JSON, it will leave the value v untouched, -// else it will create a new instance of the UnmarshalerJSONArray behind v. -func (dec *Decoder) ArrayNull(v interface{}) error { - newCursor, err := dec.decodeArrayNull(v) - if err != nil { - return err - } - dec.cursor = newCursor - dec.called |= 1 - return nil -} - -// Interface decodes the next key to an interface{}. -func (dec *Decoder) Interface(value *interface{}) error { - err := dec.decodeInterface(value) - if err != nil { - return err - } - dec.called |= 1 - return nil -} - -// Array decodes the next key to a UnmarshalerJSONArray. -// func (dec *Decoder) ArrayNull(factory func() UnmarshalerJSONArray) error { -// newCursor, err := dec.decodeArrayNull(factory) -// if err != nil { -// return err -// } -// dec.cursor = newCursor -// dec.called |= 1 -// return nil -// } - // Non exported func isDigit(b byte) bool { diff --git a/decode_array.go b/decode_array.go @@ -2,16 +2,17 @@ package gojay import "reflect" -// DecodeArray reads the next JSON-encoded value from its input and stores it in the value pointed to by v. +// DecodeArray reads the next JSON-encoded value from the decoder's input (io.Reader) +// and stores it in the value pointed to by v. // // v must implement UnmarshalerJSONArray. // // See the documentation for Unmarshal for details about the conversion of JSON into a Go value. -func (dec *Decoder) DecodeArray(arr UnmarshalerJSONArray) error { +func (dec *Decoder) DecodeArray(v UnmarshalerJSONArray) error { if dec.isPooled == 1 { panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder")) } - _, err := dec.decodeArray(arr) + _, err := dec.decodeArray(v) return err } func (dec *Decoder) decodeArray(arr UnmarshalerJSONArray) (int, error) { @@ -174,14 +175,20 @@ func (dec *Decoder) skipArray() (int, error) { return 0, dec.raiseInvalidJSONErr(dec.cursor) } -// DecodeArrayFunc is a custom func type implementing UnmarshalerJSONArray. -// Use it to cast a func(*Decoder) to Unmarshal an object. +// DecodeArrayFunc is a func type implementing UnmarshalerJSONArray. +// Use it to cast a `func(*Decoder) error` to Unmarshal an array on the fly. // -// str := "" +// strSlice := make([]string, 0) // dec := gojay.NewDecoder(io.Reader) -// dec.DecodeArray(gojay.DecodeArrayFunc(func(dec *gojay.Decoder, k string) error { -// return dec.AddString(&str) -// })) +// +// err := dec.DecodeArray( dec.DecodeArray(gojay.DecodeArrayFunc(func(dec *gojay.Decoder) error { +// var str string +// if err := dec.AddString(&str); err != nil { +// return err +// } +// strSlice = append(strSplice, str) +// return nil +// }))) type DecodeArrayFunc func(*Decoder) error // UnmarshalJSONArray implements UnmarshalerJSONArray. @@ -193,3 +200,40 @@ func (f DecodeArrayFunc) UnmarshalJSONArray(dec *Decoder) error { func (f DecodeArrayFunc) IsNil() bool { return f == nil } + +// Add Values functions + +// AddArray decodes the next key to a UnmarshalerJSONArray. +func (dec *Decoder) AddArray(v UnmarshalerJSONArray) error { + return dec.Array(v) +} + +// AddArrayNull decodes the next key to a UnmarshalerJSONArray. +func (dec *Decoder) AddArrayNull(v UnmarshalerJSONArray) error { + return dec.ArrayNull(v) +} + +// Array decodes the next key to a UnmarshalerJSONArray. +func (dec *Decoder) Array(v UnmarshalerJSONArray) error { + newCursor, err := dec.decodeArray(v) + if err != nil { + return err + } + dec.cursor = newCursor + dec.called |= 1 + return nil +} + +// ArrayNull decodes the next key to a UnmarshalerJSONArray. +// v should be a pointer to an UnmarshalerJSONArray, +// if `null` value is encountered in JSON, it will leave the value v untouched, +// else it will create a new instance of the UnmarshalerJSONArray behind v. +func (dec *Decoder) ArrayNull(v interface{}) error { + newCursor, err := dec.decodeArrayNull(v) + if err != nil { + return err + } + dec.cursor = newCursor + dec.called |= 1 + return nil +} diff --git a/decode_bool.go b/decode_bool.go @@ -1,6 +1,7 @@ package gojay -// DecodeBool reads the next JSON-encoded value from its input and stores it in the boolean pointed to by v. +// DecodeBool reads the next JSON-encoded value from the decoder's input (io.Reader) +// and stores it in the boolean pointed to by v. // // See the documentation for Unmarshal for details about the conversion of JSON into a Go value. func (dec *Decoder) DecodeBool(v *bool) error { @@ -197,3 +198,44 @@ func (dec *Decoder) assertFalse() error { } return dec.raiseInvalidJSONErr(dec.cursor) } + +// Add Values functions + +// AddBool decodes the next key to a *bool. +// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. +// If next key is null, bool will be false. +func (dec *Decoder) AddBool(v *bool) error { + return dec.Bool(v) +} + +// AddBoolNull decodes the next key to a *bool. +// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. +// If next key is null, bool will be false. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddBoolNull(v **bool) error { + return dec.BoolNull(v) +} + +// Bool decodes the next key to a *bool. +// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. +// If next key is null, bool will be false. +func (dec *Decoder) Bool(v *bool) error { + err := dec.decodeBool(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// BoolNull decodes the next key to a *bool. +// If next key is neither null nor a JSON boolean, an InvalidUnmarshalError will be returned. +// If next key is null, bool will be false. +func (dec *Decoder) BoolNull(v **bool) error { + err := dec.decodeBoolNull(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} diff --git a/decode_embedded_json.go b/decode_embedded_json.go @@ -70,6 +70,12 @@ func (dec *Decoder) decodeEmbeddedJSON(ej *EmbeddedJSON) error { // AddEmbeddedJSON adds an EmbeddedsJSON to the value pointed by v. // It can be used to delay JSON decoding or precompute a JSON encoding. func (dec *Decoder) AddEmbeddedJSON(v *EmbeddedJSON) error { + return dec.EmbeddedJSON(v) +} + +// EmbeddedJSON adds an EmbeddedsJSON to the value pointed by v. +// It can be used to delay JSON decoding or precompute a JSON encoding. +func (dec *Decoder) EmbeddedJSON(v *EmbeddedJSON) error { err := dec.decodeEmbeddedJSON(v) if err != nil { return err diff --git a/decode_interface.go b/decode_interface.go @@ -111,3 +111,20 @@ func (dec *Decoder) getObject() (start int, end int, err error) { err = dec.raiseInvalidJSONErr(dec.cursor) return } + +// Add Values functions + +// AddInterface decodes the next key to a interface{}. +func (dec *Decoder) AddInterface(v *interface{}) error { + return dec.Interface(v) +} + +// Interface decodes the next key to an interface{}. +func (dec *Decoder) Interface(value *interface{}) error { + err := dec.decodeInterface(value) + if err != nil { + return err + } + dec.called |= 1 + return nil +} diff --git a/decode_number_float.go b/decode_number_float.go @@ -417,3 +417,100 @@ func (dec *Decoder) getFloat32() (float32, error) { } return float32(dec.atoi32(start, end)), nil } + +// Add Values functions + +// AddFloat decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddFloat(v *float64) error { + return dec.Float64(v) +} + +// AddFloatNull decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddFloatNull(v **float64) error { + return dec.Float64Null(v) +} + +// AddFloat64 decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddFloat64(v *float64) error { + return dec.Float64(v) +} + +// AddFloat64Null decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddFloat64Null(v **float64) error { + return dec.Float64Null(v) +} + +// AddFloat32 decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddFloat32(v *float32) error { + return dec.Float32(v) +} + +// AddFloat32Null decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddFloat32Null(v **float32) error { + return dec.Float32Null(v) +} + +// Float decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Float(v *float64) error { + return dec.Float64(v) +} + +// FloatNull decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) FloatNull(v **float64) error { + return dec.Float64Null(v) +} + +// Float64 decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Float64(v *float64) error { + err := dec.decodeFloat64(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Float64Null decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Float64Null(v **float64) error { + err := dec.decodeFloat64Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Float32 decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Float32(v *float32) error { + err := dec.decodeFloat32(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Float32Null decodes the next key to a *float64. +// If next key value overflows float64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Float32Null(v **float32) error { + err := dec.decodeFloat32Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} diff --git a/decode_number_int.go b/decode_number_int.go @@ -1158,3 +1158,180 @@ func (dec *Decoder) atoi8(start, end int) int8 { } return val } + +// Add Values functions + +// AddInt decodes the next key to an *int. +// If next key value overflows int, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddInt(v *int) error { + return dec.Int(v) +} + +// AddIntNull decodes the next key to an *int. +// If next key value overflows int, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddIntNull(v **int) error { + return dec.IntNull(v) +} + +// AddInt8 decodes the next key to an *int. +// If next key value overflows int8, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddInt8(v *int8) error { + return dec.Int8(v) +} + +// AddInt8Null decodes the next key to an *int. +// If next key value overflows int8, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddInt8Null(v **int8) error { + return dec.Int8Null(v) +} + +// AddInt16 decodes the next key to an *int. +// If next key value overflows int16, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddInt16(v *int16) error { + return dec.Int16(v) +} + +// AddInt16Null decodes the next key to an *int. +// If next key value overflows int16, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddInt16Null(v **int16) error { + return dec.Int16Null(v) +} + +// AddInt32 decodes the next key to an *int. +// If next key value overflows int32, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddInt32(v *int32) error { + return dec.Int32(v) +} + +// AddInt32Null decodes the next key to an *int. +// If next key value overflows int32, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddInt32Null(v **int32) error { + return dec.Int32Null(v) +} + +// AddInt64 decodes the next key to an *int. +// If next key value overflows int64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddInt64(v *int64) error { + return dec.Int64(v) +} + +// AddInt64Null decodes the next key to an *int. +// If next key value overflows int64, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddInt64Null(v **int64) error { + return dec.Int64Null(v) +} + +// Int decodes the next key to an *int. +// If next key value overflows int, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int(v *int) error { + err := dec.decodeInt(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// IntNull decodes the next key to an *int. +// If next key value overflows int, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) IntNull(v **int) error { + err := dec.decodeIntNull(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int8 decodes the next key to an *int. +// If next key value overflows int8, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int8(v *int8) error { + err := dec.decodeInt8(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int8Null decodes the next key to an *int. +// If next key value overflows int8, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int8Null(v **int8) error { + err := dec.decodeInt8Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int16 decodes the next key to an *int. +// If next key value overflows int16, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int16(v *int16) error { + err := dec.decodeInt16(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int16Null decodes the next key to an *int. +// If next key value overflows int16, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int16Null(v **int16) error { + err := dec.decodeInt16Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int32 decodes the next key to an *int. +// If next key value overflows int32, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int32(v *int32) error { + err := dec.decodeInt32(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int32Null decodes the next key to an *int. +// If next key value overflows int32, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int32Null(v **int32) error { + err := dec.decodeInt32Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int64 decodes the next key to an *int. +// If next key value overflows int64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int64(v *int64) error { + err := dec.decodeInt64(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Int64Null decodes the next key to an *int. +// If next key value overflows int64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Int64Null(v **int64) error { + err := dec.decodeInt64Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} diff --git a/decode_number_uint.go b/decode_number_uint.go @@ -571,3 +571,145 @@ func (dec *Decoder) atoui8(start, end int) uint8 { } return val } + +// Add Values functions + +// AddUint8 decodes the next key to an *int. +// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddUint8(v *uint8) error { + return dec.Uint8(v) +} + +// AddUint8Null decodes the next key to an *int. +// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddUint8Null(v **uint8) error { + return dec.Uint8Null(v) +} + +// AddUint16 decodes the next key to an *int. +// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddUint16(v *uint16) error { + return dec.Uint16(v) +} + +// AddUint16Null decodes the next key to an *int. +// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddUint16Null(v **uint16) error { + return dec.Uint16Null(v) +} + +// AddUint32 decodes the next key to an *int. +// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddUint32(v *uint32) error { + return dec.Uint32(v) +} + +// AddUint32Null decodes the next key to an *int. +// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddUint32Null(v **uint32) error { + return dec.Uint32Null(v) +} + +// AddUint64 decodes the next key to an *int. +// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) AddUint64(v *uint64) error { + return dec.Uint64(v) +} + +// AddUint64Null decodes the next key to an *int. +// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddUint64Null(v **uint64) error { + return dec.Uint64Null(v) +} + +// Uint8 decodes the next key to an *int. +// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint8(v *uint8) error { + err := dec.decodeUint8(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Uint8Null decodes the next key to an *int. +// If next key value overflows uint8, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint8Null(v **uint8) error { + err := dec.decodeUint8Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Uint16 decodes the next key to an *int. +// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint16(v *uint16) error { + err := dec.decodeUint16(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Uint16Null decodes the next key to an *int. +// If next key value overflows uint16, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint16Null(v **uint16) error { + err := dec.decodeUint16Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Uint32 decodes the next key to an *int. +// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint32(v *uint32) error { + err := dec.decodeUint32(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Uint32Null decodes the next key to an *int. +// If next key value overflows uint32, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint32Null(v **uint32) error { + err := dec.decodeUint32Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Uint64 decodes the next key to an *int. +// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint64(v *uint64) error { + err := dec.decodeUint64(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// Uint64Null decodes the next key to an *int. +// If next key value overflows uint64, an InvalidUnmarshalError error will be returned. +func (dec *Decoder) Uint64Null(v **uint64) error { + err := dec.decodeUint64Null(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} diff --git a/decode_object.go b/decode_object.go @@ -360,3 +360,54 @@ func (f DecodeObjectFunc) UnmarshalJSONObject(dec *Decoder, k string) error { func (f DecodeObjectFunc) NKeys() int { return 0 } + +// Add Values functions + +// AddObject decodes the next key to a UnmarshalerJSONObject. +func (dec *Decoder) AddObject(v UnmarshalerJSONObject) error { + return dec.Object(v) +} + +// AddObjectNull decodes the next key to a UnmarshalerJSONObject. +func (dec *Decoder) AddObjectNull(v interface{}) error { + return dec.ObjectNull(v) +} + +// Object decodes the next key to a UnmarshalerJSONObject. +func (dec *Decoder) Object(value UnmarshalerJSONObject) error { + initialKeysDone := dec.keysDone + initialChild := dec.child + dec.keysDone = 0 + dec.called = 0 + dec.child |= 1 + newCursor, err := dec.decodeObject(value) + if err != nil { + return err + } + dec.cursor = newCursor + dec.keysDone = initialKeysDone + dec.child = initialChild + dec.called |= 1 + return nil +} + +// ObjectNull decodes the next key to a UnmarshalerJSONObject. +// v should be a pointer to an UnmarshalerJSONObject, +// if `null` value is encountered in JSON, it will leave the value v untouched, +// else it will create a new instance of the UnmarshalerJSONObject behind v. +func (dec *Decoder) ObjectNull(v interface{}) error { + initialKeysDone := dec.keysDone + initialChild := dec.child + dec.keysDone = 0 + dec.called = 0 + dec.child |= 1 + newCursor, err := dec.decodeObjectNull(v) + if err != nil { + return err + } + dec.cursor = newCursor + dec.keysDone = initialKeysDone + dec.child = initialChild + dec.called |= 1 + return nil +} diff --git a/decode_sqlnull.go b/decode_sqlnull.go @@ -73,3 +73,85 @@ func (dec *Decoder) decodeSQLNullBool(v *sql.NullBool) error { v.Valid = true return nil } + +// Add Values functions + +// AddSQLNullString decodes the next key to qn *sql.NullString +func (dec *Decoder) AddSQLNullString(v *sql.NullString) error { + return dec.SQLNullString(v) +} + +// SQLNullString decodes the next key to an *sql.NullString +func (dec *Decoder) SQLNullString(v *sql.NullString) error { + var b *string + if err := dec.StringNull(&b); err != nil { + return err + } + if b == nil { + v.Valid = false + } else { + v.String = *b + v.Valid = true + } + return nil +} + +// AddSQLNullInt64 decodes the next key to qn *sql.NullInt64 +func (dec *Decoder) AddSQLNullInt64(v *sql.NullInt64) error { + return dec.SQLNullInt64(v) +} + +// SQLNullInt64 decodes the next key to an *sql.NullInt64 +func (dec *Decoder) SQLNullInt64(v *sql.NullInt64) error { + var b *int64 + if err := dec.Int64Null(&b); err != nil { + return err + } + if b == nil { + v.Valid = false + } else { + v.Int64 = *b + v.Valid = true + } + return nil +} + +// AddSQLNullFloat64 decodes the next key to qn *sql.NullFloat64 +func (dec *Decoder) AddSQLNullFloat64(v *sql.NullFloat64) error { + return dec.SQLNullFloat64(v) +} + +// SQLNullFloat64 decodes the next key to an *sql.NullFloat64 +func (dec *Decoder) SQLNullFloat64(v *sql.NullFloat64) error { + var b *float64 + if err := dec.Float64Null(&b); err != nil { + return err + } + if b == nil { + v.Valid = false + } else { + v.Float64 = *b + v.Valid = true + } + return nil +} + +// AddSQLNullBool decodes the next key to an *sql.NullBool +func (dec *Decoder) AddSQLNullBool(v *sql.NullBool) error { + return dec.SQLNullBool(v) +} + +// SQLNullBool decodes the next key to an *sql.NullBool +func (dec *Decoder) SQLNullBool(v *sql.NullBool) error { + var b *bool + if err := dec.BoolNull(&b); err != nil { + return err + } + if b == nil { + v.Valid = false + } else { + v.Bool = *b + v.Valid = true + } + return nil +} diff --git a/decode_string.go b/decode_string.go @@ -213,3 +213,41 @@ func (dec *Decoder) skipString() error { } return dec.raiseInvalidJSONErr(len(dec.data) - 1) } + +// Add Values functions + +// AddString decodes the next key to a *string. +// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. +func (dec *Decoder) AddString(v *string) error { + return dec.String(v) +} + +// AddStringNull decodes the next key to a *string. +// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) AddStringNull(v **string) error { + return dec.StringNull(v) +} + +// String decodes the next key to a *string. +// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. +func (dec *Decoder) String(v *string) error { + err := dec.decodeString(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} + +// StringNull decodes the next key to a **string. +// If next key is not a JSON string nor null, InvalidUnmarshalError will be returned. +// If a `null` is encountered, gojay does not change the value of the pointer. +func (dec *Decoder) StringNull(v **string) error { + err := dec.decodeStringNull(v) + if err != nil { + return err + } + dec.called |= 1 + return nil +} diff --git a/decode_time.go b/decode_time.go @@ -34,3 +34,20 @@ func (dec *Decoder) decodeTime(v *time.Time, format string) error { *v = tt return nil } + +// Add Values functions + +// AddTime decodes the next key to a *time.Time with the given format +func (dec *Decoder) AddTime(v *time.Time, format string) error { + return dec.Time(v, format) +} + +// Time decodes the next key to a *time.Time with the given format +func (dec *Decoder) Time(v *time.Time, format string) error { + err := dec.decodeTime(v, format) + if err != nil { + return err + } + dec.called |= 1 + return nil +}