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 #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON
58 simdjson_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();
68 #if SIMDJSON_CHECK_EOF
74 inline void json_iterator::rewind() noexcept {
75 token.set_position( root_position() );
76 logger::log_headers();
77 _string_buf_loc = parser->string_buf.get();
81 inline 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())
105 SIMDJSON_PUSH_DISABLE_WARNINGS
106 SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING
107 simdjson_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");
182 SIMDJSON_POP_DISABLE_WARNINGS
184 simdjson_inline
bool json_iterator::at_root() const noexcept {
185 return position() == root_position();
188 simdjson_inline
bool json_iterator::is_single_token() const noexcept {
189 return parser->implementation->n_structural_indexes == 1;
192 simdjson_inline
bool json_iterator::streaming() const noexcept {
196 simdjson_inline token_position json_iterator::root_position() const noexcept {
200 simdjson_inline
void json_iterator::assert_at_document_depth() const noexcept {
201 SIMDJSON_ASSUME( _depth == 1 );
204 simdjson_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 );
213 simdjson_inline
void json_iterator::assert_more_tokens(uint32_t required_tokens)
const noexcept {
214 assert_valid_position(token._position + required_tokens - 1);
217 simdjson_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] );
224 simdjson_inline
bool json_iterator::at_end() const noexcept {
225 return position() == end_position();
227 simdjson_inline token_position json_iterator::end_position() const noexcept {
228 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
229 return &parser->implementation->structural_indexes[n_structural_indexes];
232 inline std::string json_iterator::to_string() const noexcept {
233 if( !is_alive() ) {
return "dead json_iterator instance"; }
234 const char * current_structural =
reinterpret_cast<const char *
>(token.peek());
235 return std::string(
"json_iterator [ depth : ") + std::to_string(_depth)
236 + std::string(
", structural : '") + std::string(current_structural,1)
237 + std::string(
"', offset : ") + std::to_string(token.current_offset())
242 inline simdjson_result<const char *> json_iterator::current_location() const noexcept {
245 return reinterpret_cast<const char *
>(token.peek(-1));
247 return reinterpret_cast<const char *
>(token.peek());
253 return reinterpret_cast<const char *
>(token.peek());
256 simdjson_inline
bool json_iterator::is_alive() const noexcept {
260 simdjson_inline
void json_iterator::abandon() noexcept {
265 simdjson_inline
const uint8_t *json_iterator::return_current_and_advance() noexcept {
266 #if SIMDJSON_CHECK_EOF
267 assert_more_tokens();
269 return token.return_current_and_advance();
272 simdjson_inline
const uint8_t *json_iterator::unsafe_pointer() const noexcept {
277 simdjson_inline
const uint8_t *json_iterator::peek(int32_t delta)
const noexcept {
278 #if SIMDJSON_CHECK_EOF
279 assert_more_tokens(delta+1);
281 return token.peek(delta);
284 simdjson_inline uint32_t json_iterator::peek_length(int32_t delta)
const noexcept {
285 #if SIMDJSON_CHECK_EOF
286 assert_more_tokens(delta+1);
288 return token.peek_length(delta);
291 simdjson_inline
const uint8_t *json_iterator::peek(token_position position)
const noexcept {
297 return token.peek(position);
300 simdjson_inline uint32_t json_iterator::peek_length(token_position position)
const noexcept {
301 #if SIMDJSON_CHECK_EOF
302 assert_valid_position(position);
304 return token.peek_length(position);
306 simdjson_inline uint32_t json_iterator::peek_root_length(token_position position)
const noexcept {
307 #if SIMDJSON_CHECK_EOF
308 assert_valid_position(position);
310 return token.peek_root_length(position);
313 simdjson_inline token_position json_iterator::last_position() const noexcept {
317 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
318 SIMDJSON_ASSUME(n_structural_indexes > 0);
319 return &parser->implementation->structural_indexes[n_structural_indexes - 1];
321 simdjson_inline
const uint8_t *json_iterator::peek_last() const noexcept {
322 return token.peek(last_position());
325 simdjson_inline
void json_iterator::ascend_to(
depth_t parent_depth) noexcept {
326 SIMDJSON_ASSUME(parent_depth >= 0 && parent_depth < INT32_MAX - 1);
327 SIMDJSON_ASSUME(_depth == parent_depth + 1);
328 _depth = parent_depth;
331 simdjson_inline
void json_iterator::descend_to(
depth_t child_depth) noexcept {
332 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
333 SIMDJSON_ASSUME(_depth == child_depth - 1);
334 _depth = child_depth;
337 simdjson_inline
depth_t json_iterator::depth() const noexcept {
341 simdjson_inline uint8_t *&json_iterator::string_buf_loc() noexcept {
342 return _string_buf_loc;
345 simdjson_inline
error_code json_iterator::report_error(
error_code _error,
const char *message) noexcept {
347 logger::log_error(*
this, message);
352 simdjson_inline token_position json_iterator::position() const noexcept {
353 return token.position();
356 simdjson_inline simdjson_result<std::string_view> json_iterator::unescape(raw_json_string in,
bool allow_replacement) noexcept {
357 #if SIMDJSON_DEVELOPMENT_CHECKS
358 auto result = parser->unescape(in, _string_buf_loc, allow_replacement);
359 SIMDJSON_ASSUME(!parser->string_buffer_overflow(_string_buf_loc));
362 return parser->unescape(in, _string_buf_loc, allow_replacement);
366 simdjson_inline simdjson_result<std::string_view> json_iterator::unescape_wobbly(raw_json_string in) noexcept {
367 #if SIMDJSON_DEVELOPMENT_CHECKS
368 auto result = parser->unescape_wobbly(in, _string_buf_loc);
369 SIMDJSON_ASSUME(!parser->string_buffer_overflow(_string_buf_loc));
372 return parser->unescape_wobbly(in, _string_buf_loc);
376 simdjson_inline
void json_iterator::reenter_child(token_position position,
depth_t child_depth) noexcept {
377 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
378 SIMDJSON_ASSUME(_depth == child_depth - 1);
379 #if SIMDJSON_DEVELOPMENT_CHECKS
380 #ifndef SIMDJSON_CLANG_VISUAL_STUDIO
381 SIMDJSON_ASSUME(
size_t(child_depth) < parser->max_depth());
382 SIMDJSON_ASSUME(position >= parser->start_positions[child_depth]);
385 token.set_position(position);
386 _depth = child_depth;
389 simdjson_inline
error_code json_iterator::consume_character(
char c) noexcept {
391 return_current_and_advance();
397 #if SIMDJSON_DEVELOPMENT_CHECKS
399 simdjson_inline token_position json_iterator::start_position(
depth_t depth)
const noexcept {
400 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
401 return size_t(depth) < parser->max_depth() ? parser->start_positions[depth] : 0;
404 simdjson_inline
void json_iterator::set_start_position(
depth_t depth, token_position position) noexcept {
405 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
406 if(
size_t(depth) < parser->max_depth()) { parser->start_positions[depth] = position; }
412 simdjson_inline
error_code json_iterator::optional_error(
error_code _error,
const char *message) noexcept {
414 logger::log_error(*
this, message);
419 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 {
422 if((N < max_len) || (N == 0)) {
return false; }
424 std::memcpy(tmpbuf, json, max_len);
426 std::memset(tmpbuf + max_len,
' ', N - max_len);
437 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &&value) noexcept
438 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(value)) {}
439 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(
error_code error) noexcept
440 : 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