simdjson 4.2.3
Ridiculously Fast JSON
Loading...
Searching...
No Matches
object-inl.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_OBJECT_INL_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_OBJECT_INL_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/ondemand/field.h"
7#include "simdjson/generic/ondemand/object.h"
8#include "simdjson/generic/ondemand/object_iterator.h"
9#include "simdjson/generic/ondemand/raw_json_string.h"
10#include "simdjson/generic/ondemand/json_iterator.h"
11#include "simdjson/generic/ondemand/value-inl.h"
12#include "simdjson/jsonpathutil.h"
13#if SIMDJSON_STATIC_REFLECTION
14#include "simdjson/generic/ondemand/json_string_builder.h" // for constevalutil::fixed_string
15#include <meta>
16#endif
17#endif // SIMDJSON_CONDITIONAL_INCLUDE
18
19namespace simdjson {
20namespace SIMDJSON_IMPLEMENTATION {
21namespace ondemand {
22
23simdjson_inline simdjson_result<value> object::find_field_unordered(const std::string_view key) & noexcept {
24 bool has_value;
25 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(has_value) );
26 if (!has_value) {
27 logger::log_line(iter.json_iter(), "ERROR: ", "Cannot find key %.*s", "", -1, 0, logger::log_level::error, static_cast<int>(key.size()), key.data());
28 return NO_SUCH_FIELD;
29 }
30 return value(iter.child());
31}
32simdjson_inline simdjson_result<value> object::find_field_unordered(const std::string_view key) && noexcept {
33 bool has_value;
34 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(has_value) );
35 if (!has_value) {
36 logger::log_line(iter.json_iter(), "ERROR: ", "Cannot find key %.*s", "", -1, 0, logger::log_level::error, static_cast<int>(key.size()), key.data());
37 return NO_SUCH_FIELD;
38 }
39 return value(iter.child());
40}
41simdjson_inline simdjson_result<value> object::operator[](const std::string_view key) & noexcept {
42 return find_field_unordered(key);
43}
44simdjson_inline simdjson_result<value> object::operator[](const std::string_view key) && noexcept {
45 return std::forward<object>(*this).find_field_unordered(key);
46}
47simdjson_inline simdjson_result<value> object::find_field(const std::string_view key) & noexcept {
48 bool has_value;
49 SIMDJSON_TRY( iter.find_field_raw(key).get(has_value) );
50 if (!has_value) {
51 logger::log_line(iter.json_iter(), "ERROR: ", "Cannot find key %.*s", "", -1, 0, logger::log_level::error, static_cast<int>(key.size()), key.data());
52 return NO_SUCH_FIELD;
53 }
54 return value(iter.child());
55}
56simdjson_inline simdjson_result<value> object::find_field(const std::string_view key) && noexcept {
57 bool has_value;
58 SIMDJSON_TRY( iter.find_field_raw(key).get(has_value) );
59 if (!has_value) {
60 logger::log_line(iter.json_iter(), "ERROR: ", "Cannot find key %.*s", "", -1, 0, logger::log_level::error, static_cast<int>(key.size()), key.data());
61 return NO_SUCH_FIELD;
62 }
63 return value(iter.child());
64}
65
66simdjson_inline simdjson_result<object> object::start(value_iterator &iter) noexcept {
67 SIMDJSON_TRY( iter.start_object().error() );
68 return object(iter);
69}
70simdjson_inline simdjson_result<object> object::start_root(value_iterator &iter) noexcept {
71 SIMDJSON_TRY( iter.start_root_object().error() );
72 return object(iter);
73}
74simdjson_warn_unused simdjson_inline error_code object::consume() noexcept {
75 if(iter.is_at_key()) {
89 simdjson_unused raw_json_string actual_key;
90 auto error = iter.field_key().get(actual_key);
91 if (error) { iter.abandon(); return error; };
92 // Let us move to the value while we are at it.
93 if ((error = iter.field_value())) { iter.abandon(); return error; }
94 }
95 auto error_skip = iter.json_iter().skip_child(iter.depth()-1);
96 if(error_skip) { iter.abandon(); }
97 return error_skip;
98}
99
101 const uint8_t * starting_point{iter.peek_start()};
102 auto error = consume();
103 if(error) { return error; }
104 const uint8_t * final_point{iter._json_iter->peek()};
105 return std::string_view(reinterpret_cast<const char*>(starting_point), size_t(final_point - starting_point));
106}
107
108simdjson_inline simdjson_result<object> object::started(value_iterator &iter) noexcept {
109 SIMDJSON_TRY( iter.started_object().error() );
110 return object(iter);
111}
112
113simdjson_inline object object::resume(const value_iterator &iter) noexcept {
114 return iter;
115}
116
117simdjson_inline object::object(const value_iterator &_iter) noexcept
118 : iter{_iter}
119{
120}
121
122simdjson_inline simdjson_result<object_iterator> object::begin() noexcept {
123#if SIMDJSON_DEVELOPMENT_CHECKS
124 if (!iter.is_at_iterator_start()) { return OUT_OF_ORDER_ITERATION; }
125#endif
126 return object_iterator(iter);
127}
128simdjson_inline simdjson_result<object_iterator> object::end() noexcept {
129 return object_iterator(iter);
130}
131
132inline simdjson_result<value> object::at_pointer(std::string_view json_pointer) noexcept {
133 if (json_pointer[0] != '/') { return INVALID_JSON_POINTER; }
134 json_pointer = json_pointer.substr(1);
135 size_t slash = json_pointer.find('/');
136 std::string_view key = json_pointer.substr(0, slash);
137 // Grab the child with the given key
139
140 // If there is an escape character in the key, unescape it and then get the child.
141 size_t escape = key.find('~');
142 if (escape != std::string_view::npos) {
143 // Unescape the key
144 std::string unescaped(key);
145 do {
146 switch (unescaped[escape+1]) {
147 case '0':
148 unescaped.replace(escape, 2, "~");
149 break;
150 case '1':
151 unescaped.replace(escape, 2, "/");
152 break;
153 default:
154 return INVALID_JSON_POINTER; // "Unexpected ~ escape character in JSON pointer");
155 }
156 escape = unescaped.find('~', escape+1);
157 } while (escape != std::string::npos);
158 child = find_field(unescaped); // Take note find_field does not unescape keys when matching
159 } else {
160 child = find_field(key);
161 }
162 if(child.error()) {
163 return child; // we do not continue if there was an error
164 }
165 // If there is a /, we have to recurse and look up more of the path
166 if (slash != std::string_view::npos) {
167 child = child.at_pointer(json_pointer.substr(slash));
168 }
169 return child;
170}
171
172inline simdjson_result<value> object::at_path(std::string_view json_path) noexcept {
173 auto json_pointer = json_path_to_pointer_conversion(json_path);
174 if (json_pointer == "-1") {
176 }
177 return at_pointer(json_pointer);
178}
179
180inline simdjson_result<std::vector<value>> object::at_path_with_wildcard(std::string_view json_path) noexcept {
181 std::vector<value> result;
182
183 auto result_pair = get_next_key_and_json_path(json_path);
184 std::string_view key = result_pair.first;
185 std::string_view remaining_path = result_pair.second;
186 // Handle when its the case for wildcard
187 if (key == "*") {
188 // Loop through each field in the object
189 for (auto field : *this) {
190 value val;
191 SIMDJSON_TRY(field.value().get(val));
192
193 if (remaining_path.empty()) {
194 result.push_back(std::move(val));
195 } else {
196 auto nested_result = val.at_path_with_wildcard(remaining_path);
197
198 if (nested_result.error()) {
199 return nested_result.error();
200 }
201 // Extract and append all nested matches to our result
202 std::vector<value> nested_vec;
203 SIMDJSON_TRY(std::move(nested_result).get(nested_vec));
204
205 result.insert(result.end(),
206 std::make_move_iterator(nested_vec.begin()),
207 std::make_move_iterator(nested_vec.end()));
208 }
209 }
210 return result;
211 } else {
212 value val;
213 SIMDJSON_TRY(find_field(key).get(val));
214
215 if (remaining_path.empty()) {
216 result.push_back(std::move(val));
217 return result;
218 } else {
219 return val.at_path_with_wildcard(remaining_path);
220 }
221 }
222}
223
224simdjson_inline simdjson_result<size_t> object::count_fields() & noexcept {
225 size_t count{0};
226 // Important: we do not consume any of the values.
227 for(simdjson_unused auto v : *this) { count++; }
228 // The above loop will always succeed, but we want to report errors.
229 if(iter.error()) { return iter.error(); }
230 // We need to move back at the start because we expect users to iterate through
231 // the object after counting the number of elements.
232 iter.reset_object();
233 return count;
234}
235
236simdjson_inline simdjson_result<bool> object::is_empty() & noexcept {
237 bool is_not_empty;
238 auto error = iter.reset_object().get(is_not_empty);
239 if(error) { return error; }
240 return !is_not_empty;
241}
242
243simdjson_inline simdjson_result<bool> object::reset() & noexcept {
244 return iter.reset_object();
245}
246
247#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION
248
249template<constevalutil::fixed_string... FieldNames, typename T>
250 requires(std::is_class_v<T> && (sizeof...(FieldNames) > 0))
251simdjson_warn_unused simdjson_inline error_code object::extract_into(T& out) & noexcept {
252 // Helper to check if a field name matches any of the requested fields
253 auto should_extract = [](std::string_view field_name) constexpr -> bool {
254 return ((FieldNames.view() == field_name) || ...);
255 };
256
257 // Iterate through all members of T using reflection
258 template for (constexpr auto mem : std::define_static_array(
259 std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()))) {
260
261 if constexpr (!std::meta::is_const(mem) && std::meta::is_public(mem)) {
262 constexpr std::string_view key = std::define_static_string(std::meta::identifier_of(mem));
263
264 // Only extract this field if it's in our list of requested fields
265 if constexpr (should_extract(key)) {
266 // Try to find and extract the field
267 if constexpr (concepts::optional_type<decltype(out.[:mem:])>) {
268 // For optional fields, it's ok if they're missing
269 auto field_result = find_field_unordered(key);
270 if (!field_result.error()) {
271 auto error = field_result.get(out.[:mem:]);
272 if (error && error != NO_SUCH_FIELD) {
273 return error;
274 }
275 } else if (field_result.error() != NO_SUCH_FIELD) {
276 return field_result.error();
277 } else {
278 out.[:mem:].reset();
279 }
280 } else {
281 // For required fields (in the requested list), fail if missing
282 SIMDJSON_TRY((*this)[key].get(out.[:mem:]));
283 }
284 }
285 }
286 };
287
288 return SUCCESS;
289}
290
291#endif // SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION
292
293} // namespace ondemand
294} // namespace SIMDJSON_IMPLEMENTATION
295} // namespace simdjson
296
297namespace simdjson {
298
299simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept
300 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(value)) {}
301simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(error_code error) noexcept
302 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(error) {}
303
304simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::begin() noexcept {
305 if (error()) { return error(); }
306 return first.begin();
307}
308simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::end() noexcept {
309 if (error()) { return error(); }
310 return first.end();
311}
312simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) & noexcept {
313 if (error()) { return error(); }
314 return first.find_field_unordered(key);
315}
316simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) && noexcept {
317 if (error()) { return error(); }
318 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field_unordered(key);
319}
320simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) & noexcept {
321 if (error()) { return error(); }
322 return first[key];
323}
324simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) && noexcept {
325 if (error()) { return error(); }
326 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first)[key];
327}
328simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) & noexcept {
329 if (error()) { return error(); }
330 return first.find_field(key);
331}
332simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) && noexcept {
333 if (error()) { return error(); }
334 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field(key);
335}
336
337simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_pointer(std::string_view json_pointer) noexcept {
338 if (error()) { return error(); }
339 return first.at_pointer(json_pointer);
340}
341
342simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_path(
343 std::string_view json_path) noexcept {
344 if (error()) {
345 return error();
346 }
347 return first.at_path(json_path);
348}
349
350simdjson_inline simdjson_result<std::vector<SIMDJSON_IMPLEMENTATION::ondemand::value>> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_path_with_wildcard(std::string_view json_path) noexcept {
351 if (error()) { return error(); }
352 return first.at_path_with_wildcard(json_path);
353}
354
355inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::reset() noexcept {
356 if (error()) { return error(); }
357 return first.reset();
358}
359
360inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::is_empty() noexcept {
361 if (error()) { return error(); }
362 return first.is_empty();
363}
364
365simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::count_fields() & noexcept {
366 if (error()) { return error(); }
367 return first.count_fields();
368}
369
370simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::raw_json() noexcept {
371 if (error()) { return error(); }
372 return first.raw_json();
373}
374
375} // namespace simdjson
376
377#endif // SIMDJSON_GENERIC_ONDEMAND_OBJECT_INL_H
A JSON field (key/value pair) in an object.
Definition field.h:22
simdjson_inline ondemand::value & value() &noexcept
Get the field value.
Definition field-inl.h:67
A forward-only JSON object field iterator.
Definition object.h:21
simdjson_inline object() noexcept=default
Create a new invalid object.
simdjson_inline simdjson_result< value > find_field_unordered(std::string_view key) &noexcept
Look up a field by name on an object, without regard to key order.
Definition object-inl.h:23
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Consumes the object and returns a string_view instance corresponding to the object as represented in ...
Definition object-inl.h:100
simdjson_warn_unused simdjson_inline error_code consume() noexcept
Go to the end of the object, no matter where you are right now.
Definition object-inl.h:74
simdjson_inline simdjson_result< value > find_field(std::string_view key) &noexcept
Look up a field by name on an object (order-sensitive).
Definition object-inl.h:47
simdjson_result< std::vector< value > > at_path_with_wildcard(std::string_view json_path) noexcept
Get all values matching the given JSONPath expression with wildcard support.
Definition object-inl.h:180
simdjson_result< bool > is_empty() &noexcept
This method scans the beginning of the object and checks whether the object is empty.
Definition object-inl.h:236
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the object.
Definition object-inl.h:243
simdjson_result< value > at_path(std::string_view json_path) noexcept
Get the value associated with the given JSONPath expression.
Definition object-inl.h:172
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
Definition object-inl.h:132
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
Definition object-inl.h:224
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
Definition value.h:22
simdjson_inline simdjson_result< std::vector< value > > at_path_with_wildcard(std::string_view json_path) noexcept
Get all values matching the given JSONPath expression with wildcard support.
Definition value-inl.h:299
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
@ OUT_OF_ORDER_ITERATION
tried to iterate an array or object out of order (checked when SIMDJSON_DEVELOPMENT_CHECKS=1)
Definition error.h:46
@ NO_SUCH_FIELD
JSON field not found in object.
Definition error.h:40
@ SUCCESS
No error.
Definition error.h:20
@ 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.
The result of a simdjson operation that could fail.
Definition error.h:280
simdjson_inline error_code error() const noexcept
The error.
Definition error-inl.h:168