package json

import "github.com/tdewolff/parse/json"

Package json is a JSON parser following the specifications at http://json.org/.

Index

Examples

Variables

var ErrBadArrayEnding = errors.New("unexpected right bracket character")

ErrBadArrayEnding is returned when an unexpected right bracket is encountered.

var ErrBadComma = errors.New("unexpected comma character outside an array or object")

ErrBadComma is returned when an unexpected comma is encountered.

var ErrBadObjectDeclaration = errors.New("expected colon character after object key")

ErrBadObjectDeclaration is returned when the object key is not followed by a colon character.

var ErrBadObjectEnding = errors.New("unexpected right brace character")

ErrBadObjectEnding is returned when an unexpected right brace is encountered.

var ErrBadObjectKey = errors.New("expected object key to be a quoted string")

ErrBadObjectKey is returned when the object key is not a quoted string.

var ErrNoComma = errors.New("expected comma character or an array or object ending")

ErrNoComma is returned when no comma is present between two values.

Types

type GrammarType

type GrammarType uint32

GrammarType determines the type of grammar

const (
	ErrorGrammar GrammarType = iota // extra grammar when errors occur
	WhitespaceGrammar
	LiteralGrammar
	NumberGrammar
	StringGrammar
	StartObjectGrammar // {
	EndObjectGrammar   // }
	StartArrayGrammar  // [
	EndArrayGrammar    // ]
)

GrammarType values.

func (GrammarType) String

func (gt GrammarType) String() string

String returns the string representation of a GrammarType.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is the state for the lexer.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new Parser for a given io.Reader.

Example

Code:

{
	p := NewParser(bytes.NewBufferString(`{"key": 5}`))
	out := ""
	for {
		state := p.State()
		tt, data := p.Next()
		if tt == ErrorGrammar {
			break
		}
		if state == ObjectKeyState && tt != EndObjectGrammar {
			out += "\""
		}
		out += string(data)
		if state == ObjectKeyState && tt != EndObjectGrammar {
			out += "\":"
		}
		// not handling comma insertion
	}
	fmt.Println(out)
	// Output: {"key":5}
}

Output:

{"key":5}

func (Parser) Err

func (p Parser) Err() error

Err returns the error encountered during tokenization, this is often io.EOF but also other errors can be returned.

func (*Parser) Next

func (p *Parser) Next() (GrammarType, []byte)

Next returns the next Grammar. It returns ErrorGrammar when an error was encountered. Using Err() one can retrieve the error message.

func (*Parser) State

func (p *Parser) State() State

State returns the state the parser is currently in (ie. which token is expected).

type State

type State uint32

State determines the current state the parser is in.

const (
	ValueState State = iota // extra token when errors occur
	ObjectKeyState
	ObjectValueState
	ArrayState
)

State values.

func (State) String

func (state State) String() string

String returns the string representation of a State.

Source Files

parse.go

Version
v1.1.0 (latest)
Published
Nov 2, 2015
Platform
darwin/amd64
Imports
4 packages
Last checked
1 hour ago

Tools for package owners.