jseq

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2025 License: MIT Imports: 8 Imported by: 0

README

Jseq - streaming JSON parser

Go Reference Tests

This is jseq, a streaming JSON parser.

It relies on the encoding/json/jsontext package in the Go standard library, which in Go 1.25 (the latest version as of this writing) is still experimental. To enable it, you must build with the environment variable GOEXPERIMENT set to jsonv2. This package is expected to become a fully fledged part of the stdlib in Go 1.26, at which point setting GOEXPERIMENT will not be necessary. For more details, please see A new experimental Go API for JSON.

The main function in this package, Values, produces JSON values from its input as soon as they are encountered. This means, for example, that it will produce the members of an array one by one first, followed by the complete array. For more information and a working example, see the Go doc for this package.

Documentation

Overview

Package jseq supplies streaming parsers for JSON tokens and values.

This package relies on encoding/json/jsontext, which is new and experimental in Go 1.25 and expected to become standard in Go 1.26. To use this package with Go 1.25 you must set GOEXPERIMENT=jsonv2. For more on this, see https://go.dev/blog/jsonv2-exp#experimenting-with-jsonv2

Example
package main

import (
	"errors"
	"fmt"
	"strings"

	"github.com/bobg/jseq"
)

func main() {
	r := strings.NewReader(`{"hello": [1, 2]} {"world": [3, 4]}`)
	tokens, errptr1 := jseq.Tokens(r)
	values, errptr2 := jseq.Values(tokens)
	for pointer, value := range values {
		fmt.Printf("%q: %v\n", pointer.Text(), value)
	}
	if err := errors.Join(*errptr1, *errptr2); err != nil {
		panic(err)
	}
}
Output:


"/hello/0": 1
"/hello/1": 2
"/hello": [1 2]
"": map[hello:[1 2]]
"/world/0": 3
"/world/1": 4
"/world": [3 4]
"": map[world:[3 4]]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tokens

func Tokens(r io.Reader, opts ...jsontext.Options) (iter.Seq[jsontext.Token], *error)

Tokens parses JSON tokens from r and returns them as an iter.Seq. This sequence is suitable as input to Values.

After consuming the resulting sequence, the caller may check for errors by dereferencing the returned error pointer.

func Values

func Values(tokens iter.Seq[jsontext.Token]) (iter.Seq2[Pointer, any], *error)

Values consumes a sequence of JSON tokens and produces a sequence of JSON values, each paired with the Pointer that can locate it within its top-level object.

The input to this function may be supplied by a call to Tokens.

Values are produced as they are encountered, in depth-first fashion, making this a "streaming" or "event-based" parser. For example, given a sequence of tokens representing this input:

{"hello": [1, 2], "world": [3, 4]}

Values will produce pointer/value pairs in this order:

"/hello/0"  1
"/hello/1"  2
"/hello"    [1, 2]
"/world/0"  3
"/world/1"  4
"/world"    [3, 4]
""          {"hello": [1, 2], "world": [3, 4]}

Note that object keys are not considered values to be separately emitted.

Value types in the resulting sequence are:

  • []any for arrays
  • map[string]any for objects
  • strings for strings
  • boolean for booleans
  • Null for null
  • Number for numbers

The input may contain multiple top-level JSON values, each of which will be paired with the empty pointer "". If the input ends in the middle of a JSON value, Values produces an io.ErrUnexpectedEOF error.

After consuming the resulting sequence, the caller may check for errors by dereferencing the returned error pointer.

Types

type Null

type Null struct{}

Null is the type of a JSON "null" value.

type Number

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

Number is the type of a JSON number.

func Float

func Float(n float64) Number

Float produces a new Number from a float64 value.

func Int

func Int(n int64) Number

Int produces a new Number from an int64 value.

func NewNumber

func NewNumber(tok jsontext.Token) Number

NewNumber produces a new Number from a jsontext.Token. The input must have jsontext.Kind '0' ("number").

func Uint

func Uint(n uint64) Number

Uint produces a new Number from a uint64 value.

func (Number) Float

func (n Number) Float() float64

Float returns the number’s float64 value.

func (Number) Int

func (n Number) Int() (int64, bool)

Int returns the number’s int64 value, if possible. The boolean result indicates whether n can accurately be represented as an int64.

func (Number) String

func (n Number) String() string

String returns the number’s raw JSON representation.

func (Number) Uint

func (n Number) Uint() (uint64, bool)

Uint returns the number’s uint64 value, if possible. The boolean result indicates whether n can accurately be represented as a uint64.

type Pointer

type Pointer []any

Pointer is the type of a JSON pointer produced by Values. It can be converted to a jsontext.Pointer via its Text method. Object keys are represented as strings, and array indexes are represented as ints. This allows the caller to distinguish between an array member at position X and an object member with key X, which jsontext.Pointer cannot do.

func (Pointer) Locate

func (p Pointer) Locate(val any) (any, error)

Locate locates the element within val represented by p.

func (Pointer) Text

func (p Pointer) Text() jsontext.Pointer

Text converts p to a jsontext.Pointer.

Jump to

Keyboard shortcuts

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