jsoniter

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 12 Imported by: 0

README

This package is a modified extract of https://github.com/json-iterator/go (MIT License, not maintained)


json-iterator/go

This Package

This package only provides a subset of the original features:

  • JSON iterator without reflection related features
  • JSON stream without reflection related features nor indention logic

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{}.Froze()

ConfigDefault the default API

Functions

func AppendString

func AppendString(buf *[]byte, str string)

func WriteStringSlowPath

func WriteStringSlowPath(buf *[]byte, i int, s string)

Types

type API

type API interface {
	IteratorPool
	StreamPool
	Valid(data []byte) bool
}

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

type Config

type Config struct {
	IndentionStep int
}

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

func (Config) Froze

func (cfg Config) Froze() API

Froze forge API from config

type Iterator

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

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.

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) 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 reads an object and calls callback each time it reads a key.

func (*Iterator) ReadObjectMinimizeAllocationsCB

func (iter *Iterator) ReadObjectMinimizeAllocationsCB(callbackFn func(it *Iterator, key []byte, allocated bool) bool) bool

ReadObjectAvoidAllocationsCB reads an object and calls callbackFn each time it reads a key, keys that don't require decoding are passed to the callbackFn without allocation. If the callback function is called with the allocated argument to true that means the key has been decoded, the key argument can be stored and modified. Otherwise the key should only be accessed during the duration of the current callback.

func (*Iterator) ReadString

func (iter *Iterator) ReadString() string

ReadString reads and decodes a JSON string literal.

func (*Iterator) ReadStringAsBytes

func (iter *Iterator) ReadStringAsBytes() (ret []byte, allocated bool)

ReadStringAsBytes reads and decodes a JSON string literal. If allocated is false the slice should not be stored or modified, and it should not be read or modified after any mutation of the 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) 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

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

IteratorPool a thread safe pool of iterators with same configuration

type Stream

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

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.

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

type StreamPool

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

StreamPool a thread safe pool of streams with same configuration

type ValueType

type ValueType int

ValueType the type for JSON element

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
)

Jump to

Keyboard shortcuts

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