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 {
218#ifndef SIMDJSON_CLANG_VISUAL_STUDIO
219 SIMDJSON_ASSUME( position >= &parser->implementation->structural_indexes[0] );
220 SIMDJSON_ASSUME( position < &parser->implementation->structural_indexes[parser->implementation->n_structural_indexes] );
226simdjson_inline
bool json_iterator::at_end() const noexcept {
227 return position() == end_position();
229simdjson_inline token_position json_iterator::end_position() const noexcept {
230 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
231 return &parser->implementation->structural_indexes[n_structural_indexes];
234inline std::string json_iterator::to_string() const noexcept {
235 if( !is_alive() ) {
return "dead json_iterator instance"; }
236 const char * current_structural =
reinterpret_cast<const char *
>(token.peek());
237 return std::string(
"json_iterator [ depth : ") + std::to_string(_depth)
238 + std::string(
", structural : '") + std::string(current_structural,1)
239 + std::string(
"', offset : ") + std::to_string(token.current_offset())
244inline simdjson_result<const char *> json_iterator::current_location() const noexcept {
247 return reinterpret_cast<const char *
>(token.peek(-1));
249 return reinterpret_cast<const char *
>(token.peek());
255 return reinterpret_cast<const char *
>(token.peek());
258simdjson_inline
bool json_iterator::is_alive() const noexcept {
262simdjson_inline
void json_iterator::abandon() noexcept {
267simdjson_inline
const uint8_t *json_iterator::return_current_and_advance() noexcept {
268#if SIMDJSON_CHECK_EOF
269 assert_more_tokens();
271 return token.return_current_and_advance();
274simdjson_inline
const uint8_t *json_iterator::unsafe_pointer() const noexcept {
279simdjson_inline
const uint8_t *json_iterator::peek(int32_t delta)
const noexcept {
280#if SIMDJSON_CHECK_EOF
281 assert_more_tokens(delta+1);
283 return token.peek(delta);
286simdjson_inline uint32_t json_iterator::peek_length(int32_t delta)
const noexcept {
287#if SIMDJSON_CHECK_EOF
288 assert_more_tokens(delta+1);
290 return token.peek_length(delta);
293simdjson_inline
const uint8_t *json_iterator::peek(token_position position)
const noexcept {
299 return token.peek(position);
302simdjson_inline uint32_t json_iterator::peek_length(token_position position)
const noexcept {
303#if SIMDJSON_CHECK_EOF
304 assert_valid_position(position);
306 return token.peek_length(position);
308simdjson_inline uint32_t json_iterator::peek_root_length(token_position position)
const noexcept {
309#if SIMDJSON_CHECK_EOF
310 assert_valid_position(position);
312 return token.peek_root_length(position);
315simdjson_inline token_position json_iterator::last_position() const noexcept {
319 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
320 SIMDJSON_ASSUME(n_structural_indexes > 0);
321 return &parser->implementation->structural_indexes[n_structural_indexes - 1];
323simdjson_inline
const uint8_t *json_iterator::peek_last() const noexcept {
324 return token.peek(last_position());
327simdjson_inline
void json_iterator::ascend_to(
depth_t parent_depth)
noexcept {
328 SIMDJSON_ASSUME(parent_depth >= 0 && parent_depth < INT32_MAX - 1);
329 SIMDJSON_ASSUME(_depth == parent_depth + 1);
330 _depth = parent_depth;
333simdjson_inline
void json_iterator::descend_to(
depth_t child_depth)
noexcept {
334 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
335 SIMDJSON_ASSUME(_depth == child_depth - 1);
336 _depth = child_depth;
339simdjson_inline
depth_t json_iterator::depth() const noexcept {
343simdjson_inline uint8_t *&json_iterator::string_buf_loc() noexcept {
344 return _string_buf_loc;
347simdjson_warn_unused simdjson_inline
error_code json_iterator::report_error(
error_code _error,
const char *message)
noexcept {
349 logger::log_error(*
this, message);
354simdjson_inline token_position json_iterator::position() const noexcept {
355 return token.position();
358simdjson_inline simdjson_result<std::string_view> json_iterator::unescape(raw_json_string in,
bool allow_replacement)
noexcept {
359#if SIMDJSON_DEVELOPMENT_CHECKS
360 auto result = parser->unescape(in, _string_buf_loc, allow_replacement);
361 SIMDJSON_ASSUME(!parser->string_buffer_overflow(_string_buf_loc));
364 return parser->unescape(in, _string_buf_loc, allow_replacement);
368simdjson_inline simdjson_result<std::string_view> json_iterator::unescape_wobbly(raw_json_string in)
noexcept {
369#if SIMDJSON_DEVELOPMENT_CHECKS
370 auto result = parser->unescape_wobbly(in, _string_buf_loc);
371 SIMDJSON_ASSUME(!parser->string_buffer_overflow(_string_buf_loc));
374 return parser->unescape_wobbly(in, _string_buf_loc);
378simdjson_inline
void json_iterator::reenter_child(token_position position,
depth_t child_depth)
noexcept {
379 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
380 SIMDJSON_ASSUME(_depth == child_depth - 1);
381#if SIMDJSON_DEVELOPMENT_CHECKS
382#ifndef SIMDJSON_CLANG_VISUAL_STUDIO
383 SIMDJSON_ASSUME(
size_t(child_depth) < parser->max_depth());
384 SIMDJSON_ASSUME(position >= parser->start_positions[child_depth]);
387 token.set_position(position);
388 _depth = child_depth;
391simdjson_warn_unused simdjson_inline
error_code json_iterator::consume_character(
char c)
noexcept {
393 return_current_and_advance();
399#if SIMDJSON_DEVELOPMENT_CHECKS
401simdjson_inline token_position json_iterator::start_position(
depth_t depth)
const noexcept {
402 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
403 return size_t(depth) < parser->max_depth() ? parser->start_positions[depth] : 0;
406simdjson_inline
void json_iterator::set_start_position(
depth_t depth, token_position position)
noexcept {
407 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
408 if(
size_t(depth) < parser->max_depth()) { parser->start_positions[depth] = position; }
414simdjson_warn_unused simdjson_inline
error_code json_iterator::optional_error(
error_code _error,
const char *message)
noexcept {
416 logger::log_error(*
this, message);
421simdjson_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 {
424 if((N < max_len) || (N == 0)) {
return false; }
426 std::memcpy(tmpbuf, json, max_len);
428 std::memset(tmpbuf + max_len,
' ', N - max_len);
439simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &&value) noexcept
440 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(value)) {}
441simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(
error_code error) noexcept
442 : 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