ndjson

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ndjson provides shared NDJSON (newline-delimited JSON) framing utilities for both daemon IPC and adapter serve-mode communication.

Both the CLI-daemon Unix socket IPC and the daemon-adapter stdin/stdout IPC use NDJSON. This package ensures consistent buffer sizes (1MB max line), explicit Err() checking, and compact encoding across all IPC layers.

Index

Constants

View Source
const MaxLineBytes = 1 << 20 // 1MB

MaxLineBytes is the maximum size of a single NDJSON line (1MB). This matches the existing daemon IPC limit and prevents unbounded memory allocation from malformed or malicious input.

Variables

This section is empty.

Functions

func Decode

func Decode(data []byte, v any) error

Decode unmarshals a single NDJSON line (as bytes) into v. This is a convenience for decoding Scanner output.

Types

type Encoder

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

Encoder wraps json.Encoder with compact encoding enforced (no indentation). Each Encode call writes exactly one JSON object followed by a newline.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates an Encoder that writes compact NDJSON to w.

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode serializes v as a single compact JSON line followed by a newline.

type Scanner

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

Scanner wraps bufio.Scanner with the correct buffer size for NDJSON lines. Always check Err() after Scan() returns false to distinguish EOF from errors.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner creates a Scanner that reads NDJSON lines from r with a 1MB buffer.

func (*Scanner) Bytes

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

Bytes returns the current line as a byte slice. The slice is only valid until the next call to Scan.

func (*Scanner) Err

func (sc *Scanner) Err() error

Err returns the first non-EOF error encountered by the Scanner.

func (*Scanner) Scan

func (sc *Scanner) Scan() bool

Scan advances to the next NDJSON line. Returns false at EOF or on error.

func (*Scanner) Text

func (sc *Scanner) Text() string

Text returns the current line as a string.

Jump to

Keyboard shortcuts

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