1 #ifndef SIMDJSON_GENERIC_ONDEMAND_JSON_ITERATOR_INL_H
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_JSON_ITERATOR_INL_H
5 #include "simdjson/internal/dom_parser_implementation.h"
6 #include "simdjson/generic/ondemand/base.h"
7 #include "simdjson/generic/ondemand/json_iterator.h"
8 #include "simdjson/generic/ondemand/parser.h"
9 #include "simdjson/generic/ondemand/raw_json_string.h"
10 #include "simdjson/generic/ondemand/logger-inl.h"
11 #include "simdjson/generic/ondemand/parser-inl.h"
12 #include "simdjson/generic/ondemand/token_iterator-inl.h"
16 namespace SIMDJSON_IMPLEMENTATION {
19 simdjson_inline json_iterator::json_iterator(json_iterator &&other) noexcept
20 : token(std::forward<token_iterator>(other.token)),
22 _string_buf_loc{other._string_buf_loc},
26 _streaming{other._streaming}
28 other.parser =
nullptr;
30 simdjson_inline json_iterator &json_iterator::operator=(json_iterator &&other) noexcept {
32 parser = other.parser;
33 _string_buf_loc = other._string_buf_loc;
35 _depth = other._depth;
37 _streaming = other._streaming;
38 other.parser =
nullptr;
42 simdjson_inline json_iterator::json_iterator(
const uint8_t *buf, ondemand::parser *_parser) noexcept
43 : token(buf, &_parser->implementation->structural_indexes[0]),
45 _string_buf_loc{parser->string_buf.get()},
47 _root{parser->implementation->structural_indexes.get()},
51 logger::log_headers();
52 #if SIMDJSON_CHECK_EOF
57 inline void json_iterator::rewind() noexcept {
58 token.set_position( root_position() );
59 logger::log_headers();
60 _string_buf_loc = parser->string_buf.get();
64 inline bool json_iterator::balanced() const noexcept {
65 token_iterator ti(token);
67 ti.set_position( root_position() );
68 while(ti.peek() <= peek_last()) {
69 switch (*ti.return_current_and_advance())
88 SIMDJSON_PUSH_DISABLE_WARNINGS
89 SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING
90 simdjson_warn_unused simdjson_inline
error_code json_iterator::skip_child(
depth_t parent_depth) noexcept {
91 if (depth() <= parent_depth) {
return SUCCESS; }
92 switch (*return_current_and_advance()) {
101 case '[':
case '{':
case ':':
102 logger::log_start_value(*
this,
"skip");
106 logger::log_value(*
this,
"skip");
110 logger::log_end_value(*
this,
"skip");
112 if (depth() <= parent_depth) {
return SUCCESS; }
113 #if SIMDJSON_CHECK_EOF
125 logger::log_value(*
this,
"key");
126 return_current_and_advance();
129 simdjson_fallthrough;
133 logger::log_value(*
this,
"skip");
135 if (depth() <= parent_depth) {
return SUCCESS; }
140 while (position() < end_position()) {
141 switch (*return_current_and_advance()) {
143 logger::log_start_value(*
this,
"skip");
152 logger::log_end_value(*
this,
"skip");
154 if (depth() <= parent_depth) {
return SUCCESS; }
157 logger::log_value(*
this,
"skip",
"");
162 return report_error(
TAPE_ERROR,
"not enough close braces");
165 SIMDJSON_POP_DISABLE_WARNINGS
167 simdjson_inline
bool json_iterator::at_root() const noexcept {
168 return position() == root_position();
171 simdjson_inline
bool json_iterator::is_single_token() const noexcept {
172 return parser->implementation->n_structural_indexes == 1;
175 simdjson_inline
bool json_iterator::streaming() const noexcept {
179 simdjson_inline token_position json_iterator::root_position() const noexcept {
183 simdjson_inline
void json_iterator::assert_at_document_depth() const noexcept {
184 SIMDJSON_ASSUME( _depth == 1 );
187 simdjson_inline
void json_iterator::assert_at_root() const noexcept {
188 SIMDJSON_ASSUME( _depth == 1 );
189 #ifndef SIMDJSON_CLANG_VISUAL_STUDIO
192 SIMDJSON_ASSUME( token.position() == _root );
196 simdjson_inline
void json_iterator::assert_more_tokens(uint32_t required_tokens)
const noexcept {
197 assert_valid_position(token._position + required_tokens - 1);
200 simdjson_inline
void json_iterator::assert_valid_position(token_position position)
const noexcept {
201 #ifndef SIMDJSON_CLANG_VISUAL_STUDIO
202 SIMDJSON_ASSUME( position >= &parser->implementation->structural_indexes[0] );
203 SIMDJSON_ASSUME( position < &parser->implementation->structural_indexes[parser->implementation->n_structural_indexes] );
207 simdjson_inline
bool json_iterator::at_end() const noexcept {
208 return position() == end_position();
210 simdjson_inline token_position json_iterator::end_position() const noexcept {
211 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
212 return &parser->implementation->structural_indexes[n_structural_indexes];
215 inline std::string json_iterator::to_string() const noexcept {
216 if( !is_alive() ) {
return "dead json_iterator instance"; }
217 const char * current_structural =
reinterpret_cast<const char *
>(token.peek());
218 return std::string(
"json_iterator [ depth : ") + std::to_string(_depth)
219 + std::string(
", structural : '") + std::string(current_structural,1)
220 + std::string(
"', offset : ") + std::to_string(token.current_offset())
225 inline simdjson_result<const char *> json_iterator::current_location() const noexcept {
228 return reinterpret_cast<const char *
>(token.peek(-1));
230 return reinterpret_cast<const char *
>(token.peek());
236 return reinterpret_cast<const char *
>(token.peek());
239 simdjson_inline
bool json_iterator::is_alive() const noexcept {
243 simdjson_inline
void json_iterator::abandon() noexcept {
248 simdjson_inline
const uint8_t *json_iterator::return_current_and_advance() noexcept {
249 #if SIMDJSON_CHECK_EOF
250 assert_more_tokens();
252 return token.return_current_and_advance();
255 simdjson_inline
const uint8_t *json_iterator::unsafe_pointer() const noexcept {
260 simdjson_inline
const uint8_t *json_iterator::peek(int32_t delta)
const noexcept {
261 #if SIMDJSON_CHECK_EOF
262 assert_more_tokens(delta+1);
264 return token.peek(delta);
267 simdjson_inline uint32_t json_iterator::peek_length(int32_t delta)
const noexcept {
268 #if SIMDJSON_CHECK_EOF
269 assert_more_tokens(delta+1);
271 return token.peek_length(delta);
274 simdjson_inline
const uint8_t *json_iterator::peek(token_position position)
const noexcept {
280 return token.peek(position);
283 simdjson_inline uint32_t json_iterator::peek_length(token_position position)
const noexcept {
284 #if SIMDJSON_CHECK_EOF
285 assert_valid_position(position);
287 return token.peek_length(position);
289 simdjson_inline uint32_t json_iterator::peek_root_length(token_position position)
const noexcept {
290 #if SIMDJSON_CHECK_EOF
291 assert_valid_position(position);
293 return token.peek_root_length(position);
296 simdjson_inline token_position json_iterator::last_position() const noexcept {
300 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
301 SIMDJSON_ASSUME(n_structural_indexes > 0);
302 return &parser->implementation->structural_indexes[n_structural_indexes - 1];
304 simdjson_inline
const uint8_t *json_iterator::peek_last() const noexcept {
305 return token.peek(last_position());
308 simdjson_inline
void json_iterator::ascend_to(
depth_t parent_depth) noexcept {
309 SIMDJSON_ASSUME(parent_depth >= 0 && parent_depth < INT32_MAX - 1);
310 SIMDJSON_ASSUME(_depth == parent_depth + 1);
311 _depth = parent_depth;
314 simdjson_inline
void json_iterator::descend_to(
depth_t child_depth) noexcept {
315 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
316 SIMDJSON_ASSUME(_depth == child_depth - 1);
317 _depth = child_depth;
320 simdjson_inline
depth_t json_iterator::depth() const noexcept {
324 simdjson_inline uint8_t *&json_iterator::string_buf_loc() noexcept {
325 return _string_buf_loc;
328 simdjson_inline
error_code json_iterator::report_error(
error_code _error,
const char *message) noexcept {
330 logger::log_error(*
this, message);
335 simdjson_inline token_position json_iterator::position() const noexcept {
336 return token.position();
339 simdjson_inline simdjson_result<std::string_view> json_iterator::unescape(raw_json_string in,
bool allow_replacement) noexcept {
340 return parser->unescape(in, _string_buf_loc, allow_replacement);
343 simdjson_inline simdjson_result<std::string_view> json_iterator::unescape_wobbly(raw_json_string in) noexcept {
344 return parser->unescape_wobbly(in, _string_buf_loc);
347 simdjson_inline
void json_iterator::reenter_child(token_position position,
depth_t child_depth) noexcept {
348 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
349 SIMDJSON_ASSUME(_depth == child_depth - 1);
350 #if SIMDJSON_DEVELOPMENT_CHECKS
351 #ifndef SIMDJSON_CLANG_VISUAL_STUDIO
352 SIMDJSON_ASSUME(
size_t(child_depth) < parser->max_depth());
353 SIMDJSON_ASSUME(position >= parser->start_positions[child_depth]);
356 token.set_position(position);
357 _depth = child_depth;
360 simdjson_inline
error_code json_iterator::consume_character(
char c) noexcept {
362 return_current_and_advance();
368 #if SIMDJSON_DEVELOPMENT_CHECKS
370 simdjson_inline token_position json_iterator::start_position(
depth_t depth)
const noexcept {
371 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
372 return size_t(depth) < parser->max_depth() ? parser->start_positions[depth] : 0;
375 simdjson_inline
void json_iterator::set_start_position(
depth_t depth, token_position position) noexcept {
376 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
377 if(
size_t(depth) < parser->max_depth()) { parser->start_positions[depth] = position; }
383 simdjson_inline
error_code json_iterator::optional_error(
error_code _error,
const char *message) noexcept {
385 logger::log_error(*
this, message);
390 simdjson_warn_unused simdjson_inline
bool json_iterator::copy_to_buffer(
const uint8_t *json, uint32_t max_len, uint8_t *tmpbuf,
size_t N) noexcept {
393 if((N < max_len) || (N == 0)) {
return false; }
395 std::memcpy(tmpbuf, json, max_len);
397 std::memset(tmpbuf + max_len,
' ', N - max_len);
408 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &&value) noexcept
409 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(value)) {}
410 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(
error_code error) noexcept
411 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(error) {}
int32_t depth_t
Represents the depth of a JSON value (number of nested arrays/objects).
The top level simdjson namespace, containing everything the library provides.
const char * error_message(error_code error) noexcept
It is the convention throughout the code that the macro SIMDJSON_DEVELOPMENT_CHECKS determines whethe...
error_code
All possible errors returned by simdjson.
@ INCORRECT_TYPE
JSON element has a different type than user expected.
@ OUT_OF_BOUNDS
Attempted to access location outside of document.
@ TAPE_ERROR
Something went wrong, this is a generic error.
@ NO_SUCH_FIELD
JSON field not found in object.
@ INCOMPLETE_ARRAY_OR_OBJECT
The document ends early.
@ UNINITIALIZED
unknown error, or uninitialized document