Documentation
¶
Overview ¶
Package jsoniter implements encoding and decoding of JSON as defined in RFC 4627 and provides interfaces with identical syntax of standard lib encoding/json. Converting from encoding/json to jsoniter is no more than replacing the package with jsoniter and variable type declarations (if any). jsoniter interfaces gives 100% compatibility with code using standard lib.
"JSON and Go" (https://golang.org/doc/articles/json_and_go.html) gives a description of how Marshal/Unmarshal operate between arbitrary or predefined json objects and bytes, and it applies to jsoniter.Marshal/Unmarshal as well.
Besides, jsoniter.Iterator provides a different set of interfaces iterating given bytes/string/reader and yielding parsed elements one by one. This set of interfaces reads input as required and gives better performance.
Index ¶
- Variables
- func Marshal(v interface{}) ([]byte, error)
- func MarshalToString(v interface{}) (string, error)
- func RegisterExtension(extension ExtensionFunc)
- func RegisterFieldDecoder(typ string, field string, fun DecoderFunc)
- func RegisterFieldEncoder(typ string, field string, fun EncoderFunc)
- func RegisterTypeDecoder(typ string, fun DecoderFunc)
- func RegisterTypeEncoder(typ string, fun EncoderFunc)
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalFromString(str string, v interface{}) error
- type AdaptedDecoder
- type AdaptedEncoder
- type Any
- func UnmarshalAny(data []byte) (Any, error)
- func UnmarshalAnyFromString(str string) (Any, error)
- func Wrap(val interface{}) Any
- func WrapFloat64(val float64) Any
- func WrapInt32(val int32) Any
- func WrapInt64(val int64) Any
- func WrapString(val string) Any
- func WrapUint32(val uint32) Any
- func WrapUint64(val uint64) Any
- type Config
- type Decoder
- type DecoderFunc
- type Encoder
- type EncoderFunc
- type ExtensionFunc
- type Iterator
- func (iter *Iterator) CurrentBuffer() string
- func (iter *Iterator) Read() interface{}
- func (iter *Iterator) ReadAny() Any
- func (iter *Iterator) ReadArray() (ret bool)
- func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool)
- func (iter *Iterator) ReadBase64() (ret []byte)
- func (iter *Iterator) ReadBigFloat() (ret *big.Float)
- func (iter *Iterator) ReadBigInt() (ret *big.Int)
- func (iter *Iterator) ReadBool() (ret bool)
- func (iter *Iterator) ReadFloat32() (ret float32)
- func (iter *Iterator) ReadFloat64() (ret float64)
- func (iter *Iterator) ReadInt() int
- func (iter *Iterator) ReadInt16() (ret int16)
- func (iter *Iterator) ReadInt32() (ret int32)
- func (iter *Iterator) ReadInt64() (ret int64)
- func (iter *Iterator) ReadInt8() (ret int8)
- func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool
- func (iter *Iterator) ReadNil() (ret bool)
- func (iter *Iterator) ReadObject() (ret string)
- func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool
- func (iter *Iterator) ReadString() (ret string)
- func (iter *Iterator) ReadStringAsSlice() (ret []byte)
- func (iter *Iterator) ReadUint() uint
- func (iter *Iterator) ReadUint16() (ret uint16)
- func (iter *Iterator) ReadUint32() (ret uint32)
- func (iter *Iterator) ReadUint64() uint64
- func (iter *Iterator) ReadUint8() (ret uint8)
- func (iter *Iterator) ReadVal(obj interface{})
- func (iter *Iterator) Reset(reader io.Reader) *Iterator
- func (iter *Iterator) ResetBytes(input []byte) *Iterator
- func (iter *Iterator) Skip()
- func (iter *Iterator) SkipAndReturnBytes() []byte
- func (iter *Iterator) WhatIsNext() ValueType
- type Stream
- func (b *Stream) Available() int
- func (b *Stream) Buffer() []byte
- func (b *Stream) Buffered() int
- func (b *Stream) Flush() error
- func (b *Stream) Reset(out io.Writer)
- func (b *Stream) Write(p []byte) (nn int, err error)
- func (stream *Stream) WriteArrayEnd()
- func (stream *Stream) WriteArrayStart()
- func (stream *Stream) WriteBool(val bool)
- func (stream *Stream) WriteEmptyArray()
- func (stream *Stream) WriteEmptyObject()
- func (stream *Stream) WriteFalse()
- func (stream *Stream) WriteFloat32(val float32)
- func (stream *Stream) WriteFloat32Lossy(val float32)
- func (stream *Stream) WriteFloat64(val float64)
- func (stream *Stream) WriteFloat64Lossy(val float64)
- func (stream *Stream) WriteInt(val int)
- func (stream *Stream) WriteInt16(nval int16)
- func (stream *Stream) WriteInt32(nval int32)
- func (stream *Stream) WriteInt64(nval int64)
- func (stream *Stream) WriteInt8(nval int8)
- func (stream *Stream) WriteMore()
- func (stream *Stream) WriteNil()
- func (stream *Stream) WriteObjectEnd()
- func (stream *Stream) WriteObjectField(field string)
- func (stream *Stream) WriteObjectStart()
- func (b *Stream) WriteRaw(s string)
- func (stream *Stream) WriteString(s string)
- func (stream *Stream) WriteTrue()
- func (stream *Stream) WriteUint(val uint)
- func (stream *Stream) WriteUint16(val uint16)
- func (stream *Stream) WriteUint32(val uint32)
- func (stream *Stream) WriteUint64(val uint64)
- func (stream *Stream) WriteUint8(val uint8)
- func (stream *Stream) WriteVal(val interface{})
- type ValueType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DEFAULT_CONFIG = Config{}.Froze()
Functions ¶
func Marshal ¶
Marshal adapts to json/encoding Marshal API
Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API Refer to https://godoc.org/encoding/json#Marshal for more information
Example ¶
type ColorGroup struct { ID int Name string Colors []string } group := ColorGroup{ ID: 1, Name: "Reds", Colors: []string{"Crimson", "Red", "Ruby", "Maroon"}, } b, err := jsoniter.Marshal(group) if err != nil { fmt.Println("error:", err) } os.Stdout.Write(b)
Output: {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
func MarshalToString ¶
func RegisterExtension ¶
func RegisterExtension(extension ExtensionFunc)
RegisterExtension can register a custom extension
func RegisterFieldDecoder ¶
func RegisterFieldDecoder(typ string, field string, fun DecoderFunc)
RegisterFieldDecoder can register a type for json field
func RegisterFieldEncoder ¶
func RegisterFieldEncoder(typ string, field string, fun EncoderFunc)
func RegisterTypeDecoder ¶
func RegisterTypeDecoder(typ string, fun DecoderFunc)
RegisterTypeDecoder can register a type for json object
func RegisterTypeEncoder ¶
func RegisterTypeEncoder(typ string, fun EncoderFunc)
func Unmarshal ¶
Unmarshal adapts to json/encoding Unmarshal API
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. Refer to https://godoc.org/encoding/json#Unmarshal for more information
Example ¶
var jsonBlob = []byte(`[ {"Name": "Platypus", "Order": "Monotremata"}, {"Name": "Quoll", "Order": "Dasyuromorphia"} ]`) type Animal struct { Name string Order string } var animals []Animal err := jsoniter.Unmarshal(jsonBlob, &animals) if err != nil { fmt.Println("error:", err) } fmt.Printf("%+v", animals)
Output: [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
func UnmarshalFromString ¶
Types ¶
type AdaptedDecoder ¶
type AdaptedDecoder struct {
// contains filtered or unexported fields
}
AdaptedDecoder reads and decodes JSON values from an input stream. AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
func NewDecoder ¶
func NewDecoder(reader io.Reader) *AdaptedDecoder
NewDecoder adapts to json/stream NewDecoder API.
NewDecoder returns a new decoder that reads from r.
Instead of a json/encoding Decoder, an AdaptedDecoder is returned Refer to https://godoc.org/encoding/json#NewDecoder for more information
func (*AdaptedDecoder) Buffered ¶
func (adapter *AdaptedDecoder) Buffered() io.Reader
func (*AdaptedDecoder) Decode ¶
func (adapter *AdaptedDecoder) Decode(obj interface{}) error
func (*AdaptedDecoder) More ¶
func (adapter *AdaptedDecoder) More() bool
func (*AdaptedDecoder) UseNumber ¶
func (decoder *AdaptedDecoder) UseNumber()
type AdaptedEncoder ¶
type AdaptedEncoder struct {
// contains filtered or unexported fields
}
func NewEncoder ¶
func NewEncoder(writer io.Writer) *AdaptedEncoder
func (*AdaptedEncoder) Encode ¶
func (adapter *AdaptedEncoder) Encode(val interface{}) error
func (*AdaptedEncoder) SetIndent ¶
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string)
type Any ¶
type Any interface { LastError() error ValueType() ValueType ToBool() bool ToInt() int ToInt32() int32 ToInt64() int64 ToUint() uint ToUint32() uint32 ToUint64() uint64 ToFloat32() float32 ToFloat64() float64 ToString() string Get(path ...interface{}) Any Size() int Keys() []string IterateObject() (func() (string, Any, bool), bool) IterateArray() (func() (Any, bool), bool) GetArray() []Any SetArray(newList []Any) bool GetObject() map[string]Any SetObject(map[string]Any) bool GetInterface() interface{} WriteTo(stream *Stream) Parse() *Iterator }
func UnmarshalAnyFromString ¶
func WrapFloat64 ¶
func WrapString ¶
func WrapUint32 ¶
func WrapUint64 ¶
type Config ¶
type Decoder ¶
type Decoder interface {
// contains filtered or unexported methods
}
Decoder is an internal type registered to cache as needed. Don't confuse jsoniter.Decoder with json.Decoder. For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
Reflection on type to create decoders, which is then cached Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions 1. create instance of new value, for example *int will need a int to be allocated 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New 3. assignment to map, both key and value will be reflect.Value For a simple struct binding, it will be reflect.Value free and allocation free
type DecoderFunc ¶
type Encoder ¶
type Encoder interface {
// contains filtered or unexported methods
}
Encoder is an internal type registered to cache as needed. Don't confuse jsoniter.Encoder with json.Encoder. For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
type EncoderFunc ¶
type ExtensionFunc ¶
type ExtensionFunc func(typ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc)
type Iterator ¶
type Iterator struct { Error error // contains filtered or unexported fields }
Iterator is a fast and flexible JSON parser
func NewIterator ¶
func NewIterator(cfg *frozenConfig) *Iterator
Create creates an empty Iterator instance
func ParseBytes ¶
ParseBytes parses a json byte slice into an Iterator instance
func ParseString ¶
ParseString parses a json string into an Iterator instance
func (*Iterator) CurrentBuffer ¶
CurrentBuffer gets current buffer as string
func (*Iterator) ReadArrayCB ¶
func (*Iterator) ReadBase64 ¶
ReadBase64 reads a json object as Base64 in byte slice
func (*Iterator) ReadBigFloat ¶
func (*Iterator) ReadBigInt ¶
func (*Iterator) ReadFloat32 ¶
func (*Iterator) ReadFloat64 ¶
func (*Iterator) ReadObject ¶
func (*Iterator) ReadObjectCB ¶
func (*Iterator) ReadString ¶
func (*Iterator) ReadStringAsSlice ¶
func (*Iterator) ReadUint16 ¶
func (*Iterator) ReadUint32 ¶
func (*Iterator) ReadUint64 ¶
func (*Iterator) ReadVal ¶
func (iter *Iterator) ReadVal(obj interface{})
Read converts an Iterator instance into go interface, same as json.Unmarshal
func (*Iterator) ResetBytes ¶
ResetBytes can reset an Iterator instance for another json byte slice
func (*Iterator) Skip ¶
func (iter *Iterator) Skip()
Skip skips a json object and positions to relatively the next json object
func (*Iterator) SkipAndReturnBytes ¶
func (*Iterator) WhatIsNext ¶
WhatIsNext gets ValueType of relatively next json object
type Stream ¶
type Stream struct { Error error // contains filtered or unexported fields }
func (*Stream) Buffered ¶
Buffered returns the number of bytes that have been written into the current buffer.
func (*Stream) Write ¶
Write writes the contents of p into the buffer. It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short.
func (*Stream) WriteArrayEnd ¶
func (stream *Stream) WriteArrayEnd()
func (*Stream) WriteArrayStart ¶
func (stream *Stream) WriteArrayStart()
func (*Stream) WriteEmptyArray ¶
func (stream *Stream) WriteEmptyArray()
func (*Stream) WriteEmptyObject ¶
func (stream *Stream) WriteEmptyObject()
func (*Stream) WriteFalse ¶
func (stream *Stream) WriteFalse()
func (*Stream) WriteFloat32 ¶
func (*Stream) WriteFloat32Lossy ¶
func (*Stream) WriteFloat64 ¶
func (*Stream) WriteFloat64Lossy ¶
func (*Stream) WriteInt16 ¶
func (*Stream) WriteInt32 ¶
func (*Stream) WriteInt64 ¶
func (*Stream) WriteObjectEnd ¶
func (stream *Stream) WriteObjectEnd()
func (*Stream) WriteObjectField ¶
func (*Stream) WriteObjectStart ¶
func (stream *Stream) WriteObjectStart()
func (*Stream) WriteString ¶
func (*Stream) WriteUint16 ¶
func (*Stream) WriteUint32 ¶
func (*Stream) WriteUint64 ¶
func (*Stream) WriteUint8 ¶
Source Files
¶
- feature_adapter.go
- feature_any.go
- feature_any_array.go
- feature_any_bool.go
- feature_any_float.go
- feature_any_int32.go
- feature_any_int64.go
- feature_any_invalid.go
- feature_any_nil.go
- feature_any_object.go
- feature_any_string.go
- feature_any_uint32.go
- feature_any_uint64.go
- feature_config.go
- feature_iter.go
- feature_iter_array.go
- feature_iter_float.go
- feature_iter_int.go
- feature_iter_object.go
- feature_iter_skip.go
- feature_iter_string.go
- feature_reflect.go
- feature_reflect_array.go
- feature_reflect_map.go
- feature_reflect_native.go
- feature_reflect_object.go
- feature_stream.go
- feature_stream_float.go
- feature_stream_int.go