2 namespace SIMDJSON_IMPLEMENTATION {
5 simdjson_inline json_iterator::json_iterator(json_iterator &&other) noexcept
6 : token(std::forward<token_iterator>(other.token)),
8 _string_buf_loc{other._string_buf_loc},
12 _streaming{other._streaming}
14 other.parser =
nullptr;
16 simdjson_inline json_iterator &json_iterator::operator=(json_iterator &&other) noexcept {
18 parser = other.parser;
19 _string_buf_loc = other._string_buf_loc;
21 _depth = other._depth;
23 _streaming = other._streaming;
24 other.parser =
nullptr;
28 simdjson_inline json_iterator::json_iterator(
const uint8_t *buf, ondemand::parser *_parser) noexcept
29 : token(buf, &_parser->implementation->structural_indexes[0]),
31 _string_buf_loc{parser->string_buf.get()},
33 _root{parser->implementation->structural_indexes.get()},
37 logger::log_headers();
38 #if SIMDJSON_CHECK_EOF
43 inline void json_iterator::rewind() noexcept {
44 token.set_position( root_position() );
45 logger::log_headers();
46 _string_buf_loc = parser->string_buf.get();
50 inline bool json_iterator::balanced() const noexcept {
51 token_iterator ti(token);
53 ti.set_position( root_position() );
54 while(ti.peek() <= peek_last()) {
55 switch (*ti.return_current_and_advance())
74 SIMDJSON_PUSH_DISABLE_WARNINGS
75 SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING
76 simdjson_warn_unused simdjson_inline
error_code json_iterator::skip_child(
depth_t parent_depth) noexcept {
77 if (depth() <= parent_depth) {
return SUCCESS; }
78 switch (*return_current_and_advance()) {
87 case '[':
case '{':
case ':':
88 logger::log_start_value(*
this,
"skip");
92 logger::log_value(*
this,
"skip");
96 logger::log_end_value(*
this,
"skip");
98 if (depth() <= parent_depth) {
return SUCCESS; }
99 #if SIMDJSON_CHECK_EOF
111 logger::log_value(*
this,
"key");
112 return_current_and_advance();
115 simdjson_fallthrough;
119 logger::log_value(*
this,
"skip");
121 if (depth() <= parent_depth) {
return SUCCESS; }
126 while (position() < end_position()) {
127 switch (*return_current_and_advance()) {
129 logger::log_start_value(*
this,
"skip");
138 logger::log_end_value(*
this,
"skip");
140 if (depth() <= parent_depth) {
return SUCCESS; }
143 logger::log_value(*
this,
"skip",
"");
148 return report_error(
TAPE_ERROR,
"not enough close braces");
151 SIMDJSON_POP_DISABLE_WARNINGS
153 simdjson_inline
bool json_iterator::at_root() const noexcept {
154 return position() == root_position();
157 simdjson_inline
bool json_iterator::is_single_token() const noexcept {
158 return parser->implementation->n_structural_indexes == 1;
161 simdjson_inline
bool json_iterator::streaming() const noexcept {
165 simdjson_inline token_position json_iterator::root_position() const noexcept {
169 simdjson_inline
void json_iterator::assert_at_document_depth() const noexcept {
170 SIMDJSON_ASSUME( _depth == 1 );
173 simdjson_inline
void json_iterator::assert_at_root() const noexcept {
174 SIMDJSON_ASSUME( _depth == 1 );
175 #ifndef SIMDJSON_CLANG_VISUAL_STUDIO
178 SIMDJSON_ASSUME( token.position() == _root );
182 simdjson_inline
void json_iterator::assert_more_tokens(uint32_t required_tokens)
const noexcept {
183 assert_valid_position(token._position + required_tokens - 1);
186 simdjson_inline
void json_iterator::assert_valid_position(token_position position)
const noexcept {
187 #ifndef SIMDJSON_CLANG_VISUAL_STUDIO
188 SIMDJSON_ASSUME( position >= &parser->implementation->structural_indexes[0] );
189 SIMDJSON_ASSUME( position < &parser->implementation->structural_indexes[parser->implementation->n_structural_indexes] );
193 simdjson_inline
bool json_iterator::at_end() const noexcept {
194 return position() == end_position();
196 simdjson_inline token_position json_iterator::end_position() const noexcept {
197 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
198 return &parser->implementation->structural_indexes[n_structural_indexes];
201 inline std::string json_iterator::to_string() const noexcept {
202 if( !is_alive() ) {
return "dead json_iterator instance"; }
203 const char * current_structural =
reinterpret_cast<const char *
>(token.peek());
204 return std::string(
"json_iterator [ depth : ") + std::to_string(_depth)
205 + std::string(
", structural : '") + std::string(current_structural,1)
206 + std::string(
"', offset : ") + std::to_string(token.current_offset())
211 inline simdjson_result<const char *> json_iterator::current_location() noexcept {
214 return reinterpret_cast<const char *
>(token.peek(-1));
216 return reinterpret_cast<const char *
>(token.peek());
222 return reinterpret_cast<const char *
>(token.peek());
225 simdjson_inline
bool json_iterator::is_alive() const noexcept {
229 simdjson_inline
void json_iterator::abandon() noexcept {
234 simdjson_inline
const uint8_t *json_iterator::return_current_and_advance() noexcept {
235 #if SIMDJSON_CHECK_EOF
236 assert_more_tokens();
238 return token.return_current_and_advance();
241 simdjson_inline
const uint8_t *json_iterator::unsafe_pointer() const noexcept {
243 return token.peek(0);
246 simdjson_inline
const uint8_t *json_iterator::peek(int32_t delta)
const noexcept {
247 #if SIMDJSON_CHECK_EOF
248 assert_more_tokens(delta+1);
250 return token.peek(delta);
253 simdjson_inline uint32_t json_iterator::peek_length(int32_t delta)
const noexcept {
254 #if SIMDJSON_CHECK_EOF
255 assert_more_tokens(delta+1);
257 return token.peek_length(delta);
260 simdjson_inline
const uint8_t *json_iterator::peek(token_position position)
const noexcept {
266 return token.peek(position);
269 simdjson_inline uint32_t json_iterator::peek_length(token_position position)
const noexcept {
270 #if SIMDJSON_CHECK_EOF
271 assert_valid_position(position);
273 return token.peek_length(position);
276 simdjson_inline token_position json_iterator::last_position() const noexcept {
280 uint32_t n_structural_indexes{parser->implementation->n_structural_indexes};
281 SIMDJSON_ASSUME(n_structural_indexes > 0);
282 return &parser->implementation->structural_indexes[n_structural_indexes - 1];
284 simdjson_inline
const uint8_t *json_iterator::peek_last() const noexcept {
285 return token.peek(last_position());
288 simdjson_inline
void json_iterator::ascend_to(
depth_t parent_depth) noexcept {
289 SIMDJSON_ASSUME(parent_depth >= 0 && parent_depth < INT32_MAX - 1);
290 SIMDJSON_ASSUME(_depth == parent_depth + 1);
291 _depth = parent_depth;
294 simdjson_inline
void json_iterator::descend_to(
depth_t child_depth) noexcept {
295 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
296 SIMDJSON_ASSUME(_depth == child_depth - 1);
297 _depth = child_depth;
300 simdjson_inline
depth_t json_iterator::depth() const noexcept {
304 simdjson_inline uint8_t *&json_iterator::string_buf_loc() noexcept {
305 return _string_buf_loc;
308 simdjson_inline
error_code json_iterator::report_error(
error_code _error,
const char *message) noexcept {
310 logger::log_error(*
this, message);
315 simdjson_inline token_position json_iterator::position() const noexcept {
316 return token.position();
319 simdjson_inline simdjson_result<std::string_view> json_iterator::unescape(raw_json_string in,
bool allow_replacement) noexcept {
320 return parser->unescape(in, _string_buf_loc, allow_replacement);
323 simdjson_inline simdjson_result<std::string_view> json_iterator::unescape_wobbly(raw_json_string in) noexcept {
324 return parser->unescape_wobbly(in, _string_buf_loc);
327 simdjson_inline
void json_iterator::reenter_child(token_position position,
depth_t child_depth) noexcept {
328 SIMDJSON_ASSUME(child_depth >= 1 && child_depth < INT32_MAX);
329 SIMDJSON_ASSUME(_depth == child_depth - 1);
330 #if SIMDJSON_DEVELOPMENT_CHECKS
331 #ifndef SIMDJSON_CLANG_VISUAL_STUDIO
332 SIMDJSON_ASSUME(
size_t(child_depth) < parser->max_depth());
333 SIMDJSON_ASSUME(position >= parser->start_positions[child_depth]);
336 token.set_position(position);
337 _depth = child_depth;
340 #if SIMDJSON_DEVELOPMENT_CHECKS
342 simdjson_inline token_position json_iterator::start_position(
depth_t depth)
const noexcept {
343 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
344 return size_t(depth) < parser->max_depth() ? parser->start_positions[depth] : 0;
347 simdjson_inline
void json_iterator::set_start_position(
depth_t depth, token_position position) noexcept {
348 SIMDJSON_ASSUME(
size_t(depth) < parser->max_depth());
349 if(
size_t(depth) < parser->max_depth()) { parser->start_positions[depth] = position; }
355 simdjson_inline
error_code json_iterator::optional_error(
error_code _error,
const char *message) noexcept {
357 logger::log_error(*
this, message);
362 simdjson_warn_unused simdjson_inline
bool json_iterator::copy_to_buffer(
const uint8_t *json, uint32_t max_len, uint8_t (&tmpbuf)[N]) noexcept {
364 if((N < max_len) || (N == 0)) {
return false; }
372 std::memcpy(tmpbuf, json, max_len);
373 tmpbuf[max_len] =
' ';
383 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &&value) noexcept
384 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>(value)) {}
385 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_iterator>::simdjson_result(
error_code error) noexcept
386 : 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).
We want to support argument-dependent lookup (ADL).
const char * error_message(error_code error) noexcept
Get the error message for the given error code.
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