Documentation
¶
Overview ¶
Package bigjsonvalue provides types to replace interface{} for decoding unknown JSON values
Index ¶
- Constants
- Variables
- type BigJSONValue
- func (bjv *BigJSONValue) BigFloat() big.Float
- func (bjv *BigJSONValue) BigInt() big.Int
- func (bjv *BigJSONValue) Bool() bool
- func (bjv *BigJSONValue) DecodeJSONValue(text string) (*BigJSONValue, error)
- func (bjv *BigJSONValue) IsBigFloat() bool
- func (bjv *BigJSONValue) IsBigInt() bool
- func (bjv *BigJSONValue) IsBool() bool
- func (bjv *BigJSONValue) IsNil() bool
- func (bjv *BigJSONValue) IsString() bool
- func (bjv *BigJSONValue) Kind() Kind
- func (bjv *BigJSONValue) String() string
- func (bjv *BigJSONValue) UnmarshalJSON(text []byte) error
- func (bjv *BigJSONValue) Value() interface{}
- type Kind
- type NatJSONValue
- func (njv *NatJSONValue) Bool() bool
- func (njv *NatJSONValue) DecodeJSONValue(text string) (*NatJSONValue, error)
- func (njv *NatJSONValue) Float64() float64
- func (njv *NatJSONValue) Int64() int64
- func (njv *NatJSONValue) IsBool() bool
- func (njv *NatJSONValue) IsFloat64() bool
- func (njv *NatJSONValue) IsInt64() bool
- func (njv *NatJSONValue) IsNil() bool
- func (njv *NatJSONValue) IsString() bool
- func (njv *NatJSONValue) IsUint64() bool
- func (njv *NatJSONValue) Kind() Kind
- func (njv *NatJSONValue) String() string
- func (njv *NatJSONValue) Uint64() uint64
- func (njv *NatJSONValue) UnmarshalJSON(text []byte) error
- func (njv *NatJSONValue) Value() interface{}
Constants ¶
const ( // JSONNumRegexpPat defines the regexp pattern for matching JSON numbers // (integers or floats) based on http://json.org JSONNumRegexpPat = `^-?\d+(\.\d+)?([eE][-+]?\d+)?$` )
Package constants
Variables ¶
var ( // ErrInvalidJSON defines the invalid JSON error ErrInvalidJSON = errors.New("invalid JSON") // ErrNotImplemented defines the not-implemented error ErrNotImplemented = errors.New("not implemented") )
Package errors
Functions ¶
This section is empty.
Types ¶
type BigJSONValue ¶
type BigJSONValue struct {
// contains filtered or unexported fields
}
BigJSONValue is wrapper around interface{} type to force json.Unmarshal() to decode integer values as big.Int instead of float64. The problem with float64 is that it doesn't have enough precision to store exact values of large int64 and uint64 values. Instead of trying to unmarshal JSON into an interface{}, unmarshal into a BigJSONValue instead.
Compared to NatJSONValue, BigJSONValue uses big.Int and big.Float to store arbitrary-precision numbers, but is slower than NatJSONValue.
func (*BigJSONValue) BigFloat ¶
func (bjv *BigJSONValue) BigFloat() big.Float
BigFloat returns the underlying big.Float value. Panics with runtime error if not a big.Float.
func (*BigJSONValue) BigInt ¶
func (bjv *BigJSONValue) BigInt() big.Int
BigInt returns the underlying big.Int value. Panics with runtime error if not a big.Int.
func (*BigJSONValue) Bool ¶
func (bjv *BigJSONValue) Bool() bool
Bool returns the underlying bool value. Panics with runtime error if not a bool.
func (*BigJSONValue) DecodeJSONValue ¶
func (bjv *BigJSONValue) DecodeJSONValue(text string) (*BigJSONValue, error)
DecodeJSONValue decodes a JSON value, and returns itself. Results are undefined if error is returned.
The text "null" is decoded as a nil value.
The text "true" and "false" are decoded as bool values.
Text surrounded by double-quotes are decoded as string values.
Number text containing period "." or the letters "e" or "E" are decoded as big.Float values.
Otherwise, number text is decoded as big.Int values. Whether text is considered a number is based on http://json.org
func (*BigJSONValue) IsBigFloat ¶
func (bjv *BigJSONValue) IsBigFloat() bool
IsBigFloat returns true if value is a big.Float.
func (*BigJSONValue) IsBigInt ¶
func (bjv *BigJSONValue) IsBigInt() bool
IsBigInt returns true if value is a big.Int.
func (*BigJSONValue) IsBool ¶
func (bjv *BigJSONValue) IsBool() bool
IsBool returns true if value is a bool.
func (*BigJSONValue) IsNil ¶
func (bjv *BigJSONValue) IsNil() bool
IsNil returns true if value is nil.
func (*BigJSONValue) IsString ¶
func (bjv *BigJSONValue) IsString() bool
IsString returns true if value is a string.
func (*BigJSONValue) Kind ¶
func (bjv *BigJSONValue) Kind() Kind
Kind returns the kind of BigJSONValue it is holding:
Returns Bool if value is a bool.
Returns String if value is a string.
Returns BigInt if value is a big.Int.
Returns BigFloat if value is a big.Float.
Otherwise returns Nil.
func (*BigJSONValue) String ¶
func (bjv *BigJSONValue) String() string
String implements fmt.Stringer interface for BigJSONValue.
Bool values return "true" or "false".
String values return as-is (no surround double-quotes are added).
Number values return with as much precision as possible.
Nil values return "nil".
func (*BigJSONValue) UnmarshalJSON ¶
func (bjv *BigJSONValue) UnmarshalJSON(text []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for BigJSONValue
func (*BigJSONValue) Value ¶
func (bjv *BigJSONValue) Value() interface{}
Value returns the underlying interface{} value that is being wrapped.
type Kind ¶
type Kind uint
Kind enumerates the kind of underlying value being held by the wrapped interface{} value
Kind enumeration constants
type NatJSONValue ¶
type NatJSONValue struct {
// contains filtered or unexported fields
}
NatJSONValue is wrapper around interface{} type to force json.Unmarshal() to decode integer values as int64 or uint64 instead of float64. The problem with float64 is that it doesn't have enough precision to store exact values of large int64 and uint64 values. Instead of trying to unmarshal JSON into an interface{}, unmarshal into a NatJSONValue instead.
Compared to BigJSONValue, NatJSONValue uses native Golang number types int64, uint64, and float64 to store numbers, so is faster than BigJSONValue.
func (*NatJSONValue) Bool ¶
func (njv *NatJSONValue) Bool() bool
Bool returns the underlying bool value. Panics with runtime error if not a bool.
func (*NatJSONValue) DecodeJSONValue ¶
func (njv *NatJSONValue) DecodeJSONValue(text string) (*NatJSONValue, error)
DecodeJSONValue decodes a JSON value, and returns itself. Results are undefined if error is returned.
The text "null" is decoded as a nil value.
The text "true" and "false" are decoded as bool values.
Text surrounded by double-quotes are decoded as string values.
Number text containing period "." or the letters "e" or "E" are decoded as float64 values.
Otherwise, number text is decoded as int64 for negative values or uint64 for positive values. Whether text is considered a number is based on http://json.org
func (*NatJSONValue) Float64 ¶
func (njv *NatJSONValue) Float64() float64
Float64 returns the underlying float64 value. Panics with runtime error if not a float64.
func (*NatJSONValue) Int64 ¶
func (njv *NatJSONValue) Int64() int64
Int64 returns the underlying int64 value. Panics with runtime error if not a int64.
func (*NatJSONValue) IsBool ¶
func (njv *NatJSONValue) IsBool() bool
IsBool returns true if value is a bool.
func (*NatJSONValue) IsFloat64 ¶
func (njv *NatJSONValue) IsFloat64() bool
IsFloat64 returns true if value is a float64.
func (*NatJSONValue) IsInt64 ¶
func (njv *NatJSONValue) IsInt64() bool
IsInt64 returns true if value is a int64.
func (*NatJSONValue) IsNil ¶
func (njv *NatJSONValue) IsNil() bool
IsNil returns true if value is nil.
func (*NatJSONValue) IsString ¶
func (njv *NatJSONValue) IsString() bool
IsString returns true if value is a string.
func (*NatJSONValue) IsUint64 ¶
func (njv *NatJSONValue) IsUint64() bool
IsUint64 returns true if value is a uint64.
func (*NatJSONValue) Kind ¶
func (njv *NatJSONValue) Kind() Kind
Kind returns the kind of NatJSONValue it is holding:
Returns Bool if value is a bool.
Returns String if value is a string.
Returns Int64 if value is a int64.
Returns Uint64 if value is a uint64.
Returns Float64 if value is a float64.
Otherwise returns Nil.
func (*NatJSONValue) String ¶
func (njv *NatJSONValue) String() string
String implements fmt.Stringer interface for NatJSONValue.
Bool values return "true" or "false".
String values return as-is (no surround double-quotes are added).
Number values return with as much precision as possible.
Nil values return "nil".
func (*NatJSONValue) Uint64 ¶
func (njv *NatJSONValue) Uint64() uint64
Uint64 returns the underlying uint64 value. Panics with runtime error if not a uint64.
func (*NatJSONValue) UnmarshalJSON ¶
func (njv *NatJSONValue) UnmarshalJSON(text []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for NatJSONValue
func (*NatJSONValue) Value ¶
func (njv *NatJSONValue) Value() interface{}
Value returns the underlying interface{} value that is being wrapped.