Documentation ¶
Index ¶
- Variables
- func Marshal(o interface{}) ([]byte, error)
- func Unmarshal(data []byte, o interface{}) error
- func UnmarshalFromReader(r io.Reader, o interface{}) error
- func UnmarshalFromString(s string, o interface{}) error
- func Valid(data []byte) bool
- type BadQuotedStringError
- type DecOpts
- type DecodeError
- type Decoder
- type DecoderConfig
- func (decCfg *DecoderConfig) NewDecoder(r io.Reader) *Decoder
- func (decCfg *DecoderConfig) NewIterator() *Iterator
- func (decCfg *DecoderConfig) Unmarshal(data []byte, obj interface{}) error
- func (decCfg *DecoderConfig) UnmarshalFromReader(r io.Reader, obj interface{}) error
- func (decCfg *DecoderConfig) UnmarshalFromString(s string, obj interface{}) error
- type DecoderOption
- type EncOpts
- type Encoder
- type EncoderConfig
- type EncoderOption
- type IntOverflowError
- type InvalidDigitError
- type InvalidEscapeCharError
- type InvalidFloatError
- type InvalidStringCharError
- type InvalidUnicodeCharError
- type Iterator
- func (it *Iterator) AppendRaw(in []byte) ([]byte, error)
- func (it *Iterator) Buffer() []byte
- func (it *Iterator) NextValueType() (ValueType, error)
- func (it *Iterator) Read() (interface{}, error)
- func (it *Iterator) ReadArrayBegin() (ret bool, err error)
- func (it *Iterator) ReadArrayCB(cb func(*Iterator) error) error
- func (it *Iterator) ReadArrayMore() (ret bool, err error)
- func (it *Iterator) ReadBool() (bool, error)
- func (it *Iterator) ReadFloat32() (float32, error)
- func (it *Iterator) ReadFloat64() (float64, error)
- func (it *Iterator) ReadInt() (int, error)
- func (it *Iterator) ReadInt16() (int16, error)
- func (it *Iterator) ReadInt32() (int32, error)
- func (it *Iterator) ReadInt64() (int64, error)
- func (it *Iterator) ReadInt8() (int8, error)
- func (it *Iterator) ReadNull() error
- func (it *Iterator) ReadNumber() (n Number, err error)
- func (it *Iterator) ReadObjectBegin() (_ bool, _ string, err error)
- func (it *Iterator) ReadObjectCB(cb func(it *Iterator, field string) error) error
- func (it *Iterator) ReadObjectMore() (_ bool, _ string, err error)
- func (it *Iterator) ReadRaw() ([]byte, error)
- func (it *Iterator) ReadString() (_ string, err error)
- func (it *Iterator) ReadStringAndAppend(buf []byte) (_ []byte, err error)
- func (it *Iterator) ReadStringAsSlice() (_ []byte, err error)
- func (it *Iterator) ReadUint() (uint, error)
- func (it *Iterator) ReadUint16() (uint16, error)
- func (it *Iterator) ReadUint32() (uint32, error)
- func (it *Iterator) ReadUint64() (uint64, error)
- func (it *Iterator) ReadUint8() (uint8, error)
- func (it *Iterator) ReadVal(obj interface{}) error
- func (it *Iterator) Release()
- func (it *Iterator) Reset(r io.Reader)
- func (it *Iterator) ResetBytes(data []byte)
- func (it *Iterator) Skip() error
- func (it *Iterator) SkipArray() error
- func (it *Iterator) SkipNumber() error
- func (it *Iterator) SkipObject() error
- func (it *Iterator) SkipRaw() ([]byte, error)
- func (it *Iterator) SkipString() error
- func (it *Iterator) Unmarshal(data []byte, obj interface{}) error
- func (it *Iterator) UnmarshalFromReader(r io.Reader, obj interface{}) error
- func (it *Iterator) Valid(data []byte) bool
- func (it *Iterator) WrapError(err error) *DecodeError
- type Number
- type Streamer
- func (s *Streamer) ArrayEnd() *Streamer
- func (s *Streamer) ArrayStart() *Streamer
- func (s *Streamer) Bool(b bool) *Streamer
- func (s *Streamer) EscapeHTML(on bool)
- func (s *Streamer) False() *Streamer
- func (s *Streamer) Field(field string) *Streamer
- func (s *Streamer) Float32(f float32) *Streamer
- func (s *Streamer) Float64(f float64) *Streamer
- func (s *Streamer) Flush() error
- func (s *Streamer) Int(v int) *Streamer
- func (s *Streamer) Int16(v int16) *Streamer
- func (s *Streamer) Int32(v int32) *Streamer
- func (s *Streamer) Int64(v int64) *Streamer
- func (s *Streamer) Int8(v int8) *Streamer
- func (s *Streamer) Null() *Streamer
- func (s *Streamer) ObjectEnd() *Streamer
- func (s *Streamer) ObjectStart() *Streamer
- func (s *Streamer) Raw(raw []byte) *Streamer
- func (s *Streamer) RawField(b []byte) *Streamer
- func (s *Streamer) RawString(raw string) *Streamer
- func (s *Streamer) Release()
- func (s *Streamer) Reset(w io.Writer)
- func (s *Streamer) String(str string) *Streamer
- func (s *Streamer) True() *Streamer
- func (s *Streamer) Uint(v uint) *Streamer
- func (s *Streamer) Uint16(v uint16) *Streamer
- func (s *Streamer) Uint32(v uint32) *Streamer
- func (s *Streamer) Uint64(v uint64) *Streamer
- func (s *Streamer) Uint8(v uint8) *Streamer
- func (s *Streamer) Value(obj interface{}) *Streamer
- type TypeNotSupportedError
- type UnexpectedByteError
- type UnknownFieldError
- type ValDecoder
- type ValEncoder
- type ValueType
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultDecoderConfig is compatible with standard lib DefaultDecoderConfig = NewDecoderConfig(nil) )
var ( // DefaultEncoderConfig is compatible with standard lib DefaultEncoderConfig = NewEncoderConfig(nil) )
var ErrDataRemained = errors.New("expecting EOF, but there is still data")
ErrDataRemained there is still data remained in the buffer normally returned by Unmarshal methods
var ErrEfaceLooping = errors.New("eface looping detected")
ErrEfaceLooping if we encountered a loop for example:
type iface interface{} var o1 iface o1 = &o1 err := DefaultDecoderConfig.Unmarshal([]byte(`1`), o1)
var ErrEmptyIFace = errors.New("cannot unmarshal on empty iface")
ErrEmptyIFace if the decode target is an empty interface (with method)
var ErrFloatIsInfinity = errors.New("float is infinity")
ErrFloatIsInfinity the float to write is infinity
var ErrFloatIsNan = errors.New("float is NaN")
ErrFloatIsNan the float to write is NaN
var ErrNilEmbeddedPointer = errors.New("cannot unmarshal on nil pointer (unexported embedded)")
ErrNilEmbeddedPointer if the decode target has an unexported nil field
var ErrNilPointerReceiver = errors.New("the receiver is nil")
ErrNilPointerReceiver if the decode target is nil
var ErrNoAttachedWriter = errors.New("no attached writer")
ErrNoAttachedWriter there is no writer attaching to the streamer
var ErrPointerReceiver = errors.New("the receiver is not a pointer")
ErrPointerReceiver if the decode target is not a pointer
Functions ¶
func Unmarshal ¶
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to
func UnmarshalFromReader ¶ added in v0.0.4
UnmarshalFromReader behave like json.Unmarshal but with an io.Reader
func UnmarshalFromString ¶ added in v0.0.6
UnmarshalFromString behave like json.Unmarshal but with a string
Types ¶
type BadQuotedStringError ¶
type BadQuotedStringError string
BadQuotedStringError the value of field is not correctly quoted
func (BadQuotedStringError) Error ¶
func (e BadQuotedStringError) Error() string
type DecodeError ¶
type DecodeError struct {
// contains filtered or unexported fields
}
DecodeError describes the encountered error and its location.
func (*DecodeError) Error ¶
func (e *DecodeError) Error() string
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is almost standard library compatible The following standard methods are not implemented - Token
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
func (*Decoder) Buffered ¶ added in v0.0.5
Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to Decode.
func (*Decoder) Decode ¶ added in v0.0.5
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.
func (*Decoder) DisallowUnknownFields ¶ added in v0.0.5
func (dec *Decoder) DisallowUnknownFields()
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.
func (*Decoder) InputOffset ¶ added in v0.0.5
InputOffset returns the input stream byte offset of the current decoder position. The offset gives the location of the end of the most recently returned token and the beginning of the next token. Whitespace may present at the position of offset
func (*Decoder) More ¶ added in v0.0.5
More reports whether there is another element in the current array or object being parsed.
type DecoderConfig ¶ added in v0.0.5
type DecoderConfig struct {
// contains filtered or unexported fields
}
DecoderConfig is a frozen config for decoding
func NewDecoderConfig ¶ added in v0.0.5
func NewDecoderConfig(opt *DecoderOption) *DecoderConfig
NewDecoderConfig returns a new decoder config If the input option is nil, the default option will be applied
func (*DecoderConfig) NewDecoder ¶ added in v0.0.5
func (decCfg *DecoderConfig) NewDecoder(r io.Reader) *Decoder
NewDecoder returns a new decoder that reads from r.
func (*DecoderConfig) NewIterator ¶ added in v0.0.5
func (decCfg *DecoderConfig) NewIterator() *Iterator
NewIterator returns a new iterator.
func (*DecoderConfig) Unmarshal ¶ added in v0.0.5
func (decCfg *DecoderConfig) Unmarshal(data []byte, obj interface{}) error
Unmarshal behave like json.Unmarshal
func (*DecoderConfig) UnmarshalFromReader ¶ added in v0.0.5
func (decCfg *DecoderConfig) UnmarshalFromReader(r io.Reader, obj interface{}) error
UnmarshalFromReader behave like json.Unmarshal but with an io.Reader
func (*DecoderConfig) UnmarshalFromString ¶ added in v0.0.5
func (decCfg *DecoderConfig) UnmarshalFromString(s string, obj interface{}) error
UnmarshalFromString behave like json.Unmarshal but with a string
type DecoderOption ¶
type DecoderOption struct { // custom value decoders ValDecoders map[reflect.Type]ValDecoder // if the object key is case sensitive // `false` by default CaseSensitive bool // the tag name for structures // `json` by default Tag string OnlyTaggedField bool UseNumber bool DisallowUnknownFields bool }
DecoderOption can be used to customize the decoder config
type EncOpts ¶
type EncOpts struct {
Quoted bool
}
EncOpts is the context related encoding option of the object
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder is almost standard library compatible The following standard methods are not implemented - SetIndent
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
func (*Encoder) Encode ¶ added in v0.0.5
Encode writes the JSON encoding of v to the stream, followed by a newline character.
func (*Encoder) Release ¶ added in v0.0.5
func (e *Encoder) Release()
Release encoder, encoder should not be reused after call
func (*Encoder) SetEscapeHTML ¶ added in v0.0.5
SetEscapeHTML specifies whether problematic HTML characters should be escaped inside JSON quoted strings. The default behavior is to escape &, <, and > to \u0026, \u003c, and \u003e to avoid certain safety problems that can arise when embedding JSON in HTML.
In non-HTML settings where the escaping interferes with the readability of the output, SetEscapeHTML(false) disables this behavior.
type EncoderConfig ¶ added in v0.0.5
type EncoderConfig struct {
// contains filtered or unexported fields
}
EncoderConfig is a frozen config for encoding
func NewEncoderConfig ¶ added in v0.0.5
func NewEncoderConfig(opt *EncoderOption) *EncoderConfig
NewEncoderConfig returns a new encoder config If the input option is nil, the default option will be applied
func (*EncoderConfig) Marshal ¶ added in v0.0.5
func (encCfg *EncoderConfig) Marshal(obj interface{}) ([]byte, error)
Marshal behave like json.Marshal
func (*EncoderConfig) NewEncoder ¶ added in v0.0.5
func (encCfg *EncoderConfig) NewEncoder(w io.Writer) *Encoder
NewEncoder returns a new encoder that writes to w.
func (*EncoderConfig) NewStreamer ¶ added in v0.0.5
func (encCfg *EncoderConfig) NewStreamer() *Streamer
NewStreamer returns a new streamer.
type EncoderOption ¶
type EncoderOption struct { ValEncoders map[reflect.Type]ValEncoder EscapeHTML bool Tag string OnlyTaggedField bool }
EncoderOption can be used to customize the encoder config
type IntOverflowError ¶
type IntOverflowError struct {
// contains filtered or unexported fields
}
IntOverflowError the integer overflows.
func (IntOverflowError) Error ¶
func (e IntOverflowError) Error() string
type InvalidDigitError ¶
type InvalidDigitError struct {
// contains filtered or unexported fields
}
InvalidDigitError there is an invalid digit when reading number
func (InvalidDigitError) Error ¶
func (e InvalidDigitError) Error() string
type InvalidEscapeCharError ¶
type InvalidEscapeCharError struct {
// contains filtered or unexported fields
}
InvalidEscapeCharError there is an invalid escape character (when reading string)
func (InvalidEscapeCharError) Error ¶
func (e InvalidEscapeCharError) Error() string
type InvalidFloatError ¶
type InvalidFloatError struct {
// contains filtered or unexported fields
}
InvalidFloatError there is an invalid digit when reading float
func (InvalidFloatError) Error ¶
func (e InvalidFloatError) Error() string
type InvalidStringCharError ¶
type InvalidStringCharError struct {
// contains filtered or unexported fields
}
InvalidStringCharError there is an invalid character when reading string
func (InvalidStringCharError) Error ¶
func (e InvalidStringCharError) Error() string
type InvalidUnicodeCharError ¶
type InvalidUnicodeCharError struct {
// contains filtered or unexported fields
}
InvalidUnicodeCharError there is an invalid unicode character (when reading string)
func (InvalidUnicodeCharError) Error ¶
func (e InvalidUnicodeCharError) Error() string
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator is designed for one-shot use, each reuse must call reset first
func (*Iterator) NextValueType ¶
NextValueType read until the first valid token is found, only the whitespaces are consumed
func (*Iterator) ReadArrayBegin ¶
ReadArrayBegin starts to read an array
var (
more bool err error
) for more, err = it.ReadArray();
more; more, err = it.ReadArrayMore() { }
if err != nil { // error handling }
func (*Iterator) ReadArrayCB ¶
ReadArrayCB reads the array with a callback The caller should make sure that the callback is correct
func (*Iterator) ReadArrayMore ¶
ReadArrayMore tells if there is more item to read in the array
func (*Iterator) ReadFloat32 ¶
ReadFloat32 reads a float32 value
func (*Iterator) ReadFloat64 ¶
ReadFloat64 reads a float64 value
func (*Iterator) ReadNumber ¶
ReadNumber reads a Number(json.Number)
func (*Iterator) ReadObjectBegin ¶
ReadObjectBegin starts to read an object
func (*Iterator) ReadObjectCB ¶
ReadObjectCB reads the object with a callback The caller should make sure that the callback is correct
func (*Iterator) ReadObjectMore ¶
ReadObjectMore tells if there is more field to read in the object
func (*Iterator) ReadString ¶
ReadString reads a string
func (*Iterator) ReadStringAndAppend ¶
ReadStringAndAppend reads a string and appends to a byte slice
func (*Iterator) ReadStringAsSlice ¶
ReadStringAsSlice reads a string as a byte slice The returned slice can only be used temporarily, a copy must be made if the result needs to be saved
func (*Iterator) ReadUint16 ¶
ReadUint16 reads an uint16 value
func (*Iterator) ReadUint32 ¶
ReadUint32 reads an uint32 value
func (*Iterator) ReadUint64 ¶
ReadUint64 reads an uint64 value
func (*Iterator) Release ¶ added in v0.0.5
func (it *Iterator) Release()
Release the iterator, the iterator should not be reused after call.
func (*Iterator) Reset ¶
Reset the iterator with an io.Reader if the reader is nil, reset the iterator to its initial state
In reset methods, explicit assignment is faster than then following
*it = Iterator{ ... }
When the above code is used, runtime.duffcopy and runtime.duffzero will be used which will slow down our code (correct me if I am wrong)
func (*Iterator) ResetBytes ¶
ResetBytes resets iterator with a byte slice
func (*Iterator) SkipRaw ¶
SkipRaw skips and returns the bytes skipped slice will not be copied, so make a copy if the return value is to be stored somewhere
func (*Iterator) UnmarshalFromReader ¶ added in v0.0.3
UnmarshalFromReader behave like standard json.Unmarshal but with an io.Reader
func (*Iterator) WrapError ¶
func (it *Iterator) WrapError(err error) *DecodeError
WrapError wraps the error with the current iterator location
type Streamer ¶
type Streamer struct { Error error // TODO: 1. type of context? // TODO: 2. should context be reset as well? Context interface{} // custom stream context // contains filtered or unexported fields }
Streamer is a chained class for encoding object to json
func (*Streamer) ArrayStart ¶
ArrayStart starts to write an array
func (*Streamer) EscapeHTML ¶ added in v0.0.5
EscapeHTML set if the string should be html-escaped
func (*Streamer) ObjectStart ¶
ObjectStart starts to write an object
func (*Streamer) Release ¶ added in v0.0.5
func (s *Streamer) Release()
Release the streamer, the streamer should not be used after call
type TypeNotSupportedError ¶
type TypeNotSupportedError string
TypeNotSupportedError the decode target is not supported
func (TypeNotSupportedError) Error ¶
func (e TypeNotSupportedError) Error() string
type UnexpectedByteError ¶
type UnexpectedByteError struct {
// contains filtered or unexported fields
}
UnexpectedByteError there is an unexpected character
func (UnexpectedByteError) Error ¶
func (e UnexpectedByteError) Error() string
type UnknownFieldError ¶
type UnknownFieldError string
UnknownFieldError there is an unknown field when decoding
func (UnknownFieldError) Error ¶
func (e UnknownFieldError) Error() string
type ValDecoder ¶
ValDecoder is the interface to be implemented for custom decoder
type ValEncoder ¶
type ValEncoder interface { IsEmpty(ptr unsafe.Pointer) bool Encode(ptr unsafe.Pointer, s *Streamer, opts *EncOpts) }
ValEncoder is the interface to be implemented for custom encoder
type ValueType ¶
type ValueType int
ValueType is the type of the next json token
const ( // WhiteSpaceValue the next token is whitespace WhiteSpaceValue ValueType = iota // InvalidValue an error occurred InvalidValue // StringValue the next token is string StringValue // NumberValue the next token is number NumberValue // ObjectValue the next token is object ObjectValue // ArrayValue the next token is array ArrayValue // BoolValue the next token is a boolean value BoolValue // NullValue the next token is null NullValue // LastValue is a counter, should not be used LastValue )
Source Files ¶
- build.go
- convert.go
- decoder.go
- decoder_config.go
- decoder_config_adaptor.go
- eface.go
- encoder.go
- encoder_config.go
- encoder_config_adaptor.go
- errors_decoder.go
- errors_encoder.go
- helper.go
- iface.go
- is_empty.go
- iterator.go
- iterator_array.go
- iterator_bool.go
- iterator_float.go
- iterator_float32.go
- iterator_float64.go
- iterator_int.go
- iterator_int16.go
- iterator_int32.go
- iterator_int64.go
- iterator_int8.go
- iterator_null.go
- iterator_number.go
- iterator_object.go
- iterator_pool.go
- iterator_raw.go
- iterator_read.go
- iterator_read_array.go
- iterator_read_object.go
- iterator_skip.go
- iterator_skip_array.go
- iterator_skip_number.go
- iterator_skip_object.go
- iterator_skip_str.go
- iterator_str.go
- iterator_val.go
- link_reflect.go
- node.go
- number.go
- stack.go
- standard_compatible.go
- standard_like.go
- streamer.go
- streamer_float.go
- streamer_int.go
- streamer_pool.go
- streamer_str.go
- streamer_val.go
- struct.go
- struct_field.go
- struct_field_fold.go
- struct_field_upper.go
- struct_tag.go
- type_queue.go
- val_decoder.go
- val_decoder_json_number.go
- val_decoder_json_rawmessage.go
- val_decoder_json_unmarshaler.go
- val_decoder_native.go
- val_decoder_native_array.go
- val_decoder_native_float.go
- val_decoder_native_int.go
- val_decoder_native_interface.go
- val_decoder_native_map.go
- val_decoder_native_ptr.go
- val_decoder_native_slice.go
- val_decoder_native_struct.go
- val_decoder_native_struct_one_field.go
- val_decoder_native_struct_small.go
- val_decoder_native_uint.go
- val_decoder_skip.go
- val_decoder_text_unmarshaler.go
- val_encoder.go
- val_encoder_direct.go
- val_encoder_json_marshaler.go
- val_encoder_json_number.go
- val_encoder_json_rawmessage.go
- val_encoder_native.go
- val_encoder_native_array.go
- val_encoder_native_base64.go
- val_encoder_native_float.go
- val_encoder_native_int.go
- val_encoder_native_interface.go
- val_encoder_native_map.go
- val_encoder_native_ptr.go
- val_encoder_native_slice.go
- val_encoder_native_struct.go
- val_encoder_native_uint.go
- val_encoder_text_marshaler.go
- value_type.go