jsonparse

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: BSD-3-Clause, GPL-2.0-or-later Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrParserExceededMaxDepth = errors.New("exceeded max depth")

Functions

This section is empty.

Types

type InvalidCharacterError added in v0.3.8

type InvalidCharacterError struct {
	Char   rune
	IsRune bool
	Where  string
}

func (*InvalidCharacterError) Error added in v0.3.8

func (e *InvalidCharacterError) Error() string

type Parser

type Parser struct {
	// Setting MaxError to a value greater than 0 causes
	// HandleRune to return ErrParserExceededMaxDepth if
	// objects/arrays become nested more deeply than this.
	MaxDepth int
	// contains filtered or unexported fields
}

Parser is the low-level JSON parser that powers both *Decoder and *ReEncoder.

func (*Parser) HandleEOF

func (par *Parser) HandleEOF() (RuneType, error)

HandleEOF feeds EOF to the Parser. The returned RuneType is either RuneTypeEOF or RuneTypeError.

An error is returned if and only if the RuneType is RuneTypeError. Returns io/fs.ErrClosed if .HandleEOF() has previously been called (and .Reset() has not been called since).

Once RuneTypeError or RuneTypeEOF has been returned, it will keep being returned from both .HandleRune(c) and .HandleEOF() until .Reset() is called.

RuneTypeEOF indicates that a complete JSON document has been read.

func (*Parser) HandleRune

func (par *Parser) HandleRune(c rune, isRune bool) (RuneType, error)

HandleRune feeds a Unicode rune to the Parser.

An error is returned if and only if the RuneType is RuneTypeError. Returns io/fs.ErrClosed if .HandleEOF() has previously been called (and .Reset() has not been called since).

Once RuneTypeError or RuneTypeEOF has been returned, it will keep being returned from both .HandleRune(c) and .HandleEOF() until .Reset() is called.

RuneTypeEOF indicates that the rune cannot be appended to the JSON document; a new JSON document must be started in order to process that rune.

func (*Parser) IsAtBarrier added in v0.3.7

func (par *Parser) IsAtBarrier() bool

IsAtBarrier returns whether a read-barrier has been reached and the next HandleRune call would definitely return RuneTypeEOF.

func (*Parser) PopBarrier

func (par *Parser) PopBarrier()

PopBarrier reverses a call to PushReadBarrier or PushWriteBarrier.

func (*Parser) PushReadBarrier

func (par *Parser) PushReadBarrier()

PushReadBarrier causes the parser to emit EOF once the end of the element that is started by the current top-of-stack is reached (which means that it will reject whitespace between the end of the element and EOF), until this is un-done with PopBarrier. It essentially turns the parser in to a sub-parser.

PushReadBarrier may only be called at the beginning of an element, whether that be

  • runeTypeAny
  • RuneTypeObjectBeg
  • RuneTypeArrayBeg
  • RuneTypeStringBeg
  • RuneTypeNumberIntNeg, RuneTypeNumberIntZero, RuneTypeNumberIntDig
  • RuneTypeTrueT
  • RuneTypeFalseF
  • RuneTypeNullN

func (*Parser) PushWriteBarrier

func (par *Parser) PushWriteBarrier()

PushWriteBarrier causes the parser to emit EOF once the end of the about-to-start element is reached and any trailing whitespace has been exhausted, until this is un-done with PopBarrier. It essentially turns the parser in to a sub-parser.

PushWriteBarrier may only be called at the places where an element of any type may start:

  • runeTypeAny for top-level and object-value elements
  • RuneTypeArrayBeg for array-item elements

PushWriteBarrier signals intent to write an element; if it is called in a place where an element is optional (at the beginning of an array), it becomes a syntax error to not write the element.

func (*Parser) Reset

func (par *Parser) Reset()

Reset all Parser state.

func (*Parser) StackIsEmpty

func (par *Parser) StackIsEmpty() bool

func (*Parser) StackSize

func (par *Parser) StackSize() int

type RuneType

type RuneType uint8

RuneType is the classification of a rune when parsing JSON input. A Parser, rather than grouping runes into tokens and classifying tokens, classifies runes directly.

const (
	RuneTypeError RuneType = iota

	RuneTypeSpace // whitespace

	RuneTypeObjectBeg   // '{'
	RuneTypeObjectColon // ':'
	RuneTypeObjectComma // ','
	RuneTypeObjectEnd   // '}'

	RuneTypeArrayBeg   // '['
	RuneTypeArrayComma // ','
	RuneTypeArrayEnd   // ']'

	RuneTypeStringBeg   // opening '"'
	RuneTypeStringChar  // normal character
	RuneTypeStringEsc   // backslash
	RuneTypeStringEsc1  // single-char after a backslash
	RuneTypeStringEscU  // \uABCD : u
	RuneTypeStringEscUA // \uABCD : A
	RuneTypeStringEscUB // \uABCD : B
	RuneTypeStringEscUC // \uABCD : C
	RuneTypeStringEscUD // \uABCD : D
	RuneTypeStringEnd   // closing '"'

	RuneTypeNumberIntNeg
	RuneTypeNumberIntZero // leading zero only; non-leading zeros are IntDig, not IntZero
	RuneTypeNumberIntDig
	RuneTypeNumberFracDot
	RuneTypeNumberFracDig
	RuneTypeNumberExpE
	RuneTypeNumberExpSign
	RuneTypeNumberExpDig

	RuneTypeTrueT
	RuneTypeTrueR
	RuneTypeTrueU
	RuneTypeTrueE

	RuneTypeFalseF
	RuneTypeFalseA
	RuneTypeFalseL
	RuneTypeFalseS
	RuneTypeFalseE

	RuneTypeNullN
	RuneTypeNullU
	RuneTypeNullL1
	RuneTypeNullL2

	RuneTypeEOF
)

func (RuneType) GoString

func (t RuneType) GoString() string

GoString implements fmt.GoStringer.

func (RuneType) IsNumber

func (t RuneType) IsNumber() bool

IsNumber returns whether the RuneType is one of the RuneTypeNumberXXX values.

func (RuneType) JSONType

func (t RuneType) JSONType() string

func (RuneType) String

func (t RuneType) String() string

String implements fmt.Stringer.

Jump to

Keyboard shortcuts

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