Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
JSONL

A JSON Lines (https://jsonlines.org) implementation with iterator support. Every non-empty line in a JSONL stream is a complete, valid JSON value, and lines are separated by newline characters (U+000A). Multi-line JSON values are not supported, as per the JSONL specification. Blank and whitespace-only lines are skipped rather than treated as errors. JSON Lines is a convention rather than a formal specification, so tolerating stray blank lines is friendlier to real-world input. More...

Classes

class  sourcemeta::core::JSONL
class  sourcemeta::core::ConstJSONLIterator

Detailed Description

A JSON Lines (https://jsonlines.org) implementation with iterator support. Every non-empty line in a JSONL stream is a complete, valid JSON value, and lines are separated by newline characters (U+000A). Multi-line JSON values are not supported, as per the JSONL specification. Blank and whitespace-only lines are skipped rather than treated as errors. JSON Lines is a convention rather than a formal specification, so tolerating stray blank lines is friendlier to real-world input.

This functionality is included as follows:

#include <sourcemeta/core/jsonl.h>

Class Documentation

◆ sourcemeta::core::JSONL

class sourcemeta::core::JSONL

A range over the JSON documents contained in a JSON Lines stream.

Public Types

enum class  Mode : std::uint8_t { Raw , GZIP }
 The mode of operation for the JSONL parser. More...

Public Member Functions

 JSONL (std::basic_istream< JSON::Char, JSON::CharTraits > &stream, Mode mode=Mode::Raw)
 JSONL (const JSONL &)=delete
 JSONL (JSONL &&)=delete

Member Enumeration Documentation

◆ Mode

enum class sourcemeta::core::JSONL::Mode : std::uint8_t
strong

The mode of operation for the JSONL parser.

Enumerator
Raw 

The input stream contains raw JSONL text.

GZIP 

The input stream contains gzip-compressed JSONL data.

Constructor & Destructor Documentation

◆ JSONL()

sourcemeta::core::JSONL::JSONL ( std::basic_istream< JSON::Char, JSON::CharTraits > & stream,
Mode mode = Mode::Raw )

Parse a JSONL document from a C++ standard input stream using a standard read-only C++ forward iterator interface. An optional mode parameter controls whether the input is treated as raw text or gzip-compressed data. For example, you can parse a JSONL document and prettify each of its rows as follows:

#include <sourcemeta/core/jsonl.h>
#include <cassert>
#include <sstream>
#include <iostream>
std::istringstream stream{
"{ \"foo\": 1 }\n{ \"bar\": 2 }\n{ \"baz\": 3 }"};
for (const auto &document : sourcemeta::core::JSONL{stream}) {
assert(document.is_object());
sourcemeta::core::prettify(document, std::cout);
std::cout << '\n';
}
SOURCEMETA_CORE_JSON_EXPORT auto prettify(const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const std::size_t spaces=2) -> void
Definition jsonl.h:37

If parsing fails, sourcemeta::core::JSONParseError will be thrown.

◆ sourcemeta::core::ConstJSONLIterator

class sourcemeta::core::ConstJSONLIterator

A forward iterator to parse JSON documents out of a JSON Lines stream. Blank and whitespace-only lines are skipped rather than treated as errors, as JSON Lines is a convention rather than a formal specification and tolerating stray blank lines is friendlier to real-world input.

Public Member Functions

 ConstJSONLIterator (std::basic_istream< JSON::Char, JSON::CharTraits > *stream)
 Construct an iterator over the JSON documents in a stream.