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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 ¶
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 ¶
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).