A JSON Lines (https://jsonlines.org) and RFC 7464 JSON text sequence implementation with iterator support. Every non-empty line in a JSONL stream is a complete, valid JSON value of any type, and lines are separated by newline characters (U+000A), optionally preceded by a carriage return (U+000D). Multi-line JSON values are not supported, as per the JSONL specification.
JSON Lines and NDJSON (https://github.com/ndjson/ndjson-spec) describe the same format with minor differences, and this implementation accepts a superset of both:
- Blank and whitespace-only lines are skipped rather than treated as errors. JSON Lines considers them invalid, while NDJSON 3.2 permits ignoring them as long as the behavior is documented
- A newline after the last value is optional. JSON Lines makes it a recommendation, while NDJSON 3.1 requires it when serializing
- Whitespace is tolerated anywhere around a value, including carriage returns that NDJSON 3.1 only allows right before a newline
The same iterator reads RFC 7464 JSON text sequences, the application/json-seq media type, when given the corresponding framing. There, every value is introduced by a record separator (U+001E) rather than terminated by a newline, so a value may span multiple lines. RFC 7464 Section 2.4 requires dropping a top-level number, boolean or null that no whitespace follows, as it may have been truncated, and this implementation honors that. It deviates from the specification as follows:
- Section 2.3 states that a parser "should skip to the next RS" when an element is not a valid JSON text. This implementation throws instead, so that malformed input is never dropped without notice
- Section 2.1 permits ignoring the empty elements that consecutive record separators denote. This implementation also ignores elements that carry nothing but whitespace, matching how it treats blank lines
This functionality is included as follows:
#include <sourcemeta/core/jsonl.h>