simdjson 4.0.7
Ridiculously Fast JSON
Loading...
Searching...
No Matches
array-inl.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_ARRAY_INL_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_ARRAY_INL_H
5#include "simdjson/jsonpathutil.h"
6#include "simdjson/generic/ondemand/base.h"
7#include "simdjson/generic/ondemand/array.h"
8#include "simdjson/generic/ondemand/array_iterator-inl.h"
9#include "simdjson/generic/ondemand/json_iterator.h"
10#include "simdjson/generic/ondemand/value.h"
11#include "simdjson/generic/ondemand/value_iterator-inl.h"
12#endif // SIMDJSON_CONDITIONAL_INCLUDE
13
14namespace simdjson {
15namespace SIMDJSON_IMPLEMENTATION {
16namespace ondemand {
17
18//
19// ### Live States
20//
21// While iterating or looking up values, depth >= iter->depth. at_start may vary. Error is
22// always SUCCESS:
23//
24// - Start: This is the state when the array is first found and the iterator is just past the `{`.
25// In this state, at_start == true.
26// - Next: After we hand a scalar value to the user, or an array/object which they then fully
27// iterate over, the iterator is at the `,` before the next value (or `]`). In this state,
28// depth == iter->depth, at_start == false, and error == SUCCESS.
29// - Unfinished Business: When we hand an array/object to the user which they do not fully
30// iterate over, we need to finish that iteration by skipping child values until we reach the
31// Next state. In this state, depth > iter->depth, at_start == false, and error == SUCCESS.
32//
33// ## Error States
34//
35// In error states, we will yield exactly one more value before stopping. iter->depth == depth
36// and at_start is always false. We decrement after yielding the error, moving to the Finished
37// state.
38//
39// - Chained Error: When the array iterator is part of an error chain--for example, in
40// `for (auto tweet : doc["tweets"])`, where the tweet element may be missing or not be an
41// array--we yield that error in the loop, exactly once. In this state, error != SUCCESS and
42// iter->depth == depth, and at_start == false. We decrement depth when we yield the error.
43// - Missing Comma Error: When the iterator ++ method discovers there is no comma between elements,
44// we flag that as an error and treat it exactly the same as a Chained Error. In this state,
45// error == TAPE_ERROR, iter->depth == depth, and at_start == false.
46//
47// ## Terminal State
48//
49// The terminal state has iter->depth < depth. at_start is always false.
50//
51// - Finished: When we have reached a `]` or have reported an error, we are finished. We signal this
52// by decrementing depth. In this state, iter->depth < depth, at_start == false, and
53// error == SUCCESS.
54//
55
56simdjson_inline array::array(const value_iterator &_iter) noexcept
57 : iter{_iter}
58{
59}
60
61simdjson_inline simdjson_result<array> array::start(value_iterator &iter) noexcept {
62 // We don't need to know if the array is empty to start iteration, but we do want to know if there
63 // is an error--thus `simdjson_unused`.
64 simdjson_unused bool has_value;
65 SIMDJSON_TRY( iter.start_array().get(has_value) );
66 return array(iter);
67}
68simdjson_inline simdjson_result<array> array::start_root(value_iterator &iter) noexcept {
69 simdjson_unused bool has_value;
70 SIMDJSON_TRY( iter.start_root_array().get(has_value) );
71 return array(iter);
72}
73simdjson_inline simdjson_result<array> array::started(value_iterator &iter) noexcept {
74 bool has_value;
75 SIMDJSON_TRY(iter.started_array().get(has_value));
76 return array(iter);
77}
78
80#if SIMDJSON_DEVELOPMENT_CHECKS
81 if (!iter.is_at_iterator_start()) { return OUT_OF_ORDER_ITERATION; }
82#endif
83 return array_iterator(iter);
84}
88simdjson_warn_unused simdjson_warn_unused simdjson_inline error_code array::consume() noexcept {
89 auto error = iter.json_iter().skip_child(iter.depth()-1);
90 if(error) { iter.abandon(); }
91 return error;
92}
93
95 const uint8_t * starting_point{iter.peek_start()};
96 auto error = consume();
97 if(error) { return error; }
98 // After 'consume()', we could be left pointing just beyond the document, but that
99 // is ok because we are not going to dereference the final pointer position, we just
100 // use it to compute the length in bytes.
101 const uint8_t * final_point{iter._json_iter->unsafe_pointer()};
102 return std::string_view(reinterpret_cast<const char*>(starting_point), size_t(final_point - starting_point));
103}
104
105SIMDJSON_PUSH_DISABLE_WARNINGS
106SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING
107simdjson_inline simdjson_result<size_t> array::count_elements() & noexcept {
108 size_t count{0};
109 // Important: we do not consume any of the values.
110 for(simdjson_unused auto v : *this) { count++; }
111 // The above loop will always succeed, but we want to report errors.
112 if(iter.error()) { return iter.error(); }
113 // We need to move back at the start because we expect users to iterate through
114 // the array after counting the number of elements.
115 iter.reset_array();
116 return count;
117}
118SIMDJSON_POP_DISABLE_WARNINGS
119
120simdjson_inline simdjson_result<bool> array::is_empty() & noexcept {
121 bool is_not_empty;
122 auto error = iter.reset_array().get(is_not_empty);
123 if(error) { return error; }
124 return !is_not_empty;
125}
126
128 return iter.reset_array();
129}
130
131inline simdjson_result<value> array::at_pointer(std::string_view json_pointer) noexcept {
132 if (json_pointer[0] != '/') { return INVALID_JSON_POINTER; }
133 json_pointer = json_pointer.substr(1);
134 // - means "the append position" or "the element after the end of the array"
135 // We don't support this, because we're returning a real element, not a position.
136 if (json_pointer == "-") { return INDEX_OUT_OF_BOUNDS; }
137
138 // Read the array index
139 size_t array_index = 0;
140 size_t i;
141 for (i = 0; i < json_pointer.length() && json_pointer[i] != '/'; i++) {
143 // Check for non-digit in array index. If it's there, we're trying to get a field in an object
144 if (digit > 9) { return INCORRECT_TYPE; }
146 }
147
148 // 0 followed by other digits is invalid
149 if (i > 1 && json_pointer[0] == '0') { return INVALID_JSON_POINTER; } // "JSON pointer array index has other characters after 0"
150
151 // Empty string is invalid; so is a "/" with no digits before it
152 if (i == 0) { return INVALID_JSON_POINTER; } // "Empty string in JSON pointer array index"
153 // Get the child
154 auto child = at(array_index);
155 // If there is an error, it ends here
156 if(child.error()) {
157 return child;
158 }
159
160 // If there is a /, we're not done yet, call recursively.
161 if (i < json_pointer.length()) {
162 child = child.at_pointer(json_pointer.substr(i));
163 }
164 return child;
165}
166
167inline simdjson_result<value> array::at_path(std::string_view json_path) noexcept {
169 if (json_pointer == "-1") { return INVALID_JSON_POINTER; }
170 return at_pointer(json_pointer);
171}
172
173simdjson_inline simdjson_result<value> array::at(size_t index) noexcept {
174 size_t i = 0;
175 for (auto value : *this) {
176 if (i == index) { return value; }
177 i++;
178 }
179 return INDEX_OUT_OF_BOUNDS;
180}
181
182} // namespace ondemand
183} // namespace SIMDJSON_IMPLEMENTATION
184} // namespace simdjson
185
186namespace simdjson {
187
188simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::simdjson_result(
189 SIMDJSON_IMPLEMENTATION::ondemand::array &&value
190) noexcept
191 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array>(
192 std::forward<SIMDJSON_IMPLEMENTATION::ondemand::array>(value)
193 )
194{
195}
196simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::simdjson_result(
197 error_code error
198) noexcept
199 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::array>(error)
200{
201}
202
203simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::begin() noexcept {
204 if (error()) { return error(); }
205 return first.begin();
206}
207simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::end() noexcept {
208 if (error()) { return error(); }
209 return first.end();
210}
211simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::count_elements() & noexcept {
212 if (error()) { return error(); }
213 return first.count_elements();
214}
215simdjson_inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::is_empty() & noexcept {
216 if (error()) { return error(); }
217 return first.is_empty();
218}
219simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::at(size_t index) noexcept {
220 if (error()) { return error(); }
221 return first.at(index);
222}
223simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::at_pointer(std::string_view json_pointer) noexcept {
224 if (error()) { return error(); }
225 return first.at_pointer(json_pointer);
226}
227simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::at_path(std::string_view json_path) noexcept {
228 if (error()) { return error(); }
229 return first.at_path(json_path);
230}
231simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array>::raw_json() noexcept {
232 if (error()) { return error(); }
233 return first.raw_json();
234}
235} // namespace simdjson
236
237#endif // SIMDJSON_GENERIC_ONDEMAND_ARRAY_INL_H
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index.
Definition array-inl.h:173
value_iterator iter
Iterator marking current position.
Definition array.h:212
simdjson_inline simdjson_result< bool > is_empty() &noexcept
This method scans the beginning of the array and checks whether the array is empty.
Definition array-inl.h:120
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
Definition array-inl.h:131
static simdjson_inline simdjson_result< array > start(value_iterator &iter) noexcept
Begin array iteration.
Definition array-inl.h:61
simdjson_inline simdjson_result< array_iterator > begin() noexcept
Begin array iteration.
Definition array-inl.h:79
simdjson_inline array() noexcept=default
Create a new invalid array.
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
Definition array-inl.h:107
simdjson_result< value > at_path(std::string_view json_path) noexcept
Get the value associated with the given JSONPath expression.
Definition array-inl.h:167
static simdjson_inline simdjson_result< array > start_root(value_iterator &iter) noexcept
Begin array iteration from the root.
Definition array-inl.h:68
static simdjson_inline simdjson_result< array > started(value_iterator &iter) noexcept
Begin array iteration.
Definition array-inl.h:73
simdjson_warn_unused simdjson_inline error_code consume() noexcept
Go to the end of the array, no matter where you are right now.
Definition array-inl.h:88
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Consumes the array and returns a string_view instance corresponding to the array as represented in JS...
Definition array-inl.h:94
simdjson_inline simdjson_result< array_iterator > end() noexcept
Sentinel representing the end of the array.
Definition array-inl.h:85
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the array.
Definition array-inl.h:127
An ephemeral JSON value returned during iteration.
Definition value.h:21
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
@ INCORRECT_TYPE
JSON element has a different type than user expected.
Definition error.h:37
@ OUT_OF_ORDER_ITERATION
tried to iterate an array or object out of order (checked when SIMDJSON_DEVELOPMENT_CHECKS=1)
Definition error.h:46
@ INDEX_OUT_OF_BOUNDS
JSON array index too large.
Definition error.h:39
@ INVALID_JSON_POINTER
Invalid JSON pointer syntax.
Definition error.h:42
std::string json_path_to_pointer_conversion(std::string_view json_path)
Converts JSONPath to JSON Pointer.