go: github.com/json-iterator/go Index | Examples | Files | Directories

package jsoniter

import "github.com/json-iterator/go"

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

Examples

Package 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_number.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_pool.go feature_reflect.go feature_reflect_array.go feature_reflect_extension.go feature_reflect_map.go feature_reflect_native.go feature_reflect_object.go feature_reflect_slice.go feature_stream.go feature_stream_float.go feature_stream_int.go feature_stream_string.go

Variables

var ConfigCompatibleWithStandardLibrary = Config{
    EscapeHtml:  true,
    SortMapKeys: true,
}.Froze()

Trying to be 100% compatible with standard library behavior

var ConfigDefault = Config{
    EscapeHtml: true,
}.Froze()
var ConfigFastest = Config{
    EscapeHtml:              false,
    MarshalFloatWith6Digits: true,
}.Froze()

func Marshal Uses

func Marshal(v interface{}) ([]byte, error)

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

Code:

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 Uses

func MarshalToString(v interface{}) (string, error)

func RegisterExtension Uses

func RegisterExtension(extension Extension)

func RegisterFieldDecoder Uses

func RegisterFieldDecoder(typ string, field string, decoder ValDecoder)

func RegisterFieldDecoderFunc Uses

func RegisterFieldDecoderFunc(typ string, field string, fun DecoderFunc)

func RegisterFieldEncoder Uses

func RegisterFieldEncoder(typ string, field string, encoder ValEncoder)

func RegisterFieldEncoderFunc Uses

func RegisterFieldEncoderFunc(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool)

func RegisterTypeDecoder Uses

func RegisterTypeDecoder(typ string, decoder ValDecoder)

func RegisterTypeDecoderFunc Uses

func RegisterTypeDecoderFunc(typ string, fun DecoderFunc)

func RegisterTypeEncoder Uses

func RegisterTypeEncoder(typ string, encoder ValEncoder)

func RegisterTypeEncoderFunc Uses

func RegisterTypeEncoderFunc(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool)

func Unmarshal Uses

func Unmarshal(data []byte, v interface{}) error

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

Code:

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 Uses

func UnmarshalFromString(str string, v interface{}) error

func WriteToStream Uses

func WriteToStream(val interface{}, stream *Stream, encoder ValEncoder)

type Any Uses

type Any interface {
    LastError() error
    ValueType() ValueType
    MustBeValid() Any
    ToBool() bool
    ToInt() int
    ToInt32() int32
    ToInt64() int64
    ToUint() uint
    ToUint32() uint32
    ToUint64() uint64
    ToFloat32() float32
    ToFloat64() float64
    ToString() string
    ToVal(val interface{})
    Get(path ...interface{}) Any
    // TODO: add Set
    Size() int
    Keys() []string
    // TODO: remove me
    GetArray() []Any
    // TODO: remove me
    GetObject() map[string]Any
    GetInterface() interface{}
    WriteTo(stream *Stream)
}

func Get Uses

func Get(data []byte, path ...interface{}) Any

func Wrap Uses

func Wrap(val interface{}) Any

func WrapFloat64 Uses

func WrapFloat64(val float64) Any

func WrapInt32 Uses

func WrapInt32(val int32) Any

func WrapInt64 Uses

func WrapInt64(val int64) Any

func WrapString Uses

func WrapString(val string) Any

func WrapUint32 Uses

func WrapUint32(val uint32) Any

func WrapUint64 Uses

func WrapUint64(val uint64) Any

type Api Uses

type Api interface {
    MarshalToString(v interface{}) (string, error)
    Marshal(v interface{}) ([]byte, error)
    UnmarshalFromString(str string, v interface{}) error
    Unmarshal(data []byte, v interface{}) error
    Get(data []byte, path ...interface{}) Any
    NewEncoder(writer io.Writer) *Encoder
    NewDecoder(reader io.Reader) *Decoder
}

type Binding Uses

type Binding struct {
    Field     *reflect.StructField
    FromNames []string
    ToNames   []string
    Encoder   ValEncoder
    Decoder   ValDecoder
}

type Config Uses

type Config struct {
    IndentionStep           int
    MarshalFloatWith6Digits bool
    EscapeHtml              bool
    SortMapKeys             bool
    UseNumber               bool
}

func (Config) Froze Uses

func (cfg Config) Froze() *frozenConfig

type Decoder Uses

type Decoder struct {
    // contains filtered or unexported fields
}

Decoder reads and decodes JSON values from an input stream. Decoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)

func NewDecoder Uses

func NewDecoder(reader io.Reader) *Decoder

NewDecoder adapts to json/stream NewDecoder API.

NewDecoder returns a new decoder that reads from r.

Instead of a json/encoding Decoder, an Decoder is returned Refer to https://godoc.org/encoding/json#NewDecoder for more information

func (*Decoder) Buffered Uses

func (adapter *Decoder) Buffered() io.Reader

func (*Decoder) Decode Uses

func (adapter *Decoder) Decode(obj interface{}) error

func (*Decoder) More Uses

func (adapter *Decoder) More() bool

func (*Decoder) UseNumber Uses

func (decoder *Decoder) UseNumber()

type DecoderFunc Uses

type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)

type DummyExtension Uses

type DummyExtension struct {
}

func (*DummyExtension) CreateDecoder Uses

func (extension *DummyExtension) CreateDecoder(typ reflect.Type) ValDecoder

func (*DummyExtension) CreateEncoder Uses

func (extension *DummyExtension) CreateEncoder(typ reflect.Type) ValEncoder

func (*DummyExtension) DecorateDecoder Uses

func (extension *DummyExtension) DecorateDecoder(typ reflect.Type, decoder ValDecoder) ValDecoder

func (*DummyExtension) DecorateEncoder Uses

func (extension *DummyExtension) DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder

func (*DummyExtension) UpdateStructDescriptor Uses

func (extension *DummyExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor)

type Encoder Uses

type Encoder struct {
    // contains filtered or unexported fields
}

func NewEncoder Uses

func NewEncoder(writer io.Writer) *Encoder

func (*Encoder) Encode Uses

func (adapter *Encoder) Encode(val interface{}) error

func (*Encoder) SetEscapeHTML Uses

func (adapter *Encoder) SetEscapeHTML(escapeHtml bool)

func (*Encoder) SetIndent Uses

func (adapter *Encoder) SetIndent(prefix, indent string)

type EncoderFunc Uses

type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)

type Extension Uses

type Extension interface {
    UpdateStructDescriptor(structDescriptor *StructDescriptor)
    CreateDecoder(typ reflect.Type) ValDecoder
    CreateEncoder(typ reflect.Type) ValEncoder
    DecorateDecoder(typ reflect.Type, decoder ValDecoder) ValDecoder
    DecorateEncoder(typ reflect.Type, encoder ValEncoder) ValEncoder
}

type Iterator Uses

type Iterator struct {
    Error error
    // contains filtered or unexported fields
}

Iterator is a fast and flexible JSON parser

func NewIterator Uses

func NewIterator(cfg *frozenConfig) *Iterator

Create creates an empty Iterator instance

func Parse Uses

func Parse(cfg *frozenConfig, reader io.Reader, bufSize int) *Iterator

Parse parses a json buffer in io.Reader into an Iterator instance

func ParseBytes Uses

func ParseBytes(cfg *frozenConfig, input []byte) *Iterator

ParseBytes parses a json byte slice into an Iterator instance

func ParseString Uses

func ParseString(cfg *frozenConfig, input string) *Iterator

ParseString parses a json string into an Iterator instance

func (*Iterator) Config Uses

func (iter *Iterator) Config() *frozenConfig

func (*Iterator) CurrentBuffer Uses

func (iter *Iterator) CurrentBuffer() string

CurrentBuffer gets current buffer as string

func (*Iterator) Read Uses

func (iter *Iterator) Read() interface{}

func (*Iterator) ReadAny Uses

func (iter *Iterator) ReadAny() Any

func (*Iterator) ReadArray Uses

func (iter *Iterator) ReadArray() (ret bool)

func (*Iterator) ReadArrayCB Uses

func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool)

func (*Iterator) ReadBigFloat Uses

func (iter *Iterator) ReadBigFloat() (ret *big.Float)

func (*Iterator) ReadBigInt Uses

func (iter *Iterator) ReadBigInt() (ret *big.Int)

func (*Iterator) ReadBool Uses

func (iter *Iterator) ReadBool() (ret bool)

ReadBool reads a json object as Bool

func (*Iterator) ReadFloat32 Uses

func (iter *Iterator) ReadFloat32() (ret float32)

func (*Iterator) ReadFloat64 Uses

func (iter *Iterator) ReadFloat64() (ret float64)

func (*Iterator) ReadInt Uses

func (iter *Iterator) ReadInt() int

func (*Iterator) ReadInt16 Uses

func (iter *Iterator) ReadInt16() (ret int16)

func (*Iterator) ReadInt32 Uses

func (iter *Iterator) ReadInt32() (ret int32)

func (*Iterator) ReadInt64 Uses

func (iter *Iterator) ReadInt64() (ret int64)

func (*Iterator) ReadInt8 Uses

func (iter *Iterator) ReadInt8() (ret int8)

func (*Iterator) ReadMapCB Uses

func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool

func (*Iterator) ReadNil Uses

func (iter *Iterator) ReadNil() (ret bool)

ReadNil reads a json object as nil and returns whether it's a nil or not

func (*Iterator) ReadObject Uses

func (iter *Iterator) ReadObject() (ret string)

func (*Iterator) ReadObjectCB Uses

func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool

func (*Iterator) ReadString Uses

func (iter *Iterator) ReadString() (ret string)

func (*Iterator) ReadStringAsSlice Uses

func (iter *Iterator) ReadStringAsSlice() (ret []byte)

func (*Iterator) ReadUint Uses

func (iter *Iterator) ReadUint() uint

func (*Iterator) ReadUint16 Uses

func (iter *Iterator) ReadUint16() (ret uint16)

func (*Iterator) ReadUint32 Uses

func (iter *Iterator) ReadUint32() (ret uint32)

func (*Iterator) ReadUint64 Uses

func (iter *Iterator) ReadUint64() uint64

func (*Iterator) ReadUint8 Uses

func (iter *Iterator) ReadUint8() (ret uint8)

func (*Iterator) ReadVal Uses

func (iter *Iterator) ReadVal(obj interface{})

Read converts an Iterator instance into go interface, same as json.Unmarshal

func (*Iterator) ReportError Uses

func (iter *Iterator) ReportError(operation string, msg string)

func (*Iterator) Reset Uses

func (iter *Iterator) Reset(reader io.Reader) *Iterator

Reset can reset an Iterator instance for another json buffer in io.Reader

func (*Iterator) ResetBytes Uses

func (iter *Iterator) ResetBytes(input []byte) *Iterator

ResetBytes can reset an Iterator instance for another json byte slice

func (*Iterator) Skip Uses

func (iter *Iterator) Skip()

Skip skips a json object and positions to relatively the next json object

func (*Iterator) SkipAndReturnBytes Uses

func (iter *Iterator) SkipAndReturnBytes() []byte

func (*Iterator) WhatIsNext Uses

func (iter *Iterator) WhatIsNext() ValueType

WhatIsNext gets ValueType of relatively next json object

type RawMessage Uses

type RawMessage []byte

type Stream Uses

type Stream struct {
    Error error
    // contains filtered or unexported fields
}

func NewStream Uses

func NewStream(cfg *frozenConfig, out io.Writer, bufSize int) *Stream

func (*Stream) Available Uses

func (b *Stream) Available() int

Available returns how many bytes are unused in the buffer.

func (*Stream) Buffer Uses

func (b *Stream) Buffer() []byte

func (*Stream) Buffered Uses

func (b *Stream) Buffered() int

Buffered returns the number of bytes that have been written into the current buffer.

func (*Stream) Flush Uses

func (b *Stream) Flush() error

Flush writes any buffered data to the underlying io.Writer.

func (*Stream) Reset Uses

func (b *Stream) Reset(out io.Writer)

func (*Stream) Write Uses

func (b *Stream) Write(p []byte) (nn int, err error)

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 Uses

func (stream *Stream) WriteArrayEnd()

func (*Stream) WriteArrayStart Uses

func (stream *Stream) WriteArrayStart()

func (*Stream) WriteBool Uses

func (stream *Stream) WriteBool(val bool)

func (*Stream) WriteEmptyArray Uses

func (stream *Stream) WriteEmptyArray()

func (*Stream) WriteEmptyObject Uses

func (stream *Stream) WriteEmptyObject()

func (*Stream) WriteFalse Uses

func (stream *Stream) WriteFalse()

func (*Stream) WriteFloat32 Uses

func (stream *Stream) WriteFloat32(val float32)

func (*Stream) WriteFloat32Lossy Uses

func (stream *Stream) WriteFloat32Lossy(val float32)

func (*Stream) WriteFloat64 Uses

func (stream *Stream) WriteFloat64(val float64)

func (*Stream) WriteFloat64Lossy Uses

func (stream *Stream) WriteFloat64Lossy(val float64)

func (*Stream) WriteInt Uses

func (stream *Stream) WriteInt(val int)

func (*Stream) WriteInt16 Uses

func (stream *Stream) WriteInt16(nval int16)

func (*Stream) WriteInt32 Uses

func (stream *Stream) WriteInt32(nval int32)

func (*Stream) WriteInt64 Uses

func (stream *Stream) WriteInt64(nval int64)

func (*Stream) WriteInt8 Uses

func (stream *Stream) WriteInt8(nval int8)

func (*Stream) WriteMore Uses

func (stream *Stream) WriteMore()

func (*Stream) WriteNil Uses

func (stream *Stream) WriteNil()

func (*Stream) WriteObjectEnd Uses

func (stream *Stream) WriteObjectEnd()

func (*Stream) WriteObjectField Uses

func (stream *Stream) WriteObjectField(field string)

func (*Stream) WriteObjectStart Uses

func (stream *Stream) WriteObjectStart()

func (*Stream) WriteRaw Uses

func (b *Stream) WriteRaw(s string)

func (*Stream) WriteString Uses

func (stream *Stream) WriteString(s string)

func (*Stream) WriteStringWithHtmlEscaped Uses

func (stream *Stream) WriteStringWithHtmlEscaped(s string)

func (*Stream) WriteTrue Uses

func (stream *Stream) WriteTrue()

func (*Stream) WriteUint Uses

func (stream *Stream) WriteUint(val uint)

func (*Stream) WriteUint16 Uses

func (stream *Stream) WriteUint16(val uint16)

func (*Stream) WriteUint32 Uses

func (stream *Stream) WriteUint32(val uint32)

func (*Stream) WriteUint64 Uses

func (stream *Stream) WriteUint64(val uint64)

func (*Stream) WriteUint8 Uses

func (stream *Stream) WriteUint8(val uint8)

func (*Stream) WriteVal Uses

func (stream *Stream) WriteVal(val interface{})

type StructDescriptor Uses

type StructDescriptor struct {
    Type   reflect.Type
    Fields []*Binding
}

func (*StructDescriptor) GetField Uses

func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding

type ValDecoder Uses

type ValDecoder interface {
    Decode(ptr unsafe.Pointer, iter *Iterator)
}

ValDecoder is an internal type registered to cache as needed. Don't confuse jsoniter.ValDecoder 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 ValEncoder Uses

type ValEncoder interface {
    IsEmpty(ptr unsafe.Pointer) bool
    Encode(ptr unsafe.Pointer, stream *Stream)
    EncodeInterface(val interface{}, stream *Stream)
}

ValEncoder is an internal type registered to cache as needed. Don't confuse jsoniter.ValEncoder with json.Encoder. For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).

type ValueType Uses

type ValueType int
const (
    Invalid ValueType = iota
    String
    Number
    Nil
    Bool
    Array
    Object
)

Directories

PathSynopsis
assert
extra
require

Package jsoniter imports 19 packages (graph) and is imported by 2 packages. Updated 2017-06-23. Refresh now. Tools for package owners.