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"
16namespace SIMDJSON_IMPLEMENTATION {
19simdjson_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;
30simdjson_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;
42simdjson_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();
57#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
58simdjson_inline json_iterator::json_iterator(
const uint8_t *buf, ondemand::parser *_parser,
bool streaming) noexcept
59 : token(buf, &_parser->implementation->structural_indexes[0]),
61 _string_buf_loc{parser->string_buf.get()},
63 _root{parser->implementation->structural_indexes.get()},
67 logger::log_headers();
74inline void json_iterator::rewind() noexcept {
75 token.set_position( root_position() );
76 logger::log_headers();
77 _string_buf_loc = parser->string_buf.get();
81inline bool json_iterator::balanced() const noexcept {
82 token_iterator ti(token);
84 ti.set_position( root_position() );
85 while(ti.peek() <= peek_last()) {
86 switch (*ti.return_current_and_advance())
105SIMDJSON_PUSH_DISABLE_WARNINGS
106SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING
107simdjson_warn_unused simdjson_inline
error_code json_iterator::skip_child(
depth_t parent_depth)
noexcept {
108 if (depth() <= parent_depth) {
return SUCCESS; }
109 switch (*return_current_and_advance()) {
118 case '[':
case '{':
case ':':
119 logger::log_start_value(*
this,
"skip");
123 logger::log_value(*
this,
"skip");
127 logger::log_end_value(*
this,
"skip");
129 if (depth() <= parent_depth) {
return SUCCESS; }
130#if SIMDJSON_CHECK_EOF
142 logger::log_value(*
this,
"key");
143 return_current_and_advance();
146 simdjson_fallthrough;
150 logger::log_value(*
this,
"skip");
152 if (depth() <= parent_depth) {
return SUCCESS; }
157 while (position() < end_position()) {
158 switch (*return_current_and_advance()) {
160 logger::log_start_value(*
this,
"skip");
169 logger::log_end_value(*
this,
"skip");
171 if (depth() <= parent_depth) {
return SUCCESS; }
174 logger::log_value(*
this,
"skip",
"");
179 return report_error(
TAPE_ERROR,
"not enough close braces");
182SIMDJSON_POP_DISABLE_WARNINGS
184simdjson_inline
bool json_iterator::at_root() const noexcept {
185 return position() == root_position();
188simdjson_inline
bool json_iterator::is_single_token() const noexcept {
189 return parser->implementation->n_structural_indexes == 1;
192simdjson_inline
bool json_iterator::streaming() const noexcept {
196simdjson_inline token_position json_iterator::root_position() const noexcept {
200simdjson_inline
void json_iterator::assert_at_document_depth() const noexcept {
201 SIMDJSON_ASSUME( _depth == 1 );
204simdjson_inline
void json_iterator::assert_at_root() const noexcept {
205 SIMDJSON_ASSUME( _depth == 1 );
206#ifndef SIMDJSON_CLANG_VISUAL_STUDIO
209 SIMDJSON_ASSUME( token.position() == _root );
213simdjson_inline
void json_iterator::assert_more_tokens(uint32_t required_tokens)
const noexcept {
214 assert_valid_position(token._position + required_tokens - 1);
217simdjson_inline
void json_iterator::assert_valid_position(token_position position)
const noexcept {
219#ifndef SIMDJSON_CLANG_VISUAL_STUDIO
220 SIMDJSON_ASSUME( position >= &parser->implementation->structural_indexes[0] );
221 SIMDJSON_ASSUME( position < &parser->implementation->structural_indexes[parser->implementation->n_structural_indexes] );
225simdjson_inline
bool json_iterator::at_end() const noexcept {
226 return position() == end_position();
228simdjson_inline token_position json_iterator::end_position() const noexcept {
229 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
230 return &parser->implementation->structural_indexes[n_structural_indexes];
233inline std::string json_iterator::to_string() const noexcept {
234 if( !is_alive() ) {
return "dead json_iterator instance"; }
235 const char * current_structural =
reinterpret_cast<const char *
>(token.peek());
236 return std::string(
"json_iterator [ depth : ") + std::to_string(_depth)
237 + std::string(
", structural : '") + std::string(current_structural,1)
238 + std::string(
"', offset : ") + std::to_string(token.current_offset())
243inline simdjson_result<const char *> json_iterator::current_location() const noexcept {
246 return reinterpret_cast<const char *
>(token.peek(-1));
248 return reinterpret_cast<const char *
>(token.peek());
254 return reinterpret_cast<const char *
>(token.peek());
257simdjson_inline
bool json_iterator::is_alive() const noexcept {
261simdjson_inline
void json_iterator::abandon() noexcept {
266simdjson_inline
const uint8_t *json_iterator::return_current_and_advance() noexcept {
267#if SIMDJSON_CHECK_EOF
268 assert_more_tokens();
270 return token.return_current_and_advance();
273simdjson_inline
const uint8_t *json_iterator::unsafe_pointer() const noexcept {
278simdjson_inline
const uint8_t *json_iterator::peek(int32_t delta)
const noexcept {
279#if SIMDJSON_CHECK_EOF
280 assert_more_tokens(delta+1);
282 return token.peek(delta);
285simdjson_inline uint32_t json_iterator::peek_length(int32_t delta)
const noexcept {
286#if SIMDJSON_CHECK_EOF
287 assert_more_tokens(delta+1);
289 return token.peek_length(delta);
292simdjson_inline
const uint8_t *json_iterator::peek(token_position position)
const noexcept {
298 return token.peek(position);
301simdjson_inline uint32_t json_iterator::peek_length(token_position position)
const noexcept {
302#if SIMDJSON_CHECK_EOF
303 assert_valid_position(position);
305 return token.peek_length(position);
307simdjson_inline uint32_t json_iterator::peek_root_length(token_position position)
const noexcept {
308#if SIMDJSON_CHECK_EOF
309 assert_valid_position(position);
311 return token.peek_root_length(position);
314simdjson_inline token_position json_iterator::last_position() const noexcept {
318 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
319 SIMDJSON_ASSUME(n_structural_indexes > 0);
320 return &parser->implementation->structural_indexes[n_structural_indexes - 1];
322simdjson_inline
const uint8_t *json_iterator::peek_last() const noexcept {
323 return token.peek(last_position());
326simdjson_inline
void json_iterator::ascend_to(
depth_t parent_depth)
noexcept {
327 SIMDJSON_ASSUME(parent_depth >= 0 && parent_depth < INT32_MAX - 1);
328 SIMDJSON_ASSUME(_depth == parent_depth + 1);
329 _depth = parent_depth;
332simdjson_inline
void json_iterator::descend_to(
depth_t child_depth)
noexcept {
333 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
334 SIMDJSON_ASSUME(_depth == child_depth - 1);
335 _depth = child_depth;
338simdjson_inline
depth_t json_iterator::depth() const noexcept {
342simdjson_inline uint8_t *&json_iterator::string_buf_loc() noexcept {
343 return _string_buf_loc;
346simdjson_warn_unused simdjson_inline
error_code json_iterator::report_error(
error_code _error,
const char *message)
noexcept {
348 logger::log_error(*
this, message);
353simdjson_inline token_position json_iterator::position() const noexcept {
354 return token.position();
357simdjson_inline simdjson_result<std::string_view> json_iterator::unescape(raw_json_string in,
bool allow_replacement)
noexcept {
358#if SIMDJSON_DEVELOPMENT_CHECKS
359 auto result = parser->unescape(in, _string_buf_loc, allow_replacement);
360#if !defined(SIMDJSON_VISUAL_STUDIO) && !defined(SIMDJSON_CLANG_VISUAL_STUDIO)
363 SIMDJSON_ASSUME(!parser->string_buffer_overflow(_string_buf_loc));
367 return parser->unescape(in, _string_buf_loc, allow_replacement);
371simdjson_inline simdjson_result<std::string_view> json_iterator::unescape_wobbly(raw_json_string in)
noexcept {
372#if SIMDJSON_DEVELOPMENT_CHECKS
373 auto result = parser->unescape_wobbly(in, _string_buf_loc);
374#if !defined(SIMDJSON_VISUAL_STUDIO) && !defined(SIMDJSON_CLANG_VISUAL_STUDIO)
377 SIMDJSON_ASSUME(!parser->string_buffer_overflow(_string_buf_loc));
381 return parser->unescape_wobbly(in, _string_buf_loc);
385simdjson_inline
void json_iterator::reenter_child(token_position position,
depth_t child_depth)
noexcept {
386 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
387 SIMDJSON_ASSUME(_depth == child_depth - 1);
388#if SIMDJSON_DEVELOPMENT_CHECKS
389#ifndef SIMDJSON_CLANG_VISUAL_STUDIO
390 SIMDJSON_ASSUME(
size_t(child_depth) < parser->max_depth());
391 SIMDJSON_ASSUME(position >= parser->start_positions[child_depth]);
394 token.set_position(position);
395 _depth = child_depth;
398simdjson_warn_unused simdjson_inline
error_code json_iterator::consume_character(
char c)
noexcept {
400 return_current_and_advance();
406#if SIMDJSON_DEVELOPMENT_CHECKS
408simdjson_inline token_position json_iterator::start_position(
depth_t depth)
const noexcept {
409 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
410 return size_t(depth) < parser->max_depth() ? parser->start_positions[depth] : 0;
413simdjson_inline
void json_iterator::set_start_position(
depth_t depth, token_position position)
noexcept {
414 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
415 if(
size_t(depth) < parser->max_depth()) { parser->start_positions[depth] = position; }
421simdjson_warn_unused simdjson_inline
error_code json_iterator::optional_error(
error_code _error,
const char *message)
noexcept {
423 logger::log_error(*
this, message);
428simdjson_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 {
431 if((N < max_len) || (N == 0)) {
return false; }
433 std::memcpy(tmpbuf, json, max_len);
435 std::memset(tmpbuf + max_len,
' ', N - max_len);
446simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &&value) noexcept
447 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(value)) {}
448simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(
error_code error) noexcept
449 : 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. Fatal/unrecoverable error.
@ NO_SUCH_FIELD
JSON field not found in object.
@ INCOMPLETE_ARRAY_OR_OBJECT
The document ends early. Fatal/unrecoverable error.
@ UNINITIALIZED
unknown error, or uninitialized document