jsoniter

package module
v0.0.0-...-22f9531 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2017 License: MIT Imports: 11 Imported by: 0

README

rcard

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go

Why jsoniter?

  • Jsoniter is the fastest JSON parser. It could be up to 10x faster than normal parser, data binding included. Shameless self benchmark
  • Extremely flexible api. You can mix and match three different styles: bind-api, any-api or iterator-api. Checkout your api choices
  • Unique iterator api can iterate through JSON directly, zero memory allocation! See how iterator works

Show off

Here is a quick show off, for more complete report you can checkout the full benchmark with in-depth optimization to back the numbers up

go-medium

Bind-API is the best

Bind-api should always be the first choice. Given this JSON document [0,1,2,3]

Parse with Go bind-api

import "github.com/json-iterator/go"
iter := jsoniter.ParseString(`[0,1,2,3]`)
var := iter.Read()
fmt.Println(val)

Iterator-API for quick extraction

When you do not need to get all the data back, just extract some.

Parse with Go iterator-api

import "github.com/json-iterator/go"
iter := ParseString(`[0, [1, 2], [3, 4], 5]`)
count := 0
for iter.ReadArray() {
    iter.Skip()
    count++
}
fmt.Println(count) // 4

Any-API for maximum flexibility

Parse with Go any-api

import "github.com/json-iterator/go"
iter := jsoniter.ParseString(`[{"field1":"11","field2":"12"},{"field1":"21","field2":"22"}]`)
val := iter.ReadAny()
fmt.Println(val.ToInt(1, "field2")) // 22

Notice you can extract from nested data structure, and convert any type to the type to you want.

How to get

go get github.com/json-iterator/go

Contribution Welcomed !

Report issue or pull request, or email taowen@gmail.com, or Gitter chat

Documentation

Index

Constants

View Source
const (
	// Missed represent not follow the path
	Missed int = 0

	// FollowThePath represent follow the path
	FollowThePath int = 1
)
View Source
const (
	// TypeObject value will be a object
	TypeObject int = 0

	// TypeArray value will be a array
	TypeArray int = 1
)

Variables

View Source
var (
	// ErrInvalidPath represent a invalid path
	ErrInvalidPath = errors.New("Invalid Path")

	// ErrInvalidKeyReader represent not set a correct reader
	ErrInvalidKeyReader = errors.New("Invalid Key Reader")
)
View Source
var DECODERS unsafe.Pointer
View Source
var DIGITS []uint32
View Source
var ENCODERS unsafe.Pointer
View Source
var POW10 []uint64

Functions

func CleanDecoders

func CleanDecoders()

CleanDecoders cleans decoders registered

func Marshal

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

func MarshalToString

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

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

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

Unmarshal adapts to json/encoding APIs

func UnmarshalFromString

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

func WriteToStream

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

Types

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 UnmarshalAny

func UnmarshalAny(data []byte) (Any, error)

func UnmarshalAnyFromString

func UnmarshalAnyFromString(str string) (Any, error)

func Wrap

func Wrap(val interface{}) Any

func WrapFloat64

func WrapFloat64(val float64) Any

func WrapInt32

func WrapInt32(val int32) Any

func WrapInt64

func WrapInt64(val int64) Any

func WrapString

func WrapString(val string) Any

func WrapUint32

func WrapUint32(val uint32) Any

func WrapUint64

func WrapUint64(val uint64) Any

type Decoder

type Decoder interface {
	// contains filtered or unexported methods
}

type DecoderFunc

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

type Encoder

type Encoder interface {
	// contains filtered or unexported methods
}

type EncoderFunc

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

type ExtensionFunc

type ExtensionFunc func(typ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc)

type Extract

type Extract struct {
	Path       Path
	KeyReaders []KeyReader
	// contains filtered or unexported fields
}

Extract represent a Path with several KeyReader in one object followed by the path

func NewExtract

func NewExtract(path Path, readers ...KeyReader) *Extract

NewExtract constructor

type ExtractMany

type ExtractMany struct {
	Iter     *Iterator
	Extracts []*Extract
}

ExtractMany will extract several path ------------------------------------------- {"type":"XXX","a":"b",c:[1,3,5,8,{"d":"e","f":[23,"big"]}]}

func NewExtractMany

func NewExtractMany(iter *Iterator, extracts ...*Extract) *ExtractMany

NewExtractMany constructor

func (*ExtractMany) ExtractArray

func (em *ExtractMany) ExtractArray() error

ExtractArray API

func (*ExtractMany) ExtractObject

func (em *ExtractMany) ExtractObject() error

ExtractObject API

type Field

type Field string

Field represent a object field

func (Field) Equal

func (k Field) Equal(ik IKey) bool

Equal implement IKey

func (Field) Int

func (k Field) Int() int

Int implement IKey

func (Field) String

func (k Field) String() string

func (Field) Type

func (k Field) Type() int

Type implement IKey

type IKey

type IKey interface {
	Type() int
	Equal(k IKey) bool
	Int() int
	String() string
}

IKey represent a Key

type Index

type Index int

Index be the key

func (Index) Equal

func (i Index) Equal(k IKey) bool

Equal implement IKey

func (Index) Int

func (i Index) Int() int

Int implement IKey

func (Index) String

func (i Index) String() string

func (Index) Type

func (i Index) Type() int

Type implement IKey

type Iterator

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

Iterator is a fast and flexible JSON parser

func NewIterator

func NewIterator() *Iterator

Create creates an empty Iterator instance

func Parse

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

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

func ParseBytes

func ParseBytes(input []byte) *Iterator

ParseBytes parses a json byte slice into an Iterator instance

func ParseString

func ParseString(input string) *Iterator

ParseString parses a json string into an Iterator instance

func (*Iterator) CurrentBuffer

func (iter *Iterator) CurrentBuffer() string

CurrentBuffer gets current buffer as string

func (*Iterator) Read

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

func (*Iterator) ReadAny

func (iter *Iterator) ReadAny() Any

func (*Iterator) ReadArray

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

func (*Iterator) ReadArrayCB

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

func (*Iterator) ReadBase64

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

ReadBase64 reads a json object as Base64 in byte slice

func (*Iterator) ReadBool

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

ReadBool reads a json object as Bool

func (*Iterator) ReadFloat32

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

func (*Iterator) ReadFloat64

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

func (*Iterator) ReadInt

func (iter *Iterator) ReadInt() int

func (*Iterator) ReadInt16

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

func (*Iterator) ReadInt32

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

func (*Iterator) ReadInt64

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

func (*Iterator) ReadInt8

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

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) ReadObject

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

func (*Iterator) ReadObjectCB

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

func (*Iterator) ReadString

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

TODO: avoid append

func (*Iterator) ReadStringAsSlice

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

func (*Iterator) ReadUint

func (iter *Iterator) ReadUint() uint

func (*Iterator) ReadUint16

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

func (*Iterator) ReadUint32

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

func (*Iterator) ReadUint64

func (iter *Iterator) ReadUint64() uint64

func (*Iterator) ReadUint8

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

func (*Iterator) ReadVal

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

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

func (*Iterator) Reset

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

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

func (*Iterator) ResetBytes

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

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) WhatIsNext

func (iter *Iterator) WhatIsNext() ValueType

WhatIsNext gets ValueType of relatively next json object

type KeyBool

type KeyBool struct {
	Key     IKey
	MayNull bool

	Bool bool
	// contains filtered or unexported fields
}

KeyBool ---------------------------------------------------------

func (*KeyBool) HasRead

func (kb *KeyBool) HasRead() bool

HasRead implement the KeyReader

func (*KeyBool) MaybeNull

func (kb *KeyBool) MaybeNull() bool

MaybeNull implement the KeyReader

func (*KeyBool) Read

func (kb *KeyBool) Read(iter *Iterator, key IKey) (bool, error)

type KeyFloat64

type KeyFloat64 struct {
	Key     IKey
	MayNull bool

	Float64 float64
	// contains filtered or unexported fields
}

KeyFloat64 -------------------------------------------------------

func (*KeyFloat64) HasRead

func (kf *KeyFloat64) HasRead() bool

HasRead implement the KeyReader

func (*KeyFloat64) MaybeNull

func (kf *KeyFloat64) MaybeNull() bool

MaybeNull implement the KeyReader

func (*KeyFloat64) Read

func (kf *KeyFloat64) Read(iter *Iterator, key IKey) (bool, error)

type KeyInt64

type KeyInt64 struct {
	Key     IKey
	MayNull bool

	Int64 int64
	// contains filtered or unexported fields
}

KeyInt64 ----------------------------------------------------------

func (*KeyInt64) HasRead

func (ki *KeyInt64) HasRead() bool

HasRead implement the KeyReader

func (*KeyInt64) MaybeNull

func (ki *KeyInt64) MaybeNull() bool

MaybeNull implement the KeyReader

func (*KeyInt64) Read

func (ki *KeyInt64) Read(iter *Iterator, key IKey) (bool, error)

type KeyReader

type KeyReader interface {
	Read(iter *Iterator, key IKey) (bool, error)
	HasRead() bool
	MaybeNull() bool
}

KeyReader represent a read to read the key

type KeyString

type KeyString struct {
	Key     IKey
	MayNull bool

	String string
	// contains filtered or unexported fields
}

KeyString --------------------------------

func (*KeyString) HasRead

func (k *KeyString) HasRead() bool

HasRead implement the KeyReader

func (*KeyString) MaybeNull

func (k *KeyString) MaybeNull() bool

MaybeNull implement the KeyReader

func (*KeyString) Read

func (k *KeyString) Read(iter *Iterator, key IKey) (bool, error)

Read implement KeyReader

type Path

type Path []IKey

Path represent a path to extract var input2 = `{"a":["b",{"c","d"}]}` c: NewPath(Field("a"), Index(1))

func NewPath

func NewPath(keys ...IKey) Path

NewPath contruct a path

type Stream

type Stream struct {
	Error error

	IndentionStep int
	// contains filtered or unexported fields
}

func NewStream

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

func (*Stream) Available

func (b *Stream) Available() int

Available returns how many bytes are unused in the buffer.

func (*Stream) Buffered

func (b *Stream) Buffered() int

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

func (*Stream) Flush

func (b *Stream) Flush() error

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

func (*Stream) Reset

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

func (*Stream) Write

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

func (stream *Stream) WriteArrayEnd()

func (*Stream) WriteArrayStart

func (stream *Stream) WriteArrayStart()

func (*Stream) WriteBool

func (stream *Stream) WriteBool(val bool)

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 *Stream) WriteFloat32(val float32)

func (*Stream) WriteFloat64

func (stream *Stream) WriteFloat64(val float64)

func (*Stream) WriteInt

func (stream *Stream) WriteInt(val int)

func (*Stream) WriteInt16

func (stream *Stream) WriteInt16(nval int16)

func (*Stream) WriteInt32

func (stream *Stream) WriteInt32(nval int32)

func (*Stream) WriteInt64

func (stream *Stream) WriteInt64(nval int64)

func (*Stream) WriteInt8

func (stream *Stream) WriteInt8(nval int8)

func (*Stream) WriteMore

func (stream *Stream) WriteMore()

func (*Stream) WriteNil

func (stream *Stream) WriteNil()

func (*Stream) WriteObjectEnd

func (stream *Stream) WriteObjectEnd()

func (*Stream) WriteObjectField

func (stream *Stream) WriteObjectField(field string)

func (*Stream) WriteObjectStart

func (stream *Stream) WriteObjectStart()

func (*Stream) WriteRaw

func (b *Stream) WriteRaw(s string)

func (*Stream) WriteString

func (stream *Stream) WriteString(s string)

func (*Stream) WriteTrue

func (stream *Stream) WriteTrue()

func (*Stream) WriteUint

func (stream *Stream) WriteUint(val uint)

func (*Stream) WriteUint16

func (stream *Stream) WriteUint16(val uint16)

func (*Stream) WriteUint32

func (stream *Stream) WriteUint32(val uint32)

func (*Stream) WriteUint64

func (stream *Stream) WriteUint64(val uint64)

func (*Stream) WriteUint8

func (stream *Stream) WriteUint8(val uint8)

func (*Stream) WriteVal

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

type ValueType

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL