bari

package module
v0.0.0-...-c81f8bb Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2015 License: MIT Imports: 9 Imported by: 0

README

bari

Build Status GoDoc

bari is a JSON parser which works by emitting events for each interesting part of a JSON document.

For example, you will receive an event at the start of an object, for each string value, etc.

Use case

It is intended to be used when you want to parse a big JSON file (think gigabytes plus) and you can't afford the memory needed to load this using the standard JSON parser.

Usage

The parser works by emitting events into a channel. You are responsible for providing the channel, since you might want to buffer it.

Example code:

	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)
	}

The NewParser function takes a io.Reader, so it can read from a file, a network connection, or whatever else.

License

bari is licensed under the MIT, see the LICENSE file.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Type  EventType
	Value interface{}
	Error error
}

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
)

func (EventType) String

func (i EventType) String() string

type ParseError

type ParseError struct {
	Message  string
	Line     int
	Position int
}

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 NewParser

func NewParser(r io.Reader) *Parser

NewParser creates a new parser that reads from r.

func (*Parser) Parse

func (p *Parser) Parse(ch chan Event)

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>

Jump to

Keyboard shortcuts

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