jsonio

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package jsonio owns streamio's JSON output path: encoding canonical record.Records as JSON documents, and decoding NDJSON back into them.

It writes JSON tokens straight to a jsontext.Encoder rather than marshalling a map, since reflection over a per-row map is the dominant cost this path avoids.

JSON value-rendering helpers, kept beside the encoder so every JSON-producing path renders a given value the same way.

Index

Constants

View Source
const (
	Bits32 = 32
	Bits64 = 64
)

Bits32 and Bits64 select the precision WriteFloat and AppendFloat format at.

Variables

View Source
var (
	// ErrNotObject reports a line whose top-level value isn't a JSON object.
	ErrNotObject = errors.New("jsonio: document is not a JSON object")

	// ErrUnexpectedToken reports a token that cannot appear where it did, which means the document
	// isn't well-formed for this decoder's object contract.
	ErrUnexpectedToken = errors.New("jsonio: unexpected token")
)

Errors a JSON document can fail to decode with. They are deliberately specific about what this decoder accepts, because the honest answer is "an object, arbitrarily nested" rather than "maybe malformed".

View Source
var ErrNonFinite = errors.New("json: unsupported value: non-finite float")

ErrNonFinite is returned for NaN and ±Inf, which have no JSON number representation.

Functions

func AppendFloat

func AppendFloat(dst []byte, f float64, bitSize int) []byte

AppendFloat formats f exactly as encoding/json's floatEncoder does and appends it to dst.

func Date

func Date(days int64) string

Date renders a day count since the Unix epoch as an ISO-8601 calendar date.

func NewDecoder

func NewDecoder(cfg options.Config, src options.Source) (formatio.RecordDecoder, error)

NewDecoder opens src as a stream of canonical records. Lines are parsed as JSON tokens rather than through reflection, so a scalar field costs no boxing and no map insert.

NewDecoder does not take ownership of src.Reader; the caller closes it, if it needs closing, once done with every decoder Split hands out.

func NewEncoder

func NewEncoder(_ options.Config) (formatio.RecordEncoder, error)

NewEncoder returns a JSON encoder for canonical records. cfg is unused; nothing about JSON output is configurable.

func NewRawSource

func NewRawSource(cfg options.Config, src options.Source) (formatio.RawSource, error)

NewRawSource opens src for raw passthrough: each dispatched document is one line, handed to the sink as-is. NewRawSource does not take ownership of src.Reader; the caller closes it, if it needs closing, once done with the returned source.

func TimestampMicros

func TimestampMicros(v int64) string

TimestampMicros renders an epoch timestamp in microseconds as an ISO-8601 instant.

func TimestampMillis

func TimestampMillis(v int64) string

TimestampMillis renders an epoch timestamp in milliseconds as an ISO-8601 instant.

func TimestampNanos

func TimestampNanos(v int64) string

TimestampNanos renders an epoch timestamp in nanoseconds as an ISO-8601 instant.

func WriteFloat

func WriteFloat(enc *jsontext.Encoder, f float64, bitSize int) error

WriteFloat writes f to enc as a raw JSON number formatted at bitSize precision.

Types

type ObjectDecoder

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

ObjectDecoder decodes one flat JSON object document at a time into canonical records, reusable across many lines without per-line allocation.

func NewObjectDecoder

func NewObjectDecoder() *ObjectDecoder

NewObjectDecoder returns a reusable flat-object JSON decoder.

func (*ObjectDecoder) Decode

func (d *ObjectDecoder) Decode(rec record.Record, doc []byte) (record.Record, error)

Decode refills rec from one JSON object document, preserving the document's field order. A nested object or array decodes recursively into record.KindMap/record.KindList rather than being rejected, to whatever depth the document actually has.

Jump to

Keyboard shortcuts

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