heron

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2023 License: MIT Imports: 5 Imported by: 1

README

Go Reference Build Status

heron

A JSON stream parser.

Heron provides a convenience wrapper when dealing with JSON streams. Given a stream, it will read from the stream and emit any valid JSON objects or arrays.

Examples

package main

import (
  "fmt"
  "os"

  "github.com/dcilke/heron"
)

func main() {
  h := heron.New(
    heron.WithJSON(func(a any) {
      fmt.Printf("JSON: %v\n", a)
    }),
  )

  h.Process(os.Stdin)
}

Documentation

Overview

package heron provides a streaming JSON parser that emits JSON objects and arrays as they are parsed. Non-JSON lines and errors are emitted separately.

Index

Constants

View Source
const (
	// The default buffer size used to accumulate non json bytes.
	DefaultBufSize = 512
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Heron

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

func New

func New(opts ...Option) *Heron

New creates a new Heron parser with the given options.

func (*Heron) BufSize

func (p *Heron) BufSize() int

BufSize returns the buffer size used to accumulate non json bytes.

func (*Heron) Flush

func (p *Heron) Flush()

Flush flushes the buffer.

func (*Heron) Process

func (p *Heron) Process(stream io.Reader)

Process reads the given stream and emits values.

type Option

type Option func(*Heron)

func WithBufSize

func WithBufSize(size int) Option

WithBufSize sets the buffer size used to accumulate non json bytes.

func WithBytes

func WithBytes(f func([]byte)) Option

WithBytes sets the function to call when a non JSON line is parsed. This function is called when either a newline is encountered or the buffer is full. Setting to 0 disables internal buffering and emitting of non JSON bytes.

func WithError

func WithError(f func(error)) Option

WithError sets the function to call when an error is encountered when processing the stream.

func WithJSON

func WithJSON(f func(any)) Option

WithJSON sets the function to call when a JSON object or array is parsed.

Jump to

Keyboard shortcuts

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