![]() |
simdjson
0.3.1
Ridiculously Fast JSON
|
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... | |
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.
Definition at line 620 of file document.h.
|
noexcept |
Create a JSON parser.
The new parser will have zero capacity.
| max_capacity | The 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.
|
default |
Take another parser's buffers and state.
| other | The parser to take. Its capacity is zeroed. |
|
inlinenoexcept |
Ensure this parser has enough memory to process JSON documents up to capacity bytes in length and max_depth depth.
| capacity | The new capacity. |
| max_depth | The new max_depth. Defaults to DEFAULT_MAX_DEPTH. |
Definition at line 438 of file document.h.
|
noexcept |
The largest document this parser can support without reallocating.
Definition at line 427 of file document.h.
|
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");
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.
If the parser's current capacity is less than the file length, it will allocate enough capacity to handle it (up to max_capacity).
| path | The path to load. |
Definition at line 369 of file document.h.
|
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; }
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.
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; }
When compiled with SIMDJSON_THREADS_ENABLED, this method will use a single thread under the hood to do some lookahead.
If the parser's current capacity is less than batch_size, it will allocate enough capacity to handle it (up to max_capacity).
| s | The concatenated JSON to parse. Must have at least len + SIMDJSON_PADDING allocated bytes. |
| batch_size | The 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. |
Definition at line 376 of file document.h.
|
noexcept |
The largest document this parser can automatically support.
The parser may reallocate internal buffers as needed up to this amount.
Definition at line 430 of file document.h.
|
noexcept |
The maximum level of nested object and arrays supported by this parser.
Definition at line 433 of file document.h.
Take another parser's buffers and state.
| other | The parser to take. Its capacity is zeroed. |
|
inlinenoexcept |
Parse a JSON document and return a temporary reference to it.
dom::parser parser; element doc = parser.parse(buf, len);
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.
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.
If the parser's current capacity is less than len, it will allocate enough capacity to handle it (up to max_capacity).
| buf | The JSON to parse. Must have at least len + SIMDJSON_PADDING allocated bytes, unless realloc_if_needed is true. |
| len | The length of the JSON. |
| realloc_if_needed | Whether to reallocate and enlarge the JSON buffer to add padding. |
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.
|
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; }
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.
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; }
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.
When compiled with SIMDJSON_THREADS_ENABLED, this method will use a single thread under the hood to do some lookahead.
If the parser's current capacity is less than batch_size, it will allocate enough capacity to handle it (up to max_capacity).
| buf | The concatenated JSON to parse. Must have at least len + SIMDJSON_PADDING allocated bytes. |
| len | The length of the concatenated JSON. |
| batch_size | The 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. |
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.
|
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.
| max_capacity | The new maximum capacity, in bytes. |
Definition at line 516 of file document.h.
1.8.17