# README
JSON

This package is a JSON lexer (ECMA-404) written in Go. It follows the specification at JSON. The lexer takes an io.Reader and converts it into tokens until the EOF.
Installation
Run the following command
go get github.com/tdewolff/parse/json
or add the following import and run project with go get
import "github.com/tdewolff/parse/json"
Parser
Usage
The following initializes a new Parser with io.Reader r
:
p := json.NewParser(r)
To tokenize until EOF an error, use:
for {
gt, text := p.Next()
switch gt {
case json.ErrorGrammar:
// error or EOF set in p.Err()
return
// ...
}
}
All grammars:
ErrorGrammar GrammarType = iota // extra grammar when errors occur
WhitespaceGrammar // space \t \r \n
LiteralGrammar // null true false
NumberGrammar
StringGrammar
StartObjectGrammar // {
EndObjectGrammar // }
StartArrayGrammar // [
EndArrayGrammar // ]
Examples
package main
import (
"os"
"github.com/tdewolff/parse/json"
)
// Tokenize JSON from stdin.
func main() {
p := json.NewParser(os.Stdin)
for {
gt, text := p.Next()
switch gt {
case json.ErrorGrammar:
if p.Err() != io.EOF {
fmt.Println("Error on line", p.Line(), ":", p.Err())
}
return
case json.LiteralGrammar:
fmt.Println("Literal", string(text))
case json.NumberGrammar:
fmt.Println("Number", string(text))
// ...
}
}
}
License
Released under the MIT license.
# Functions
NewParser returns a new Parser for a given io.Reader.
# Constants
State values.
].
}.
extra grammar when errors occur.
GrammarType values.
GrammarType values.
State values.
State values.
[.
{.
GrammarType values.
extra token when errors occur.
GrammarType values.
# Variables
ErrBadArrayEnding is returned when an unexpected right bracket is encountered.
ErrBadComma is returned when an unexpected comma is encountered.
ErrBadObjectDeclaration is returned when the object key is not followed by a colon character.
ErrBadObjectEnding is returned when an unexpected right brace is encountered.
ErrBadObjectKey is returned when the object key is not a quoted string.
ErrNoComma is returned when no comma is present between two values.
# Type aliases
GrammarType determines the type of grammar.
State determines the current state the parser is in.