simdjson 4.4.0
Ridiculously Fast JSON
Loading...
Searching...
No Matches
object_iterator-inl.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_OBJECT_ITERATOR_INL_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_OBJECT_ITERATOR_INL_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/ondemand/object_iterator.h"
7#include "simdjson/generic/ondemand/field-inl.h"
8#include "simdjson/generic/ondemand/value_iterator-inl.h"
9#endif // SIMDJSON_CONDITIONAL_INCLUDE
10
11namespace simdjson {
12namespace SIMDJSON_IMPLEMENTATION {
13namespace ondemand {
14
15//
16// object_iterator
17//
18
19simdjson_inline object_iterator::object_iterator(const value_iterator &_iter) noexcept
20 : iter{_iter}
21{}
22
23simdjson_inline simdjson_result<field> object_iterator::operator*() noexcept {
24#if SIMDJSON_DEVELOPMENT_CHECKS
25 // We must call * once per iteration.
26 SIMDJSON_ASSUME(!has_been_referenced);
27 has_been_referenced = true;
28#endif
29 error_code error = iter.error();
30 if (error) { iter.abandon(); return error; }
31 auto result = field::start(iter);
32 // TODO this is a safety rail ... users should exit loops as soon as they receive an error.
33 // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free.
34 if (result.error()) { iter.abandon(); }
35 return result;
36}
37simdjson_inline bool object_iterator::operator==(const object_iterator &other) const noexcept {
38 return !(*this != other);
39}
40simdjson_inline bool object_iterator::operator!=(const object_iterator &) const noexcept {
41 return iter.is_open();
42}
43
44SIMDJSON_PUSH_DISABLE_WARNINGS
45SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING
46simdjson_inline object_iterator &object_iterator::operator++() noexcept {
47#if SIMDJSON_DEVELOPMENT_CHECKS
48 // Before calling ++, we must have called *.
49 SIMDJSON_ASSUME(has_been_referenced);
50 has_been_referenced = false;
51#endif
52 // TODO this is a safety rail ... users should exit loops as soon as they receive an error.
53 // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free.
54 if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error
55
56 simdjson_unused error_code error;
57 if ((error = iter.skip_child() )) { return *this; }
58
59 simdjson_unused bool has_value;
60 if ((error = iter.has_next_field().get(has_value) )) { return *this; };
61 return *this;
62}
63SIMDJSON_POP_DISABLE_WARNINGS
64
65//
66// ### Live States
67//
68// While iterating or looking up values, depth >= iter.depth. at_start may vary. Error is
69// always SUCCESS:
70//
71// - Start: This is the state when the object is first found and the iterator is just past the {.
72// In this state, at_start == true.
73// - Next: After we hand a scalar value to the user, or an array/object which they then fully
74// iterate over, the iterator is at the , or } before the next value. In this state,
75// depth == iter.depth, at_start == false, and error == SUCCESS.
76// - Unfinished Business: When we hand an array/object to the user which they do not fully
77// iterate over, we need to finish that iteration by skipping child values until we reach the
78// Next state. In this state, depth > iter.depth, at_start == false, and error == SUCCESS.
79//
80// ## Error States
81//
82// In error states, we will yield exactly one more value before stopping. iter.depth == depth
83// and at_start is always false. We decrement after yielding the error, moving to the Finished
84// state.
85//
86// - Chained Error: When the object iterator is part of an error chain--for example, in
87// `for (auto tweet : doc["tweets"])`, where the tweet field may be missing or not be an
88// object--we yield that error in the loop, exactly once. In this state, error != SUCCESS and
89// iter.depth == depth, and at_start == false. We decrement depth when we yield the error.
90// - Missing Comma Error: When the iterator ++ method discovers there is no comma between fields,
91// we flag that as an error and treat it exactly the same as a Chained Error. In this state,
92// error == TAPE_ERROR, iter.depth == depth, and at_start == false.
93//
94// Errors that occur while reading a field to give to the user (such as when the key is not a
95// string or the field is missing a colon) are yielded immediately. Depth is then decremented,
96// moving to the Finished state without transitioning through an Error state at all.
97//
98// ## Terminal State
99//
100// The terminal state has iter.depth < depth. at_start is always false.
101//
102// - Finished: When we have reached a }, we are finished. We signal this by decrementing depth.
103// In this state, iter.depth < depth, at_start == false, and error == SUCCESS.
104//
105
106} // namespace ondemand
107} // namespace SIMDJSON_IMPLEMENTATION
108} // namespace simdjson
109
110namespace simdjson {
111
112simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>::simdjson_result(
113 SIMDJSON_IMPLEMENTATION::ondemand::object_iterator &&value
114) noexcept
115 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>(value))
116{
117 first.iter.assert_is_valid();
118}
119simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>::simdjson_result(error_code error) noexcept
120 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>({}, error)
121{
122}
123
124simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>::operator*() noexcept {
125 if (error()) { return error(); }
126 return *first;
127}
128// If we're iterating and there is an error, return the error once.
129simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>::operator==(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> &other) const noexcept {
130 if (!first.iter.is_valid()) { return !error(); }
131 return first == other.first;
132}
133// If we're iterating and there is an error, return the error once.
134simdjson_inline bool simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>::operator!=(const simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> &other) const noexcept {
135 if (!first.iter.is_valid()) { return error(); }
136 return first != other.first;
137}
138// Checks for ']' and ','
139simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> &simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator>::operator++() noexcept {
140 // Clear the error if there is one, so we don't yield it twice
141 if (error()) { second = SUCCESS; return *this; }
142 ++first;
143 return *this;
144}
145
146} // namespace simdjson
147
148#endif // SIMDJSON_GENERIC_ONDEMAND_OBJECT_ITERATOR_INL_H
simdjson_inline object_iterator() noexcept=default
Create a new invalid object_iterator.
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
error_code
All possible errors returned by simdjson.
Definition error.h:19
@ SUCCESS
No error.
Definition error.h:20