jsonl

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package jsonl frames newline-delimited JSON, the transport of the Arvel Provider Protocol (PROTOCOL §2).

Scanner reads lines of up to 64 MiB and keeps working after a line that is too long; Writer writes one JSON value per line, safe for concurrent use.

A typical read loop:

sc := jsonl.NewScanner(r, 0)
for sc.Scan() {
	if errors.Is(sc.Err(), jsonl.ErrLineTooLong) {
		// reply with a JSON-RPC error and carry on
		continue
	}
	handle(sc.Bytes())
}
if err := sc.Err(); err != nil {
	// the connection broke
}

Index

Constants

View Source
const DefaultMaxLine = 64 << 20

DefaultMaxLine is the longest line a Scanner accepts when max <= 0 (PROTOCOL §2: both sides accept lines up to 64 MiB).

Variables

View Source
var ErrLineTooLong = errors.New("jsonl: line too long")

ErrLineTooLong is reported for a line longer than the scanner limit.

Functions

This section is empty.

Types

type Scanner

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

Scanner reads newline-delimited lines from a reader. Unlike bufio.Scanner it survives a line that exceeds the limit: the line is discarded, reported once as ErrLineTooLong, and scanning continues with the next line.

Scan blocks in Read and cannot take a context; close the reader to unblock it.

func NewScanner

func NewScanner(r io.Reader, max int) *Scanner

NewScanner returns a Scanner reading from r. Lines longer than max bytes are dropped; max <= 0 selects DefaultMaxLine. The limit counts the bytes before the '\n', including a trailing '\r'.

func (*Scanner) Bytes

func (s *Scanner) Bytes() []byte

Bytes returns the current line without its '\n' and one trailing '\r'. The slice is valid until the next call to Scan. It is nil for a dropped line.

func (*Scanner) Discarded

func (s *Scanner) Discarded() int

Discarded returns the length in bytes (without the '\n') of the line that was dropped as too long, or 0 when the current line was kept.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the error of the current token after Scan returned true: ErrLineTooLong if the line was dropped, otherwise nil. After Scan returned false it returns the terminal read error, which is nil at io.EOF.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan advances to the next line. It returns true when a line, or a dropped over-long line, is available, and false at the end of input or on a read error. Empty lines are returned as empty tokens; empty input yields no token.

func (*Scanner) Text

func (s *Scanner) Text() string

Text returns the current line as a string.

type Writer

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

Writer writes one JSON value per line. It is safe for concurrent use: every call performs a single Write on the underlying writer, so lines never interleave.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a Writer on w. If w has a Flush() error method (for example *bufio.Writer) it is called after every line.

func (*Writer) WriteJSON

func (w *Writer) WriteJSON(v any) error

WriteJSON marshals v and writes it followed by '\n'. HTML characters are not escaped. A json.RawMessage is compacted, so the output never contains an embedded newline. A message longer than DefaultMaxLine is refused with ErrLineTooLong because the peer would drop it (PROTOCOL §2).

Jump to

Keyboard shortcuts

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