json

package
v0.0.0-...-6f9c769 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package json contains a JSON lexer implementation.

It is expected that it is mostly used with generated parser code, so the interface is tuned for a parser that knows what kind of data is expected.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lexer

type Lexer struct {
	Data []byte // Input data given to the lexer.

	UseMultipleErrors bool  // If we want to use multiple errors.
	FatalError        error // Fatal error occurred during lexing. It is usually a syntax error.
	// contains filtered or unexported fields
}

Lexer is a JSON lexer: it iterates over JSON tokens in a byte slice.

func (*Lexer) AddError

func (r *Lexer) AddError(e error)

func (*Lexer) AddNonFatalError

func (r *Lexer) AddNonFatalError(e error)

func (*Lexer) Bool

func (r *Lexer) Bool() bool

Bool reads a true or false boolean keyword.

func (*Lexer) Bytes

func (r *Lexer) Bytes() []byte

Bytes reads a string literal and base64 decodes it into a byte slice.

func (*Lexer) Consumed

func (r *Lexer) Consumed()

Consumed reads all remaining bytes from the input, publishing an error if there is anything but whitespace remaining.

func (*Lexer) Delim

func (r *Lexer) Delim(c byte)

Delim consumes a token and verifies that it is the given delimiter.

func (*Lexer) Error

func (r *Lexer) Error() error

func (*Lexer) FetchToken

func (r *Lexer) FetchToken()

FetchToken scans the input for the next token.

func (*Lexer) Float32

func (r *Lexer) Float32() float32

func (*Lexer) Float32Str

func (r *Lexer) Float32Str() float32

func (*Lexer) Float64

func (r *Lexer) Float64() float64

func (*Lexer) Float64Str

func (r *Lexer) Float64Str() float64

func (*Lexer) GetNonFatalErrors

func (r *Lexer) GetNonFatalErrors() []*LexerError

func (*Lexer) GetPos

func (r *Lexer) GetPos() int

func (*Lexer) Int

func (r *Lexer) Int() int

func (*Lexer) Int16

func (r *Lexer) Int16() int16

func (*Lexer) Int16Str

func (r *Lexer) Int16Str() int16

func (*Lexer) Int32

func (r *Lexer) Int32() int32

func (*Lexer) Int32Str

func (r *Lexer) Int32Str() int32

func (*Lexer) Int64

func (r *Lexer) Int64() int64

func (*Lexer) Int64Str

func (r *Lexer) Int64Str() int64

func (*Lexer) Int8

func (r *Lexer) Int8() int8

func (*Lexer) Int8Str

func (r *Lexer) Int8Str() int8

func (*Lexer) IntStr

func (r *Lexer) IntStr() int

func (*Lexer) Interface

func (r *Lexer) Interface() interface{}

Interface fetches an interface{} analogous to the 'encoding/json' package.

func (*Lexer) IsDelim

func (r *Lexer) IsDelim(c byte) bool

IsDelim returns true if there was no scanning error and next token is the given delimiter.

func (*Lexer) IsNull

func (r *Lexer) IsNull() bool

IsNull returns true if the next token is a null keyword.

func (*Lexer) IsStart

func (r *Lexer) IsStart() bool

IsStart returns whether the lexer is positioned at the start of an input string.

func (*Lexer) JsonNumber

func (r *Lexer) JsonNumber() json.Number

JsonNumber fetches and json.Number from 'encoding/json' package. Both int, float or string, contains them are valid values

func (*Lexer) Null

func (r *Lexer) Null()

Null verifies that the next token is null and consumes it.

func (*Lexer) Ok

func (r *Lexer) Ok() bool

Ok returns true if no error (including io.EOF) was encountered during scanning.

func (*Lexer) Raw

func (r *Lexer) Raw() []byte

Raw fetches the next item recursively as a data slice

func (*Lexer) Skip

func (r *Lexer) Skip()

Skip skips a single token.

func (*Lexer) SkipRecursive

func (r *Lexer) SkipRecursive()

SkipRecursive skips next array or object completely, or just skips a single token if not an array/object.

Note: no syntax validation is performed on the skipped data.

func (*Lexer) String

func (r *Lexer) String() string

String reads a string literal.

func (*Lexer) Uint

func (r *Lexer) Uint() uint

func (*Lexer) Uint16

func (r *Lexer) Uint16() uint16

func (*Lexer) Uint16Str

func (r *Lexer) Uint16Str() uint16

func (*Lexer) Uint32

func (r *Lexer) Uint32() uint32

func (*Lexer) Uint32Str

func (r *Lexer) Uint32Str() uint32

func (*Lexer) Uint64

func (r *Lexer) Uint64() uint64

func (*Lexer) Uint64Str

func (r *Lexer) Uint64Str() uint64

func (*Lexer) Uint8

func (r *Lexer) Uint8() uint8

func (*Lexer) Uint8Str

func (r *Lexer) Uint8Str() uint8

func (*Lexer) UintStr

func (r *Lexer) UintStr() uint

func (*Lexer) UintptrStr

func (r *Lexer) UintptrStr() uintptr

func (*Lexer) UnsafeBytes

func (r *Lexer) UnsafeBytes() []byte

UnsafeBytes returns the byte slice if the token is a string literal.

func (*Lexer) UnsafeFieldName

func (r *Lexer) UnsafeFieldName(skipUnescape bool) string

UnsafeFieldName returns current member name string token

func (*Lexer) UnsafeString

func (r *Lexer) UnsafeString() string

UnsafeString returns the string value if the token is a string literal.

Warning: returned string may point to the input buffer, so the string should not outlive the input buffer. Intended pattern of usage is as an argument to a switch statement.

func (*Lexer) WantColon

func (r *Lexer) WantColon()

WantColon requires a colon to be present before fetching next token.

func (*Lexer) WantComma

func (r *Lexer) WantComma()

WantComma requires a comma to be present before fetching next token.

type LexerError

type LexerError struct {
	Reason string
	Offset int
	Data   string
}

LexerError implements the error interface and represents all possible errors that can be generated during parsing the JSON data.

func (*LexerError) Error

func (l *LexerError) Error() string

type Reader

type Reader struct {
	Lexer      Lexer
	Type       uint16
	First      string
	OutOfOrder bool
	IsCompact  bool
	NoFields   bool
}

func OpenReader

func OpenReader(b []byte) (Reader, error)

func (*Reader) Bool

func (r *Reader) Bool() bool

func (*Reader) Error

func (r *Reader) Error() error

func (*Reader) FieldName

func (r *Reader) FieldName() (string, error)

func (*Reader) Float32

func (r *Reader) Float32() float32

func (*Reader) Float64

func (r *Reader) Float64() float64

func (*Reader) Int16

func (r *Reader) Int16() int16

func (*Reader) Int32

func (r *Reader) Int32() int32

func (*Reader) Int64

func (r *Reader) Int64() int64

func (*Reader) Int8

func (r *Reader) Int8() int8

func (*Reader) IsError

func (r *Reader) IsError() bool

func (*Reader) String

func (r *Reader) String() string

func (*Reader) Uint16

func (r *Reader) Uint16() uint16

func (*Reader) Uint32

func (r *Reader) Uint32() uint32

func (*Reader) Uint64

func (r *Reader) Uint64() uint64

func (*Reader) Uint8

func (r *Reader) Uint8() uint8

func (*Reader) WantComma

func (r *Reader) WantComma()

type Writer

type Writer struct {
	Data         []byte
	Error        error
	Compact      bool
	NoEscapeHTML bool
}

func NewCompactWriterFloat32

func NewCompactWriterFloat32(b []byte, typ uint16, first float32) Writer

func NewCompactWriterFloat64

func NewCompactWriterFloat64(b []byte, typ uint16, first float64) Writer

func NewCompactWriterInt16

func NewCompactWriterInt16(b []byte, typ uint16, first int16) Writer

func NewCompactWriterInt32

func NewCompactWriterInt32(b []byte, typ uint16, first int32) Writer

func NewCompactWriterInt64

func NewCompactWriterInt64(b []byte, typ uint16, first int64) Writer

func NewCompactWriterInt8

func NewCompactWriterInt8(b []byte, typ uint16, first int8) Writer

func NewCompactWriterUint16

func NewCompactWriterUint16(b []byte, typ uint16, first uint16) Writer

func NewCompactWriterUint32

func NewCompactWriterUint32(b []byte, typ uint16, first uint32) Writer

func NewCompactWriterUint64

func NewCompactWriterUint64(b []byte, typ uint16, first uint64) Writer

func NewCompactWriterUint8

func NewCompactWriterUint8(b []byte, typ uint16, first uint8) Writer

func NewWriter

func NewWriter(b []byte, typ uint16) Writer

func (*Writer) Base64Bytes

func (w *Writer) Base64Bytes(data []byte)

Base64Bytes appends data to the buffer after base64 encoding it

func (*Writer) Bool

func (w *Writer) Bool(v bool)

func (*Writer) BoolCompact

func (w *Writer) BoolCompact(v bool) *Writer

func (*Writer) BoolField

func (w *Writer) BoolField(n string, v bool)

func (*Writer) Finish

func (w *Writer) Finish() []byte

func (*Writer) Float32

func (w *Writer) Float32(n float32)

func (*Writer) Float32Compact

func (w *Writer) Float32Compact(v float32) *Writer

func (*Writer) Float32Field

func (w *Writer) Float32Field(n string, v float32)

func (*Writer) Float32Str

func (w *Writer) Float32Str(n float32)

func (*Writer) Float64

func (w *Writer) Float64(n float64)

func (*Writer) Float64Compact

func (w *Writer) Float64Compact(v float64) *Writer

func (*Writer) Float64Field

func (w *Writer) Float64Field(n string, v float64)

func (*Writer) Float64Str

func (w *Writer) Float64Str(n float64)

func (*Writer) Float64StrField

func (w *Writer) Float64StrField(n string, v float64)

func (*Writer) Int

func (w *Writer) Int(n int)

func (*Writer) Int16

func (w *Writer) Int16(n int16)

func (*Writer) Int16Compact

func (w *Writer) Int16Compact(v int16) *Writer

func (*Writer) Int16Field

func (w *Writer) Int16Field(n string, v int16)

func (*Writer) Int16Str

func (w *Writer) Int16Str(n int16)

func (*Writer) Int32

func (w *Writer) Int32(n int32)

func (*Writer) Int32Compact

func (w *Writer) Int32Compact(v int32) *Writer

func (*Writer) Int32Field

func (w *Writer) Int32Field(n string, v int32)

func (*Writer) Int32Str

func (w *Writer) Int32Str(n int32)

func (*Writer) Int64

func (w *Writer) Int64(n int64)

func (*Writer) Int64Compact

func (w *Writer) Int64Compact(v int64) *Writer

func (*Writer) Int64Field

func (w *Writer) Int64Field(n string, v int64)

func (*Writer) Int64Str

func (w *Writer) Int64Str(n int64)

func (*Writer) Int8

func (w *Writer) Int8(n int8)

func (*Writer) Int8Compact

func (w *Writer) Int8Compact(v int8) *Writer

func (*Writer) Int8Field

func (w *Writer) Int8Field(n string, v int8)

func (*Writer) Int8Str

func (w *Writer) Int8Str(n int8)

func (*Writer) IntCompact

func (w *Writer) IntCompact(v int) *Writer

func (*Writer) IntField

func (w *Writer) IntField(n string, v int)

func (*Writer) IntStr

func (w *Writer) IntStr(n int)

func (*Writer) Raw

func (w *Writer) Raw(data []byte, err error)

Raw appends raw binary data to the buffer or sets the error if it is given. Useful for calling with results of MarshalJSONTo-like functions.

func (*Writer) RawByte

func (w *Writer) RawByte(c byte)

RawByte appends raw binary data to the buffer.

func (*Writer) RawString

func (w *Writer) RawString(s string)

RawString appends raw binary data to the buffer.

func (*Writer) RawText

func (w *Writer) RawText(data []byte, err error)

RawText encloses raw binary data in quotes and appends in to the buffer. Useful for calling with results of MarshalText-like functions.

func (*Writer) String

func (w *Writer) String(s string)

func (*Writer) StringCompact

func (w *Writer) StringCompact(v string) *Writer

func (*Writer) StringField

func (w *Writer) StringField(n string, v string)

func (*Writer) Uint

func (w *Writer) Uint(n uint)

func (*Writer) Uint16

func (w *Writer) Uint16(n uint16)

func (*Writer) Uint16Compact

func (w *Writer) Uint16Compact(v uint16) *Writer

func (*Writer) Uint16Field

func (w *Writer) Uint16Field(n string, v uint16)

func (*Writer) Uint16Str

func (w *Writer) Uint16Str(n uint16)

func (*Writer) Uint32

func (w *Writer) Uint32(n uint32)

func (*Writer) Uint32Compact

func (w *Writer) Uint32Compact(v uint32) *Writer

func (*Writer) Uint32Field

func (w *Writer) Uint32Field(n string, v uint32)

func (*Writer) Uint32Str

func (w *Writer) Uint32Str(n uint32)

func (*Writer) Uint64

func (w *Writer) Uint64(n uint64)

func (*Writer) Uint64Compact

func (w *Writer) Uint64Compact(v uint64) *Writer

func (*Writer) Uint64Field

func (w *Writer) Uint64Field(n string, v uint64)

func (*Writer) Uint64Str

func (w *Writer) Uint64Str(n uint64)

func (*Writer) Uint8

func (w *Writer) Uint8(n uint8)

func (*Writer) Uint8Compact

func (w *Writer) Uint8Compact(v uint8) *Writer

func (*Writer) Uint8Field

func (w *Writer) Uint8Field(n string, v uint8)

func (*Writer) Uint8Str

func (w *Writer) Uint8Str(n uint8)

func (*Writer) UintCompact

func (w *Writer) UintCompact(v uint) *Writer

func (*Writer) UintField

func (w *Writer) UintField(n string, v uint)

func (*Writer) UintStr

func (w *Writer) UintStr(n uint)

func (*Writer) UintptrStr

func (w *Writer) UintptrStr(n uintptr)

Jump to

Keyboard shortcuts

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