...

Package jsoniter

import "github.com/json-iterator/go"
Overview
Index
Examples
Subdirectories

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 CastJsonNumber(val interface{}) (string, bool)
func Marshal(v interface{}) ([]byte, error)
func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
func MarshalToString(v interface{}) (string, error)
func RegisterExtension(extension Extension)
func RegisterFieldDecoder(typ string, field string, decoder ValDecoder)
func RegisterFieldDecoderFunc(typ string, field string, fun DecoderFunc)
func RegisterFieldEncoder(typ string, field string, encoder ValEncoder)
func RegisterFieldEncoderFunc(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool)
func RegisterTypeDecoder(typ string, decoder ValDecoder)
func RegisterTypeDecoderFunc(typ string, fun DecoderFunc)
func RegisterTypeEncoder(typ string, encoder ValEncoder)
func RegisterTypeEncoderFunc(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool)
func Unmarshal(data []byte, v interface{}) error
func UnmarshalFromString(str string, v interface{}) error
func Valid(data []byte) bool
type API
type Any
    func Get(data []byte, path ...interface{}) Any
    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 Binding
type Config
    func (cfg Config) Froze() API
type Decoder
    func NewDecoder(reader io.Reader) *Decoder
    func (adapter *Decoder) Buffered() io.Reader
    func (adapter *Decoder) Decode(obj interface{}) error
    func (adapter *Decoder) DisallowUnknownFields()
    func (adapter *Decoder) More() bool
    func (adapter *Decoder) UseNumber()
type DecoderExtension
    func (extension DecoderExtension) CreateDecoder(typ reflect2.Type) ValDecoder
    func (extension DecoderExtension) CreateEncoder(typ reflect2.Type) ValEncoder
    func (extension DecoderExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder
    func (extension DecoderExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder
    func (extension DecoderExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder
    func (extension DecoderExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder
    func (extension DecoderExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor)
type DecoderFunc
type DummyExtension
    func (extension *DummyExtension) CreateDecoder(typ reflect2.Type) ValDecoder
    func (extension *DummyExtension) CreateEncoder(typ reflect2.Type) ValEncoder
    func (extension *DummyExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder
    func (extension *DummyExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder
    func (extension *DummyExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder
    func (extension *DummyExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder
    func (extension *DummyExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor)
type Encoder
    func NewEncoder(writer io.Writer) *Encoder
    func (adapter *Encoder) Encode(val interface{}) error
    func (adapter *Encoder) SetEscapeHTML(escapeHTML bool)
    func (adapter *Encoder) SetIndent(prefix, indent string)
type EncoderExtension
    func (extension EncoderExtension) CreateDecoder(typ reflect2.Type) ValDecoder
    func (extension EncoderExtension) CreateEncoder(typ reflect2.Type) ValEncoder
    func (extension EncoderExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder
    func (extension EncoderExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder
    func (extension EncoderExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder
    func (extension EncoderExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder
    func (extension EncoderExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor)
type EncoderFunc
type Extension
type IsEmbeddedPtrNil
type Iterator
    func NewIterator(cfg API) *Iterator
    func Parse(cfg API, reader io.Reader, bufSize int) *Iterator
    func ParseBytes(cfg API, input []byte) *Iterator
    func ParseString(cfg API, input string) *Iterator
    func (iter *Iterator) CurrentBuffer() string
    func (iter *Iterator) Pool() IteratorPool
    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) 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) ReadNumber() (ret json.Number)
    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) ReportError(operation string, msg string)
    func (iter *Iterator) Reset(reader io.Reader) *Iterator
    func (iter *Iterator) ResetBytes(input []byte) *Iterator
    func (iter *Iterator) Skip()
    func (iter *Iterator) SkipAndAppendBytes(buf []byte) []byte
    func (iter *Iterator) SkipAndReturnBytes() []byte
    func (iter *Iterator) WhatIsNext() ValueType
type IteratorPool
type Number
    func (n Number) Float64() (float64, error)
    func (n Number) Int64() (int64, error)
    func (n Number) String() string
type OptionalDecoder
    func (decoder *OptionalDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
type OptionalEncoder
    func (encoder *OptionalEncoder) Encode(ptr unsafe.Pointer, stream *Stream)
    func (encoder *OptionalEncoder) IsEmpty(ptr unsafe.Pointer) bool
type RawMessage
type Stream
    func NewStream(cfg API, out io.Writer, bufSize int) *Stream
    func (stream *Stream) Available() int
    func (stream *Stream) Buffer() []byte
    func (stream *Stream) Buffered() int
    func (stream *Stream) Flush() error
    func (stream *Stream) Pool() StreamPool
    func (stream *Stream) Reset(out io.Writer)
    func (stream *Stream) SetBuffer(buf []byte)
    func (stream *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 (stream *Stream) WriteRaw(s string)
    func (stream *Stream) WriteString(s string)
    func (stream *Stream) WriteStringWithHTMLEscaped(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 StreamPool
type StructDescriptor
    func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding
type ValDecoder
type ValEncoder
type ValueType

Examples

Get
Marshal
Unmarshal

Package files

adapter.go any.go any_array.go any_bool.go any_float.go any_int32.go any_int64.go any_invalid.go any_nil.go any_number.go any_object.go any_str.go any_uint32.go any_uint64.go config.go iter.go iter_array.go iter_float.go iter_int.go iter_object.go iter_skip.go iter_skip_strict.go iter_str.go jsoniter.go pool.go reflect.go reflect_array.go reflect_dynamic.go reflect_extension.go reflect_json_number.go reflect_json_raw_message.go reflect_map.go reflect_marshaler.go reflect_native.go reflect_optional.go reflect_slice.go reflect_struct_decoder.go reflect_struct_encoder.go stream.go stream_float.go stream_int.go stream_str.go

Variables

ConfigCompatibleWithStandardLibrary tries to be 100% compatible with standard library behavior

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

ConfigDefault the default API

var ConfigDefault = Config{
    EscapeHTML: true,
}.Froze()

ConfigFastest marshals float with only 6 digits precision

var ConfigFastest = Config{
    EscapeHTML:                    false,
    MarshalFloatWith6Digits:       true,
    ObjectFieldMustBeSimpleString: true,
}.Froze()

func CastJsonNumber

func CastJsonNumber(val interface{}) (string, bool)

func Marshal

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

Example

Code:

type ColorGroup struct {
    ID     int
    Name   string
    Colors []string
}
group := ColorGroup{
    ID:     1,
    Name:   "Reds",
    Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := Marshal(group)
if err != nil {
    fmt.Println("error:", err)
}
os.Stdout.Write(b)

Output:

{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}

func MarshalIndent

func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)

MarshalIndent same as json.MarshalIndent. Prefix is not supported.

func MarshalToString

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

MarshalToString convenient method to write as string instead of []byte

func RegisterExtension

func RegisterExtension(extension Extension)

RegisterExtension register extension

func RegisterFieldDecoder

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

RegisterFieldDecoder register TypeDecoder for a struct field

func RegisterFieldDecoderFunc

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

RegisterFieldDecoderFunc register TypeDecoder for a struct field with function

func RegisterFieldEncoder

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

RegisterFieldEncoder register TypeEncoder for a struct field

func RegisterFieldEncoderFunc

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

RegisterFieldEncoderFunc register TypeEncoder for a struct field with encode/isEmpty function

func RegisterTypeDecoder

func RegisterTypeDecoder(typ string, decoder ValDecoder)

RegisterTypeDecoder register TypeDecoder for a typ

func RegisterTypeDecoderFunc

func RegisterTypeDecoderFunc(typ string, fun DecoderFunc)

RegisterTypeDecoderFunc register TypeDecoder for a type with function

func RegisterTypeEncoder

func RegisterTypeEncoder(typ string, encoder ValEncoder)

RegisterTypeEncoder register TypeEncoder for a type

func RegisterTypeEncoderFunc

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

RegisterTypeEncoderFunc register TypeEncoder for a type with encode/isEmpty function

func Unmarshal

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

Example

Code:

var jsonBlob = []byte(`[
        {"Name": "Platypus", "Order": "Monotremata"},
        {"Name": "Quoll",    "Order": "Dasyuromorphia"}
    ]`)
type Animal struct {
    Name  string
    Order string
}
var animals []Animal
err := 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

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

UnmarshalFromString is a convenient method to read from string instead of []byte

func Valid

func Valid(data []byte) bool

Valid reports whether data is a valid JSON encoding.

type API

API the public interface of this package. Primary Marshal and Unmarshal.

type API interface {
    IteratorPool
    StreamPool
    MarshalToString(v interface{}) (string, error)
    Marshal(v interface{}) ([]byte, error)
    MarshalIndent(v interface{}, prefix, indent string) ([]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
    Valid(data []byte) bool
    RegisterExtension(extension Extension)
    DecoderOf(typ reflect2.Type) ValDecoder
    EncoderOf(typ reflect2.Type) ValEncoder
}

type Any

Any generic object representation. The lazy json implementation holds []byte and parse lazily.

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
    Size() int
    Keys() []string
    GetInterface() interface{}
    WriteTo(stream *Stream)
}

func Get

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

Get quick method to get value from deeply nested JSON structure

Example

Code:

val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`)
fmt.Printf(Get(val, "Colors", 0).ToString())

Output:

Crimson

func Wrap

func Wrap(val interface{}) Any

Wrap turn a go object into Any interface

func WrapFloat64

func WrapFloat64(val float64) Any

WrapFloat64 turn float64 into Any interface

func WrapInt32

func WrapInt32(val int32) Any

WrapInt32 turn int32 into Any interface

func WrapInt64

func WrapInt64(val int64) Any

WrapInt64 turn int64 into Any interface

func WrapString

func WrapString(val string) Any

WrapString turn string into Any interface

func WrapUint32

func WrapUint32(val uint32) Any

WrapUint32 turn uint32 into Any interface

func WrapUint64

func WrapUint64(val uint64) Any

WrapUint64 turn uint64 into Any interface

type Binding

Binding describe how should we encode/decode the struct field

type Binding struct {
    Field     reflect2.StructField
    FromNames []string
    ToNames   []string
    Encoder   ValEncoder
    Decoder   ValDecoder
    // contains filtered or unexported fields
}

type Config

Config customize how the API should behave. The API is created from Config by Froze.

type Config struct {
    IndentionStep                 int
    MarshalFloatWith6Digits       bool
    EscapeHTML                    bool
    SortMapKeys                   bool
    UseNumber                     bool
    DisallowUnknownFields         bool
    TagKey                        string
    OnlyTaggedField               bool
    ValidateJsonRawMessage        bool
    ObjectFieldMustBeSimpleString bool
    CaseSensitive                 bool
}

func (Config) Froze

func (cfg Config) Froze() API

Froze forge API from config

type Decoder

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

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

func NewDecoder

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

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

Buffered remaining buffer

func (*Decoder) Decode

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

Decode decode JSON into interface{}

func (*Decoder) DisallowUnknownFields

func (adapter *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) More

func (adapter *Decoder) More() bool

More is there more?

func (*Decoder) UseNumber

func (adapter *Decoder) UseNumber()

UseNumber causes the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64.

type DecoderExtension

type DecoderExtension map[reflect2.Type]ValDecoder

func (DecoderExtension) CreateDecoder

func (extension DecoderExtension) CreateDecoder(typ reflect2.Type) ValDecoder

CreateDecoder get decoder from map

func (DecoderExtension) CreateEncoder

func (extension DecoderExtension) CreateEncoder(typ reflect2.Type) ValEncoder

CreateEncoder No-op

func (DecoderExtension) CreateMapKeyDecoder

func (extension DecoderExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder

CreateMapKeyDecoder No-op

func (DecoderExtension) CreateMapKeyEncoder

func (extension DecoderExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder

CreateMapKeyEncoder No-op

func (DecoderExtension) DecorateDecoder

func (extension DecoderExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder

DecorateDecoder No-op

func (DecoderExtension) DecorateEncoder

func (extension DecoderExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder

DecorateEncoder No-op

func (DecoderExtension) UpdateStructDescriptor

func (extension DecoderExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor)

UpdateStructDescriptor No-op

type DecoderFunc

DecoderFunc the function form of TypeDecoder

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

type DummyExtension

DummyExtension embed this type get dummy implementation for all methods of Extension

type DummyExtension struct {
}

func (*DummyExtension) CreateDecoder

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

CreateDecoder No-op

func (*DummyExtension) CreateEncoder

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

CreateEncoder No-op

func (*DummyExtension) CreateMapKeyDecoder

func (extension *DummyExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder

CreateMapKeyDecoder No-op

func (*DummyExtension) CreateMapKeyEncoder

func (extension *DummyExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder

CreateMapKeyEncoder No-op

func (*DummyExtension) DecorateDecoder

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

DecorateDecoder No-op

func (*DummyExtension) DecorateEncoder

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

DecorateEncoder No-op

func (*DummyExtension) UpdateStructDescriptor

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

UpdateStructDescriptor No-op

type Encoder

Encoder same as json.Encoder

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

func NewEncoder

func NewEncoder(writer io.Writer) *Encoder

NewEncoder same as json.NewEncoder

func (*Encoder) Encode

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

Encode encode interface{} as JSON to io.Writer

func (*Encoder) SetEscapeHTML

func (adapter *Encoder) SetEscapeHTML(escapeHTML bool)

SetEscapeHTML escape html by default, set to false to disable

func (*Encoder) SetIndent

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

SetIndent set the indention. Prefix is not supported

type EncoderExtension

type EncoderExtension map[reflect2.Type]ValEncoder

func (EncoderExtension) CreateDecoder

func (extension EncoderExtension) CreateDecoder(typ reflect2.Type) ValDecoder

CreateDecoder No-op

func (EncoderExtension) CreateEncoder

func (extension EncoderExtension) CreateEncoder(typ reflect2.Type) ValEncoder

CreateEncoder get encoder from map

func (EncoderExtension) CreateMapKeyDecoder

func (extension EncoderExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder

CreateMapKeyDecoder No-op

func (EncoderExtension) CreateMapKeyEncoder

func (extension EncoderExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder

CreateMapKeyEncoder No-op

func (EncoderExtension) DecorateDecoder

func (extension EncoderExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder

DecorateDecoder No-op

func (EncoderExtension) DecorateEncoder

func (extension EncoderExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder

DecorateEncoder No-op

func (EncoderExtension) UpdateStructDescriptor

func (extension EncoderExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor)

UpdateStructDescriptor No-op

type EncoderFunc

EncoderFunc the function form of TypeEncoder

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

type Extension

Extension the one for all SPI. Customize encoding/decoding by specifying alternate encoder/decoder. Can also rename fields by UpdateStructDescriptor.

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

type IsEmbeddedPtrNil

type IsEmbeddedPtrNil interface {
    IsEmbeddedPtrNil(ptr unsafe.Pointer) bool
}

type Iterator

Iterator is a io.Reader like object, with JSON specific read functions. Error is not returned as return value, but stored as Error member on this iterator instance.

type Iterator struct {
    Error      error
    Attachment interface{} // open for customized decoder
    // contains filtered or unexported fields
}

func NewIterator

func NewIterator(cfg API) *Iterator

NewIterator creates an empty Iterator instance

func Parse

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

Parse creates an Iterator instance from io.Reader

func ParseBytes

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

ParseBytes creates an Iterator instance from byte array

func ParseString

func ParseString(cfg API, input string) *Iterator

ParseString creates an Iterator instance from string

func (*Iterator) CurrentBuffer

func (iter *Iterator) CurrentBuffer() string

CurrentBuffer gets current buffer as string for debugging purpose

func (*Iterator) Pool

func (iter *Iterator) Pool() IteratorPool

Pool returns a pool can provide more iterator with same configuration

func (*Iterator) Read

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

Read read the next JSON element as generic interface{}.

func (*Iterator) ReadAny

func (iter *Iterator) ReadAny() Any

ReadAny read next JSON element as an Any object. It is a better json.RawMessage.

func (*Iterator) ReadArray

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

ReadArray read array element, tells if the array has more element to read.

func (*Iterator) ReadArrayCB

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

ReadArrayCB read array with callback

func (*Iterator) ReadBigFloat

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

ReadBigFloat read big.Float

func (*Iterator) ReadBigInt

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

ReadBigInt read big.Int

func (*Iterator) ReadBool

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

ReadBool reads a json object as BoolValue

func (*Iterator) ReadFloat32

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

ReadFloat32 read float32

func (*Iterator) ReadFloat64

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

ReadFloat64 read float64

func (*Iterator) ReadInt

func (iter *Iterator) ReadInt() int

ReadInt read int

func (*Iterator) ReadInt16

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

ReadInt16 read int16

func (*Iterator) ReadInt32

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

ReadInt32 read int32

func (*Iterator) ReadInt64

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

ReadInt64 read int64

func (*Iterator) ReadInt8

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

ReadInt8 read int8

func (*Iterator) ReadMapCB

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

ReadMapCB read map with callback, the key can be any string

func (*Iterator) ReadNil

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

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

func (*Iterator) ReadNumber

func (iter *Iterator) ReadNumber() (ret json.Number)

ReadNumber read json.Number

func (*Iterator) ReadObject

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

ReadObject read one field from object. If object ended, returns empty string. Otherwise, returns the field name.

func (*Iterator) ReadObjectCB

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

ReadObjectCB read object with callback, the key is ascii only and field name not copied

func (*Iterator) ReadString

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

ReadString read string from iterator

func (*Iterator) ReadStringAsSlice

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

ReadStringAsSlice read string from iterator without copying into string form. The []byte can not be kept, as it will change after next iterator call.

func (*Iterator) ReadUint

func (iter *Iterator) ReadUint() uint

ReadUint read uint

func (*Iterator) ReadUint16

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

ReadUint16 read uint16

func (*Iterator) ReadUint32

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

ReadUint32 read uint32

func (*Iterator) ReadUint64

func (iter *Iterator) ReadUint64() uint64

ReadUint64 read uint64

func (*Iterator) ReadUint8

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

ReadUint8 read uint8

func (*Iterator) ReadVal

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

ReadVal copy the underlying JSON into go interface, same as json.Unmarshal

func (*Iterator) ReportError

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

ReportError record a error in iterator instance with current position.

func (*Iterator) Reset

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

Reset reuse iterator instance by specifying another reader

func (*Iterator) ResetBytes

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

ResetBytes reuse iterator instance by specifying another byte array as input

func (*Iterator) Skip

func (iter *Iterator) Skip()

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

func (*Iterator) SkipAndAppendBytes

func (iter *Iterator) SkipAndAppendBytes(buf []byte) []byte

SkipAndAppendBytes skips next JSON element and appends its content to buffer, returning the result.

func (*Iterator) SkipAndReturnBytes

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

SkipAndReturnBytes skip next JSON element, and return its content as []byte. The []byte can be kept, it is a copy of data.

func (*Iterator) WhatIsNext

func (iter *Iterator) WhatIsNext() ValueType

WhatIsNext gets ValueType of relatively next json element

type IteratorPool

IteratorPool a thread safe pool of iterators with same configuration

type IteratorPool interface {
    BorrowIterator(data []byte) *Iterator
    ReturnIterator(iter *Iterator)
}

type Number

type Number string

func (Number) Float64

func (n Number) Float64() (float64, error)

Float64 returns the number as a float64.

func (Number) Int64

func (n Number) Int64() (int64, error)

Int64 returns the number as an int64.

func (Number) String

func (n Number) String() string

String returns the literal text of the number.

type OptionalDecoder

type OptionalDecoder struct {
    ValueType    reflect2.Type
    ValueDecoder ValDecoder
}

func (*OptionalDecoder) Decode

func (decoder *OptionalDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)

type OptionalEncoder

type OptionalEncoder struct {
    ValueEncoder ValEncoder
}

func (*OptionalEncoder) Encode

func (encoder *OptionalEncoder) Encode(ptr unsafe.Pointer, stream *Stream)

func (*OptionalEncoder) IsEmpty

func (encoder *OptionalEncoder) IsEmpty(ptr unsafe.Pointer) bool

type RawMessage

RawMessage to make replace json with jsoniter

type RawMessage []byte

type Stream

stream is a io.Writer like object, with JSON specific write functions. Error is not returned as return value, but stored as Error member on this stream instance.

type Stream struct {
    Error error

    Attachment interface{} // open for customized encoder
    // contains filtered or unexported fields
}

func NewStream

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

NewStream create new stream instance. cfg can be jsoniter.ConfigDefault. out can be nil if write to internal buffer. bufSize is the initial size for the internal buffer in bytes.

func (*Stream) Available

func (stream *Stream) Available() int

Available returns how many bytes are unused in the buffer.

func (*Stream) Buffer

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

Buffer if writer is nil, use this method to take the result

func (*Stream) Buffered

func (stream *Stream) Buffered() int

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

func (*Stream) Flush

func (stream *Stream) Flush() error

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

func (*Stream) Pool

func (stream *Stream) Pool() StreamPool

Pool returns a pool can provide more stream with same configuration

func (*Stream) Reset

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

Reset reuse this stream instance by assign a new writer

func (*Stream) SetBuffer

func (stream *Stream) SetBuffer(buf []byte)

SetBuffer allows to append to the internal buffer directly

func (*Stream) Write

func (stream *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

func (stream *Stream) WriteArrayEnd()

WriteArrayEnd write ] with possible indention

func (*Stream) WriteArrayStart

func (stream *Stream) WriteArrayStart()

WriteArrayStart write [ with possible indention

func (*Stream) WriteBool

func (stream *Stream) WriteBool(val bool)

WriteBool write true or false into stream

func (*Stream) WriteEmptyArray

func (stream *Stream) WriteEmptyArray()

WriteEmptyArray write []

func (*Stream) WriteEmptyObject

func (stream *Stream) WriteEmptyObject()

WriteEmptyObject write {}

func (*Stream) WriteFalse

func (stream *Stream) WriteFalse()

WriteFalse write false to stream

func (*Stream) WriteFloat32

func (stream *Stream) WriteFloat32(val float32)

WriteFloat32 write float32 to stream

func (*Stream) WriteFloat32Lossy

func (stream *Stream) WriteFloat32Lossy(val float32)

WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster

func (*Stream) WriteFloat64

func (stream *Stream) WriteFloat64(val float64)

WriteFloat64 write float64 to stream

func (*Stream) WriteFloat64Lossy

func (stream *Stream) WriteFloat64Lossy(val float64)

WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster

func (*Stream) WriteInt

func (stream *Stream) WriteInt(val int)

WriteInt write int to stream

func (*Stream) WriteInt16

func (stream *Stream) WriteInt16(nval int16)

WriteInt16 write int16 to stream

func (*Stream) WriteInt32

func (stream *Stream) WriteInt32(nval int32)

WriteInt32 write int32 to stream

func (*Stream) WriteInt64

func (stream *Stream) WriteInt64(nval int64)

WriteInt64 write int64 to stream

func (*Stream) WriteInt8

func (stream *Stream) WriteInt8(nval int8)

WriteInt8 write int8 to stream

func (*Stream) WriteMore

func (stream *Stream) WriteMore()

WriteMore write , with possible indention

func (*Stream) WriteNil

func (stream *Stream) WriteNil()

WriteNil write null to stream

func (*Stream) WriteObjectEnd

func (stream *Stream) WriteObjectEnd()

WriteObjectEnd write } with possible indention

func (*Stream) WriteObjectField

func (stream *Stream) WriteObjectField(field string)

WriteObjectField write "field": with possible indention

func (*Stream) WriteObjectStart

func (stream *Stream) WriteObjectStart()

WriteObjectStart write { with possible indention

func (*Stream) WriteRaw

func (stream *Stream) WriteRaw(s string)

WriteRaw write string out without quotes, just like []byte

func (*Stream) WriteString

func (stream *Stream) WriteString(s string)

WriteString write string to stream without html escape

func (*Stream) WriteStringWithHTMLEscaped

func (stream *Stream) WriteStringWithHTMLEscaped(s string)

WriteStringWithHTMLEscaped write string to stream with html special characters escaped

func (*Stream) WriteTrue

func (stream *Stream) WriteTrue()

WriteTrue write true to stream

func (*Stream) WriteUint

func (stream *Stream) WriteUint(val uint)

WriteUint write uint to stream

func (*Stream) WriteUint16

func (stream *Stream) WriteUint16(val uint16)

WriteUint16 write uint16 to stream

func (*Stream) WriteUint32

func (stream *Stream) WriteUint32(val uint32)

WriteUint32 write uint32 to stream

func (*Stream) WriteUint64

func (stream *Stream) WriteUint64(val uint64)

WriteUint64 write uint64 to stream

func (*Stream) WriteUint8

func (stream *Stream) WriteUint8(val uint8)

WriteUint8 write uint8 to stream

func (*Stream) WriteVal

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

WriteVal copy the go interface into underlying JSON, same as json.Marshal

type StreamPool

StreamPool a thread safe pool of streams with same configuration

type StreamPool interface {
    BorrowStream(writer io.Writer) *Stream
    ReturnStream(stream *Stream)
}

type StructDescriptor

StructDescriptor describe how should we encode/decode the struct

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

func (*StructDescriptor) GetField

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

GetField get one field from the descriptor by its name. Can not use map here to keep field orders.

type ValDecoder

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 ValDecoder interface {
    Decode(ptr unsafe.Pointer, iter *Iterator)
}

type ValEncoder

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 ValEncoder interface {
    IsEmpty(ptr unsafe.Pointer) bool
    Encode(ptr unsafe.Pointer, stream *Stream)
}

type ValueType

ValueType the type for JSON element

type ValueType int
const (
    // InvalidValue invalid JSON element
    InvalidValue ValueType = iota
    // StringValue JSON element "string"
    StringValue
    // NumberValue JSON element 100 or 0.10
    NumberValue
    // NilValue JSON element null
    NilValue
    // BoolValue JSON element true or false
    BoolValue
    // ArrayValue JSON element []
    ArrayValue
    // ObjectValue JSON element {}
    ObjectValue
)

Subdirectories

Name Synopsis
..
extra