1#ifndef SIMDJSON_GENERIC_ONDEMAND_PARSER_INL_H
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_PARSER_INL_H
5#include "simdjson/padded_string.h"
6#include "simdjson/padded_string_view.h"
7#include "simdjson/implementation.h"
8#include "simdjson/internal/dom_parser_implementation.h"
9#include "simdjson/dom/base.h"
10#include "simdjson/generic/ondemand/base.h"
11#include "simdjson/generic/ondemand/document_stream.h"
12#include "simdjson/generic/ondemand/parser.h"
13#include "simdjson/generic/ondemand/raw_json_string.h"
17namespace SIMDJSON_IMPLEMENTATION {
21 : _max_capacity{max_capacity} {
25 if (new_capacity > max_capacity()) {
return CAPACITY; }
26 if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) {
return SUCCESS; }
33 size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) +
SIMDJSON_PADDING, 64);
34 string_buf.reset(
new (std::nothrow) uint8_t[string_capacity]);
35#if SIMDJSON_DEVELOPMENT_CHECKS
36 start_positions.reset(
new (std::nothrow) token_position[new_max_depth]);
44 _capacity = new_capacity;
45 _max_depth = new_max_depth;
48#if SIMDJSON_DEVELOPMENT_CHECKS
49simdjson_inline simdjson_warn_unused
bool parser::string_buffer_overflow(
const uint8_t *string_buf_loc)
const noexcept {
50 return (string_buf_loc < string_buf.get()) || (size_t(string_buf_loc - string_buf.get()) >= capacity());
57 json.remove_utf8_bom();
58 _document_len = json.length();
61 if (capacity() < json.length() || !string_buf) {
62 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
66 SIMDJSON_TRY(
implementation->stage1(
reinterpret_cast<const uint8_t *
>(json.data()), json.length(), stage1_mode::regular) );
67 return document::start({
reinterpret_cast<const uint8_t *
>(json.data()),
this });
70#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
74 json.remove_utf8_bom();
75 _document_len = json.length();
78 if (capacity() < json.length() || !string_buf) {
79 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
88 return document::start({
reinterpret_cast<const uint8_t *
>(json.data()),
this,
true });
92simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const char *json,
size_t len,
size_t allocated) &
noexcept {
93 return iterate(padded_string_view(json, len, allocated));
96simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const uint8_t *json,
size_t len,
size_t allocated) &
noexcept {
97 return iterate(padded_string_view(json, len, allocated));
100simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(std::string_view json,
size_t allocated) &
noexcept {
101 return iterate(padded_string_view(json, allocated));
104simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(std::string &json) &
noexcept {
108simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const std::string &json) &
noexcept {
109 return iterate(padded_string_view(json));
112simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const simdjson_result<padded_string_view> &result) &
noexcept {
114 SIMDJSON_TRY( result.error() );
115 padded_string_view json = result.value_unsafe();
116 return iterate(json);
119simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const simdjson_result<padded_string> &result) &
noexcept {
121 SIMDJSON_TRY( result.error() );
122 const padded_string &json = result.value_unsafe();
123 return iterate(json);
126simdjson_warn_unused simdjson_inline simdjson_result<json_iterator> parser::iterate_raw(padded_string_view json) &
noexcept {
129 json.remove_utf8_bom();
132 if (capacity() < json.length()) {
133 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
137 SIMDJSON_TRY( implementation->stage1(
reinterpret_cast<const uint8_t *
>(json.data()), json.length(), stage1_mode::regular) );
138 return json_iterator(
reinterpret_cast<const uint8_t *
>(json.data()),
this);
142 if(batch_size < MINIMAL_BATCH_SIZE) { batch_size = MINIMAL_BATCH_SIZE; }
143 if((len >= 3) && (std::memcmp(buf,
"\xEF\xBB\xBF", 3) == 0)) {
150 return iterate_many(
reinterpret_cast<const uint8_t *
>(buf), len, batch_size);
154 return iterate_many(s.data(), s.length(), batch_size);
156inline simdjson_result<document_stream>
parser::iterate_many(
const padded_string &s,
size_t batch_size)
noexcept {
157 return iterate_many(padded_string_view(s), batch_size);
159inline simdjson_result<document_stream>
parser::iterate_many(
const std::string &s,
size_t batch_size)
noexcept {
160 return iterate_many(padded_string_view(s), batch_size);
162inline simdjson_result<document_stream>
parser::iterate_many(std::string &s,
size_t batch_size)
noexcept {
163 return iterate_many(
pad(s), batch_size);
166#ifndef SIMDJSON_DISABLE_DEPRECATED_API
167SIMDJSON_PUSH_DISABLE_WARNINGS
168SIMDJSON_DISABLE_DEPRECATED_WARNING
171 if(batch_size < MINIMAL_BATCH_SIZE) { batch_size = MINIMAL_BATCH_SIZE; }
172 if((len >= 3) && (std::memcmp(buf,
"\xEF\xBB\xBF", 3) == 0)) {
177 if (allow_comma_separated) {
185 return iterate_many(
reinterpret_cast<const uint8_t *
>(buf), len, batch_size, allow_comma_separated);
189 return iterate_many(s.data(), s.length(), batch_size, allow_comma_separated);
191inline simdjson_result<document_stream>
parser::iterate_many(
const padded_string &s,
size_t batch_size,
bool allow_comma_separated)
noexcept {
192 return iterate_many(padded_string_view(s), batch_size, allow_comma_separated);
194inline simdjson_result<document_stream>
parser::iterate_many(
const std::string &s,
size_t batch_size,
bool allow_comma_separated)
noexcept {
195 return iterate_many(padded_string_view(s), batch_size, allow_comma_separated);
197inline simdjson_result<document_stream>
parser::iterate_many(std::string &s,
size_t batch_size,
bool allow_comma_separated)
noexcept {
198 return iterate_many(
pad(s), batch_size, allow_comma_separated);
200SIMDJSON_POP_DISABLE_WARNINGS
204 if(batch_size < MINIMAL_BATCH_SIZE) { batch_size = MINIMAL_BATCH_SIZE; }
205 if((len >= 3) && (std::memcmp(buf,
"\xEF\xBB\xBF", 3) == 0)) {
211 while (len > 0 && (buf[0] ==
' ' || buf[0] ==
'\t' || buf[0] ==
'\n' || buf[0] ==
'\r')) {
215 if (len == 0 || buf[0] !=
'[') {
return TAPE_ERROR; }
218 while (len > 0 && (buf[len-1] ==
' ' || buf[len-1] ==
'\t' || buf[len-1] ==
'\n' || buf[len-1] ==
'\r')) {
222 if (len == 0 || buf[len-1] !=
']') {
return TAPE_ERROR; }
230 return iterate_many(
reinterpret_cast<const uint8_t *
>(buf), len, batch_size, format);
234 return iterate_many(s.data(), s.length(), batch_size, format);
237 return iterate_many(padded_string_view(s), batch_size, format);
240 return iterate_many(
pad(s), batch_size, format);
243 return iterate_many(padded_string_view(s), batch_size, format);
249 return _max_capacity;
255simdjson_inline
void parser::set_max_capacity(
size_t max_capacity)
noexcept {
256 if(max_capacity > dom::MINIMAL_DOCUMENT_CAPACITY) {
257 _max_capacity = max_capacity;
259 _max_capacity = dom::MINIMAL_DOCUMENT_CAPACITY;
264 uint8_t *end =
implementation->parse_string(in.buf, dst, allow_replacement);
266 std::string_view result(
reinterpret_cast<const char *
>(dst), end-dst);
274 std::string_view result(
reinterpret_cast<const char *
>(dst), end-dst);
280 return *parser::get_parser_instance();
284 auto &parser_instance = parser::get_threadlocal_parser_if_exists();
285 if (parser_instance) {
286 parser_instance.reset();
292simdjson_inline simdjson_warn_unused std::unique_ptr<ondemand::parser>& parser::get_parser_instance() {
293 std::unique_ptr<ondemand::parser>& parser_instance = get_threadlocal_parser_if_exists();
294 if (!parser_instance) {
295 parser_instance.reset(
new ondemand::parser());
297 return parser_instance;
300simdjson_inline simdjson_warn_unused std::unique_ptr<ondemand::parser>& parser::get_threadlocal_parser_if_exists() {
302 thread_local std::unique_ptr<ondemand::parser> parser_instance =
nullptr;
303 return parser_instance;
313simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::parser &&value) noexcept
314 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::parser>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::parser>(value)) {}
315simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser>::simdjson_result(
error_code error) noexcept
316 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::parser>(error) {}
A forward-only stream of documents.
simdjson_warn_unused simdjson_result< document > iterate(padded_string_view json) &noexcept
Start iterating an on-demand JSON document.
simdjson_pure simdjson_inline size_t max_capacity() const noexcept
The maximum capacity of this parser (the largest document it is allowed to process).
simdjson_inline simdjson_result< std::string_view > unescape(raw_json_string in, uint8_t *&dst, bool allow_replacement=false) const noexcept
Unescape this JSON string, replacing \ with \, with newline, etc.
static simdjson_inline bool release_parser()
Release the parser instance initialized by get_parser() and all the associated resources (memory).
simdjson_pure simdjson_inline size_t max_depth() const noexcept
The maximum depth of this parser (the most deeply nested objects and arrays it can process).
simdjson_pure simdjson_inline size_t capacity() const noexcept
The capacity of this parser (the largest document it can process).
parser(size_t max_capacity=SIMDJSON_MAXSIZE_BYTES) noexcept
Create a JSON parser.
simdjson_inline simdjson_result< std::string_view > unescape_wobbly(raw_json_string in, uint8_t *&dst) const noexcept
Unescape this JSON string, replacing \ with \, with newline, etc.
simdjson_result< document_stream > iterate_many(const uint8_t *buf, size_t len, size_t batch_size=DEFAULT_BATCH_SIZE) noexcept
Parse a buffer containing many JSON documents.
simdjson_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...
static simdjson_inline simdjson_warn_unused ondemand::parser & get_parser()
Get a unique parser instance corresponding to the current thread.
A string escaped per JSON rules, terminated with quote (").
An implementation of simdjson for a particular CPU architecture.
User-provided string that promises it has extra padded bytes at the end for use with parser::parse().
The top level simdjson namespace, containing everything the library provides.
SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr< const implementation > & get_active_implementation()
The active implementation.
stream_format
Stream format for parse_many/iterate_many.
@ comma_delimited
Comma-separated JSON documents (e.g., {...},{...},{...})
@ whitespace_delimited
Whitespace-delimited JSON documents (default, includes NDJSON/JSONL)
@ comma_delimited_array
A single JSON array whose elements are iterated as comma-separated documents (e.g....
padded_string_view pad_with_reserve(std::string &s) noexcept
Create a padded_string_view from a string.
error_code
All possible errors returned by simdjson.
@ UNCLOSED_STRING
missing quote at the end
@ CAPACITY
This parser can't support a document that big.
@ TAPE_ERROR
Something went wrong, this is a generic error. Fatal/unrecoverable error.
@ STRING_ERROR
Problem while parsing a string.
@ INSUFFICIENT_PADDING
The JSON doesn't have enough padding for simdjson to safely parse it.
padded_string_view pad(std::string &s) noexcept
Create a padded_string_view from a string.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.
The result of a simdjson operation that could fail.