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; }
30 size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * new_capacity / 3 +
SIMDJSON_PADDING, 64);
31 string_buf.reset(
new (std::nothrow) uint8_t[string_capacity]);
32#if SIMDJSON_DEVELOPMENT_CHECKS
33 start_positions.reset(
new (std::nothrow) token_position[new_max_depth]);
41 _capacity = new_capacity;
42 _max_depth = new_max_depth;
45#if SIMDJSON_DEVELOPMENT_CHECKS
46simdjson_inline simdjson_warn_unused
bool parser::string_buffer_overflow(
const uint8_t *string_buf_loc)
const noexcept {
47 return (string_buf_loc < string_buf.get()) || (size_t(string_buf_loc - string_buf.get()) >= capacity());
54 json.remove_utf8_bom();
57 if (capacity() < json.length() || !string_buf) {
58 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
62 SIMDJSON_TRY(
implementation->stage1(
reinterpret_cast<const uint8_t *
>(json.data()), json.length(), stage1_mode::regular) );
63 return document::start({
reinterpret_cast<const uint8_t *
>(json.data()),
this });
66#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
70 json.remove_utf8_bom();
73 if (capacity() < json.length() || !string_buf) {
74 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
83 return document::start({
reinterpret_cast<const uint8_t *
>(json.data()),
this,
true });
87simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const char *json,
size_t len,
size_t allocated) &
noexcept {
88 return iterate(padded_string_view(json, len, allocated));
91simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const uint8_t *json,
size_t len,
size_t allocated) &
noexcept {
92 return iterate(padded_string_view(json, len, allocated));
95simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(std::string_view json,
size_t allocated) &
noexcept {
96 return iterate(padded_string_view(json, allocated));
99simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(std::string &json) &
noexcept {
103simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const std::string &json) &
noexcept {
104 return iterate(padded_string_view(json));
107simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const simdjson_result<padded_string_view> &result) &
noexcept {
109 SIMDJSON_TRY( result.error() );
110 padded_string_view json = result.value_unsafe();
111 return iterate(json);
114simdjson_warn_unused simdjson_inline simdjson_result<document>
parser::iterate(
const simdjson_result<padded_string> &result) &
noexcept {
116 SIMDJSON_TRY( result.error() );
117 const padded_string &json = result.value_unsafe();
118 return iterate(json);
121simdjson_warn_unused simdjson_inline simdjson_result<json_iterator> parser::iterate_raw(padded_string_view json) &
noexcept {
124 json.remove_utf8_bom();
127 if (capacity() < json.length()) {
128 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
132 SIMDJSON_TRY( implementation->stage1(
reinterpret_cast<const uint8_t *
>(json.data()), json.length(), stage1_mode::regular) );
133 return json_iterator(
reinterpret_cast<const uint8_t *
>(json.data()),
this);
138 if(batch_size < MINIMAL_BATCH_SIZE) { batch_size = MINIMAL_BATCH_SIZE; }
139 if((len >= 3) && (std::memcmp(buf,
"\xEF\xBB\xBF", 3) == 0)) {
143 if(allow_comma_separated && batch_size < len) { batch_size = len; }
144 return document_stream(*
this, buf, len, batch_size, allow_comma_separated);
149 return iterate_many(
reinterpret_cast<const uint8_t *
>(buf), len, batch_size, allow_comma_separated);
153 return iterate_many(s.data(), s.length(), batch_size, allow_comma_separated);
155inline simdjson_result<document_stream>
parser::iterate_many(
const padded_string &s,
size_t batch_size,
bool allow_comma_separated)
noexcept {
156 return iterate_many(padded_string_view(s), batch_size, allow_comma_separated);
158inline simdjson_result<document_stream>
parser::iterate_many(
const std::string &s,
size_t batch_size,
bool allow_comma_separated)
noexcept {
159 return iterate_many(padded_string_view(s), batch_size, allow_comma_separated);
161inline simdjson_result<document_stream>
parser::iterate_many(std::string &s,
size_t batch_size,
bool allow_comma_separated)
noexcept {
162 return iterate_many(
pad(s), batch_size, allow_comma_separated);
168 return _max_capacity;
174simdjson_inline
void parser::set_max_capacity(
size_t max_capacity)
noexcept {
175 if(max_capacity < dom::MINIMAL_DOCUMENT_CAPACITY) {
176 _max_capacity = max_capacity;
178 _max_capacity = dom::MINIMAL_DOCUMENT_CAPACITY;
183 uint8_t *end =
implementation->parse_string(in.buf, dst, allow_replacement);
185 std::string_view result(
reinterpret_cast<const char *
>(dst), end-dst);
193 std::string_view result(
reinterpret_cast<const char *
>(dst), end-dst);
199 return *parser::get_parser_instance();
202simdjson_inline
bool release_parser() {
203 auto &parser_instance = parser::get_threadlocal_parser_if_exists();
204 if (parser_instance) {
205 parser_instance.reset();
211simdjson_inline simdjson_warn_unused std::unique_ptr<ondemand::parser>& parser::get_parser_instance() {
212 std::unique_ptr<ondemand::parser>& parser_instance = get_threadlocal_parser_if_exists();
213 if (!parser_instance) {
214 parser_instance.reset(
new ondemand::parser());
216 return parser_instance;
219simdjson_inline simdjson_warn_unused std::unique_ptr<ondemand::parser>& parser::get_threadlocal_parser_if_exists() {
221 thread_local std::unique_ptr<ondemand::parser> parser_instance =
nullptr;
222 return parser_instance;
232simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::parser &&value) noexcept
233 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::parser>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::parser>(value)) {}
234simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser>::simdjson_result(
error_code error) noexcept
235 : 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.
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_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.
simdjson_result< document_stream > iterate_many(const uint8_t *buf, size_t len, size_t batch_size=DEFAULT_BATCH_SIZE, bool allow_comma_separated=false) noexcept
Parse a buffer containing many JSON documents.
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.
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.
@ 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.