Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
A Event represents a point of interest in a JSON document.
Events are emitted at the start of objects, arrays, and for each values. For example, given the following JSON data:
{"foo": "bar"}
The following events will be emitted in order:
ObjectStartEvent ObjectKeyEvent StringEvent "foo" ObjectValueEvent StringEvent "bar" ObjectEndEvent
type EventType ¶
type EventType uint
EventType is the type of event generated.
const ( // UnknownEvent is an invalid event. UnknownEvent EventType = iota // ObjectStartEvent is emitted at the start of a JSON object. ObjectStartEvent // ObjectKeyEvent is emitted at the start of a key of a JSON object. ObjectKeyEvent // ObjectValueEvent is emitted at the start of a value of a JSON object. ObjectValueEvent // ObjectEndEvent is emitted at the end of a JSON object. ObjectEndEvent // ArrayStartEvent is emitted at the start of a JSON array. ArrayStartEvent // ArrayEndEvent is emitted at the end of a JSON array. ArrayEndEvent // StringEvent is emitted for each string. StringEvent // NumberEvent is emitted for each number. The associated value will be either a float64 or a int64. NumberEvent // BooleanEvent is emitted for each boolean value. BooleanEvent // NullEvent is emitted for each null value. NullEvent // EOFEvent is emitted when parsing has stopped, either because the source input is finished or because there was an error. EOFEvent )
type ParseError ¶
A ParseError is attached to an event in case of a parsing error.
It contains additional information about where the error occurred in the input stream.
func (ParseError) Error ¶
func (p ParseError) Error() string
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
A Parser reads and parses JSON documents from an input stream.
func (*Parser) Parse ¶
Parse starts parsing data from the input stream and emit events.
This method parses data until the input stream is empty.
Example (Multi) ¶
package main
import (
"fmt"
"strings"
"github.com/vrischmann/bari"
)
func main() {
const data = `{"foo": "bar"}{"bar": true}`
parser := bari.NewParser(strings.NewReader(data))
ch := make(chan bari.Event)
go func() {
parser.Parse(ch)
close(ch)
}()
for ev := range ch {
fmt.Println(ev.Type, ev.Value)
}
}
Output: ObjectStartEvent <nil> ObjectKeyEvent <nil> StringEvent foo ObjectValueEvent <nil> StringEvent bar ObjectEndEvent <nil> ObjectStartEvent <nil> ObjectKeyEvent <nil> StringEvent bar ObjectValueEvent <nil> BooleanEvent true ObjectEndEvent <nil>
Example (Single) ¶
package main
import (
"fmt"
"strings"
"github.com/vrischmann/bari"
)
func main() {
const data = `{"foo": "bar"}`
parser := bari.NewParser(strings.NewReader(data))
ch := make(chan bari.Event)
go func() {
parser.Parse(ch)
close(ch)
}()
for ev := range ch {
fmt.Println(ev.Type, ev.Value)
}
}
Output: ObjectStartEvent <nil> ObjectKeyEvent <nil> StringEvent foo ObjectValueEvent <nil> StringEvent bar ObjectEndEvent <nil>
Click to show internal directories.
Click to hide internal directories.