jsoniter

package module
v0.0.0-...-84b5b69 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2021 License: MIT Imports: 8 Imported by: 0

README

make json.Unmarshal work in tinygo

type NamedArray = []string

type RefNamedArray struct {
	Value NamedArray
}

import "encoding/json"

var val1 RefNamedArray
var val2 NamedArray
json.Unmarshal([]byte(`{ "Value": ["hello","world"] }`), &val1)
json.Unmarshal([]byte(`["hello","world"]`), &val2) 

The code above does not work in tinygo, due to incomplete runtime reflection support. To fix this, we use code generation instead of runtime reflection to implement json.Unmarshal

//go:generate go run github.com/json-iterator/tinygo/gen
type NamedArray = []string

//go:generate go run github.com/json-iterator/tinygo/gen
type RefNamedArray struct {
	Value NamedArray
}

import "github.com/json-iterator/tinygo"
// list all the types you need to unmarshal here
json := jsoniter.CreateJsonAdapter(RefNamedArray_json{}, NamedArray_json{}) 

var val1 RefNamedArray
var val2 NamedArray
json.Unmarshal([]byte(`{ "Value": ["hello","world"] }`), &val1)
json.Unmarshal([]byte(`["hello","world"]`), &val2) 

run go generate command to generate RefNamedArray_json and NamedArray_json.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 ParseBytes

func ParseBytes(input []byte) *Iterator

ParseBytes creates an Iterator instance from byte array

func (*Iterator) CurrentBuffer

func (iter *Iterator) CurrentBuffer() string

CurrentBuffer gets current buffer as string for debugging purpose

func (*Iterator) ReadArrayHead

func (iter *Iterator) ReadArrayHead() bool

ReadArrayHead tells if there is array element to read, if the value is not array will be skipped

func (*Iterator) ReadArrayMore

func (iter *Iterator) ReadArrayMore() bool

ReadArrayMore tells if there is more element to read

func (*Iterator) ReadBigFloat

func (iter *Iterator) ReadBigFloat(out *big.Float) error

ReadBigFloat read big.Float

func (*Iterator) ReadBigInt

func (iter *Iterator) ReadBigInt(out *big.Int) error

ReadBigInt read big.Int

func (*Iterator) ReadBool

func (iter *Iterator) ReadBool(out *bool) error

ReadBool will assign bool to out if found, otherwise the value will be skipped

func (*Iterator) ReadFloat32

func (iter *Iterator) ReadFloat32(out *float32) error

ReadFloat32 read float32

func (*Iterator) ReadFloat64

func (iter *Iterator) ReadFloat64(out *float64) error

ReadFloat64 read float64

func (*Iterator) ReadInt

func (iter *Iterator) ReadInt(out *int) (err error)

ReadInt read int

func (*Iterator) ReadInt16

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

ReadInt16 read int16

func (*Iterator) ReadInt32

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

ReadInt32 read int32

func (*Iterator) ReadInt64

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

ReadInt64 read int64

func (*Iterator) ReadInt8

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

ReadInt8 read int8

func (*Iterator) ReadInterface

func (iter *Iterator) ReadInterface(out *interface{}) (ret error)

func (*Iterator) ReadNumber

func (iter *Iterator) ReadNumber(out *Number) error

func (*Iterator) ReadObjectField

func (iter *Iterator) ReadObjectField() string

ReadObjectField will return field name or empty string if not found

func (*Iterator) ReadObjectHead

func (iter *Iterator) ReadObjectHead() bool

ReadObjectHead tells if there is object field to read

func (*Iterator) ReadObjectMore

func (iter *Iterator) ReadObjectMore() bool

ReadObjectMore tells if there is more field to read

func (*Iterator) ReadRawMessage

func (iter *Iterator) ReadRawMessage(out *RawMessage) (ret error)

func (*Iterator) ReadString

func (iter *Iterator) ReadString(out *string) error

ReadString will assign string to out if found, otherwise the value will be skipped

func (*Iterator) ReadUint

func (iter *Iterator) ReadUint(out *uint) (err error)

ReadUint read uint

func (*Iterator) ReadUint16

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

ReadUint16 read uint16

func (*Iterator) ReadUint32

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

ReadUint32 read uint32

func (*Iterator) ReadUint64

func (iter *Iterator) ReadUint64(out *uint64) (ret error)

ReadUint64 read uint64

func (*Iterator) ReadUint8

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

ReadUint8 read uint8

func (*Iterator) ReportError

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

ReportError record a error in iterator instance with current position.

func (*Iterator) Skip

func (iter *Iterator) Skip() error

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

type JsonAdapter

type JsonAdapter struct {
	Unmarshal     func(bytes []byte, out interface{}) error
	Marshal       func(val interface{}) ([]byte, error)
	MarshalIndent func(val interface{}, prefix, indent string) ([]byte, error)
}

func CreateJsonAdapter

func CreateJsonAdapter(adapters ...TypeAdapter) JsonAdapter

type Number

type Number string

A Number represents a JSON number literal.

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 RawMessage

type RawMessage []byte

type Stream

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

func NewStream

func NewStream() *Stream

NewStream create new stream instance.

func (*Stream) Buffer

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

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

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

func (stream *Stream) WriteArrayHead()

WriteArrayHead write [ with possible indention

func (*Stream) WriteArrayTail

func (stream *Stream) WriteArrayTail()

WriteArrayTail 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) WriteFalse

func (stream *Stream) WriteFalse()

WriteFalse write false to stream

func (*Stream) WriteFloat32

func (stream *Stream) WriteFloat32(val float32) error

WriteFloat32 write float32 to stream

func (*Stream) WriteFloat64

func (stream *Stream) WriteFloat64(val float64) error

WriteFloat64 write float64 to stream

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

func (stream *Stream) WriteInterface(val interface{}) bool

func (*Stream) WriteMore

func (stream *Stream) WriteMore()

WriteMore write , with possible indention

func (*Stream) WriteNull

func (stream *Stream) WriteNull()

WriteNull write null to stream

func (*Stream) WriteObjectField

func (stream *Stream) WriteObjectField(field string)

WriteObjectField write "field": with possible indention

func (*Stream) WriteObjectHead

func (stream *Stream) WriteObjectHead()

WriteObjectHead write { with possible indention

func (*Stream) WriteObjectTail

func (stream *Stream) WriteObjectTail()

WriteObjectTail write } with possible indention

func (*Stream) WriteRaw

func (stream *Stream) WriteRaw(s string)

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

func (*Stream) WriteRawOrNull

func (stream *Stream) WriteRawOrNull(s string)

func (*Stream) WriteRawOrZero

func (stream *Stream) WriteRawOrZero(s string)

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 TypeAdapter

type TypeAdapter interface {
	Type() interface{}
	Unmarshal(iter *Iterator, out interface{})
	Marshal(stream *Stream, val interface{})
}

Directories

Path Synopsis
sub

Jump to

Keyboard shortcuts

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