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 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, ...)
- 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
- type Binding
- type Config
- type Decoder
- 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
- 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 (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) 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) 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) 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) 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
- type OptionalDecoder
- type OptionalEncoder
- type RawMessage
- type 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) 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 (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) 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 StreamPool
- type StructDescriptor
- type ValDecoder
- type ValEncoder
- type ValueType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ConfigCompatibleWithStandardLibrary = Config{ EscapeHTML: true, SortMapKeys: true, ValidateJsonRawMessage: true, }.Froze()
ConfigCompatibleWithStandardLibrary tries to be 100% compatible with standard library behavior
var ConfigDefault = Config{ EscapeHTML: true, }.Froze()
ConfigDefault the default API
var ConfigFastest = Config{ EscapeHTML: false, MarshalFloatWith6Digits: true, ObjectFieldMustBeSimpleString: true, }.Froze()
ConfigFastest marshals float with only 6 digits precision
Functions ¶
func CastJsonNumber ¶
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 := 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 ¶
MarshalIndent same as json.MarshalIndent. Prefix is not supported.
func MarshalToString ¶
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 ¶
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 := 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 ¶
UnmarshalFromString is a convenient method to read from string instead of []byte
Types ¶
type API ¶
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
}
API the public interface of this package. Primary Marshal and Unmarshal.
type Any ¶
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)
}
Any generic object representation. The lazy json implementation holds []byte and parse lazily.
type Binding ¶
type Binding struct {
Field reflect2.StructField
FromNames []string
ToNames []string
Encoder ValEncoder
Decoder ValDecoder
// contains filtered or unexported fields
}
Binding describe how should we encode/decode the struct field
type Config ¶
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
}
Config customize how the API should behave. The API is created from Config by Froze.
type Decoder ¶
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 ¶
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) 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.
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 DummyExtension ¶
type DummyExtension struct {
}
DummyExtension embed this type get dummy implementation for all methods of Extension
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)

