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) ReadInt8() (ret int8)
- func (iter *Iterator) ReadInt16() (ret int16)
- func (iter *Iterator) ReadInt32() (ret int32)
- func (iter *Iterator) ReadInt64() (ret int64)
- 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) ReadUint8() (ret uint8)
- func (iter *Iterator) ReadUint16() (ret uint16)
- func (iter *Iterator) ReadUint32() (ret uint32)
- func (iter *Iterator) ReadUint64() uint64
- 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) WriteInt8(nval int8)
- func (stream *Stream) WriteInt16(nval int16)
- func (stream *Stream) WriteInt32(nval int32)
- func (stream *Stream) WriteInt64(nval int64)
- 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) WriteUint8(val uint8)
- func (stream *Stream) WriteUint16(val uint16)
- func (stream *Stream) WriteUint32(val uint32)
- func (stream *Stream) WriteUint64(val uint64)
- 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) ReadNil ΒΆ
ReadNil reads a json object as nil and returns whether it's a nil or not
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) WriteUint8 ΒΆ
func (*Stream) WriteUint16 ΒΆ
func (*Stream) WriteUint32 ΒΆ
func (*Stream) WriteUint64 ΒΆ
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

