simdjson  0.3.1
Ridiculously Fast JSON
Classes | Public Member Functions | List of all members
simdjson::dom::parser Class Reference

A persistent document parser. More...

Classes

class  Iterator
 

Public Member Functions

really_inline parser (size_t max_capacity=SIMDJSON_MAXSIZE_BYTES) noexcept
 Create a JSON parser. More...
 
 parser (parser &&other)=default
 Take another parser's buffers and state. More...
 
parser & operator= (parser &&other)=default
 Take another parser's buffers and state. More...
 
 ~parser ()=default
 Deallocate the JSON parser.
 
simdjson_result< element > load (const std::string &path) noexcept
 Load a JSON document from a file and return a reference to it. More...
 
simdjson_result< element > parse (const uint8_t *buf, size_t len, bool realloc_if_needed=true) noexcept
 Parse a JSON document and return a temporary reference to it. More...
 
really_inline simdjson_result< element > parse (const char *buf, size_t len, bool realloc_if_needed=true) noexcept
 
really_inline simdjson_result< element > parse (const std::string &s) noexcept
 
really_inline simdjson_result< element > parse (const padded_string &s) noexcept
 
document_stream load_many (const std::string &path, size_t batch_size=DEFAULT_BATCH_SIZE) noexcept
 Load a file containing many JSON documents. More...
 
document_stream parse_many (const uint8_t *buf, size_t len, size_t batch_size=DEFAULT_BATCH_SIZE) noexcept
 Parse a buffer containing many JSON documents. More...
 
document_stream parse_many (const char *buf, size_t len, size_t batch_size=DEFAULT_BATCH_SIZE) noexcept
 
document_stream parse_many (const std::string &s, size_t batch_size=DEFAULT_BATCH_SIZE) noexcept
 
document_stream parse_many (const padded_string &s, size_t batch_size=DEFAULT_BATCH_SIZE) noexcept
 
WARN_UNUSED error_code allocate (size_t capacity, size_t max_depth=DEFAULT_MAX_DEPTH) noexcept
 Ensure this parser has enough memory to process JSON documents up to capacity bytes in length and max_depth depth. More...
 
really_inline size_t capacity () const noexcept
 The largest document this parser can support without reallocating. More...
 
really_inline size_t max_capacity () const noexcept
 The largest document this parser can automatically support. More...
 
really_inline size_t max_depth () const noexcept
 The maximum level of nested object and arrays supported by this parser. More...
 
really_inline void set_max_capacity (size_t max_capacity) noexcept
 Set max_capacity. More...
 

Detailed Description

A persistent document parser.

The parser is designed to be reused, holding the internal buffers necessary to do parsing, as well as memory for a single document. The parsed document is overwritten on each parse.

This class cannot be copied, only moved, to avoid unintended allocations.

Note
This is not thread safe: one parser cannot produce two documents at the same time!

Definition at line 620 of file document.h.

Constructor & Destructor Documentation

◆ parser() [1/2]

really_inline simdjson::dom::parser::parser ( size_t  max_capacity = SIMDJSON_MAXSIZE_BYTES)
noexcept

Create a JSON parser.

The new parser will have zero capacity.

Parameters
max_capacityThe maximum document length the parser can automatically handle. The parser will allocate more capacity on an as needed basis (when it sees documents too big to handle) up to this amount. The parser still starts with zero capacity no matter what this number is: to allocate an initial capacity, call allocate() after constructing the parser. Defaults to SIMDJSON_MAXSIZE_BYTES (the largest single document simdjson can process).

Definition at line 317 of file document.h.

◆ parser() [2/2]

simdjson::dom::parser::parser ( parser &&  other)
default

Take another parser's buffers and state.

Parameters
otherThe parser to take. Its capacity is zeroed.

Member Function Documentation

◆ allocate()

WARN_UNUSED error_code simdjson::dom::parser::allocate ( size_t  capacity,
size_t  max_depth = DEFAULT_MAX_DEPTH 
)
inlinenoexcept

Ensure this parser has enough memory to process JSON documents up to capacity bytes in length and max_depth depth.

Parameters
capacityThe new capacity.
max_depthThe new max_depth. Defaults to DEFAULT_MAX_DEPTH.
Returns
The error, if there is one.

Definition at line 438 of file document.h.

◆ capacity()

really_inline size_t simdjson::dom::parser::capacity ( ) const
noexcept

The largest document this parser can support without reallocating.

Returns
Current capacity, in bytes.

Definition at line 427 of file document.h.

◆ load()

simdjson_result< element > simdjson::dom::parser::load ( const std::string &  path)
inlinenoexcept

Load a JSON document from a file and return a reference to it.

dom::parser parser; const element doc = parser.load("jsonexamples/twitter.json");

IMPORTANT: Document Lifetime

The JSON document still lives in the parser: this is the most efficient way to parse JSON documents because it reuses the same buffers, but you must use the document before you destroy the parser or call parse() again.

Parser Capacity

If the parser's current capacity is less than the file length, it will allocate enough capacity to handle it (up to max_capacity).

Parameters
pathThe path to load.
Returns
The document, or an error:
  • IO_ERROR if there was an error opening or reading the file.
  • MEMALLOC if the parser does not have enough capacity and memory allocation fails.
  • CAPACITY if the parser does not have enough capacity and len > max_capacity.
  • other json errors if parsing fails.

Definition at line 369 of file document.h.

◆ load_many()

document_stream simdjson::dom::parser::load_many ( const std::string &  path,
size_t  batch_size = DEFAULT_BATCH_SIZE 
)
inlinenoexcept

Load a file containing many JSON documents.

dom::parser parser; for (const element doc : parser.load_many(path)) { cout << std::string(doc["title"]) << endl; }

Format

The file must contain a series of one or more JSON documents, concatenated into a single buffer, separated by whitespace. It effectively parses until it has a fully valid document, then starts parsing the next document at that point. (It does this with more parallelism and lookahead than you might think, though.)

documents that consist of an object or array may omit the whitespace between them, concatenating with no separator. documents that consist of a single primitive (i.e. documents that are not arrays or objects) MUST be separated with whitespace.

Error Handling

All errors are returned during iteration: if there is a global error such as memory allocation, it will be yielded as the first result. Iteration always stops after the first error.

As with all other simdjson methods, non-exception error handling is readily available through the same interface, requiring you to check the error before using the document:

dom::parser parser; for (auto [doc, error] : parser.load_many(path)) { if (error) { cerr << error << endl; exit(1); } cout << std::string(doc["title"]) << endl; }

Threads

When compiled with SIMDJSON_THREADS_ENABLED, this method will use a single thread under the hood to do some lookahead.

Parser Capacity

If the parser's current capacity is less than batch_size, it will allocate enough capacity to handle it (up to max_capacity).

Parameters
sThe concatenated JSON to parse. Must have at least len + SIMDJSON_PADDING allocated bytes.
batch_sizeThe batch size to use. MUST be larger than the largest document. The sweet spot is cache-related: small enough to fit in cache, yet big enough to parse as many documents as possible in one tight loop. Defaults to 10MB, which has been a reasonable sweet spot in our tests.
Returns
The stream. If there is an error, it will be returned during iteration. An empty input will yield 0 documents rather than an EMPTY error. Errors:
  • IO_ERROR if there was an error opening or reading the file.
  • MEMALLOC if the parser does not have enough capacity and memory allocation fails.
  • CAPACITY if the parser does not have enough capacity and batch_size > max_capacity.
  • other json errors if parsing fails.

Definition at line 376 of file document.h.

◆ max_capacity()

really_inline size_t simdjson::dom::parser::max_capacity ( ) const
noexcept

The largest document this parser can automatically support.

The parser may reallocate internal buffers as needed up to this amount.

Returns
Maximum capacity, in bytes.

Definition at line 430 of file document.h.

◆ max_depth()

really_inline size_t simdjson::dom::parser::max_depth ( ) const
noexcept

The maximum level of nested object and arrays supported by this parser.

Returns
Maximum depth, in bytes.

Definition at line 433 of file document.h.

◆ operator=()

parser& simdjson::dom::parser::operator= ( parser &&  other)
default

Take another parser's buffers and state.

Parameters
otherThe parser to take. Its capacity is zeroed.

◆ parse()

simdjson_result< element > simdjson::dom::parser::parse ( const uint8_t *  buf,
size_t  len,
bool  realloc_if_needed = true 
)
inlinenoexcept

Parse a JSON document and return a temporary reference to it.

dom::parser parser; element doc = parser.parse(buf, len);

IMPORTANT: Document Lifetime

The JSON document still lives in the parser: this is the most efficient way to parse JSON documents because it reuses the same buffers, but you must use the document before you destroy the parser or call parse() again.

REQUIRED: Buffer Padding

The buffer must have at least SIMDJSON_PADDING extra allocated bytes. It does not matter what those bytes are initialized to, as long as they are allocated.

If realloc_if_needed is true, it is assumed that the buffer does not have enough padding, and it is copied into an enlarged temporary buffer before parsing.

Parser Capacity

If the parser's current capacity is less than len, it will allocate enough capacity to handle it (up to max_capacity).

Parameters
bufThe JSON to parse. Must have at least len + SIMDJSON_PADDING allocated bytes, unless realloc_if_needed is true.
lenThe length of the JSON.
realloc_if_neededWhether to reallocate and enlarge the JSON buffer to add padding.
Returns
The document, or an error:
  • MEMALLOC if realloc_if_needed is true or the parser does not have enough capacity, and memory allocation fails.
  • CAPACITY if the parser does not have enough capacity and len > max_capacity.
  • other json errors if parsing fails.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 381 of file document.h.

◆ parse_many()

document_stream simdjson::dom::parser::parse_many ( const uint8_t *  buf,
size_t  len,
size_t  batch_size = DEFAULT_BATCH_SIZE 
)
inlinenoexcept

Parse a buffer containing many JSON documents.

dom::parser parser; for (const element doc : parser.parse_many(buf, len)) { cout << std::string(doc["title"]) << endl; }

Format

The buffer must contain a series of one or more JSON documents, concatenated into a single buffer, separated by whitespace. It effectively parses until it has a fully valid document, then starts parsing the next document at that point. (It does this with more parallelism and lookahead than you might think, though.)

documents that consist of an object or array may omit the whitespace between them, concatenating with no separator. documents that consist of a single primitive (i.e. documents that are not arrays or objects) MUST be separated with whitespace.

Error Handling

All errors are returned during iteration: if there is a global error such as memory allocation, it will be yielded as the first result. Iteration always stops after the first error.

As with all other simdjson methods, non-exception error handling is readily available through the same interface, requiring you to check the error before using the document:

dom::parser parser; for (auto [doc, error] : parser.parse_many(buf, len)) { if (error) { cerr << error << endl; exit(1); } cout << std::string(doc["title"]) << endl; }

REQUIRED: Buffer Padding

The buffer must have at least SIMDJSON_PADDING extra allocated bytes. It does not matter what those bytes are initialized to, as long as they are allocated.

Threads

When compiled with SIMDJSON_THREADS_ENABLED, this method will use a single thread under the hood to do some lookahead.

Parser Capacity

If the parser's current capacity is less than batch_size, it will allocate enough capacity to handle it (up to max_capacity).

Parameters
bufThe concatenated JSON to parse. Must have at least len + SIMDJSON_PADDING allocated bytes.
lenThe length of the concatenated JSON.
batch_sizeThe batch size to use. MUST be larger than the largest document. The sweet spot is cache-related: small enough to fit in cache, yet big enough to parse as many documents as possible in one tight loop. Defaults to 10MB, which has been a reasonable sweet spot in our tests.
Returns
The stream. If there is an error, it will be returned during iteration. An empty input will yield 0 documents rather than an EMPTY error. Errors:
  • MEMALLOC if the parser does not have enough capacity and memory allocation fails
  • CAPACITY if the parser does not have enough capacity and batch_size > max_capacity.
  • other json errors if parsing fails.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 414 of file document.h.

◆ set_max_capacity()

really_inline void simdjson::dom::parser::set_max_capacity ( size_t  max_capacity)
noexcept

Set max_capacity.

This is the largest document this parser can automatically support.

The parser may reallocate internal buffers as needed up to this amount.

This call will not allocate or deallocate, even if capacity is currently above max_capacity.

Parameters
max_capacityThe new maximum capacity, in bytes.

Definition at line 516 of file document.h.


The documentation for this class was generated from the following file: