1#ifndef SIMDJSON_GENERIC_ONDEMAND_OBJECT_INL_H
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"
20namespace SIMDJSON_IMPLEMENTATION {
25 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(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());
30 return value(iter.child());
34 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(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());
39 return value(iter.child());
42 return find_field_unordered(key);
44simdjson_inline simdjson_result<value> object::operator[](
const std::string_view key) &&
noexcept {
45 return std::forward<object>(*this).find_field_unordered(key);
49 SIMDJSON_TRY( iter.find_field_raw(key).get(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());
54 return value(iter.child());
58 SIMDJSON_TRY( iter.find_field_raw(key).get(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());
63 return value(iter.child());
67 SIMDJSON_TRY( iter.start_object().error() );
70simdjson_inline simdjson_result<object> object::start_root(value_iterator &iter)
noexcept {
71 SIMDJSON_TRY( iter.start_root_object().error() );
75 if(iter.is_at_key()) {
90 auto error = iter.field_key().get(actual_key);
91 if (error) { iter.abandon();
return error; };
93 if ((error = iter.field_value())) { iter.abandon();
return error; }
95 auto error_skip = iter.json_iter().skip_child(iter.depth()-1);
96 if(error_skip) { iter.abandon(); }
101 const uint8_t * starting_point{iter.peek_start()};
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));
109 SIMDJSON_TRY( iter.started_object().error() );
113simdjson_inline
object object::resume(
const value_iterator &iter)
noexcept {
117simdjson_inline
object::object(
const value_iterator &_iter) noexcept
123#if SIMDJSON_DEVELOPMENT_CHECKS
134 json_pointer = json_pointer.substr(1);
135 size_t slash = json_pointer.find(
'/');
136 std::string_view key = json_pointer.substr(0, slash);
141 size_t escape = key.find(
'~');
142 if (escape != std::string_view::npos) {
144 std::string unescaped(key);
146 switch (unescaped[escape+1]) {
148 unescaped.replace(escape, 2,
"~");
151 unescaped.replace(escape, 2,
"/");
156 escape = unescaped.find(
'~', escape+1);
157 }
while (escape != std::string::npos);
158 child = find_field(unescaped);
160 child = find_field(key);
166 if (slash != std::string_view::npos) {
167 child = child.at_pointer(json_pointer.substr(slash));
174 if (json_pointer ==
"-1") {
177 return at_pointer(json_pointer);
180#if SIMDJSON_SUPPORTS_CONCEPTS
181template <
typename Func>
182 requires std::invocable<Func, value>
184template <
typename Func>
187 auto result_pair = get_next_key_and_json_path(json_path);
188 std::string_view key = result_pair.first;
189 std::string_view remaining_path = result_pair.second;
193 for (
auto field : *
this) {
196 if (remaining_path.empty()) {
205 SIMDJSON_TRY(find_field(key).get(val));
207 if (remaining_path.empty()) {
219 for(simdjson_unused
auto v : *
this) { count++; }
221 if(iter.error()) {
return iter.
error(); }
230 auto error = iter.reset_object().get(is_not_empty);
231 if(error) {
return error; }
232 return !is_not_empty;
236 return iter.reset_object();
239#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION
241template<constevalutil::fixed_string... FieldNames,
typename T>
242 requires(std::is_class_v<T> && (
sizeof...(FieldNames) > 0))
243simdjson_warn_unused simdjson_inline
error_code object::extract_into(T& out) &
noexcept {
245 auto should_extract = [](std::string_view field_name)
constexpr ->
bool {
246 return ((FieldNames.view() == field_name) || ...);
250 template for (
constexpr auto mem : std::define_static_array(
251 std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()))) {
253 if constexpr (!std::meta::is_const(mem) && std::meta::is_public(mem)) {
254 constexpr std::string_view key = std::define_static_string(std::meta::identifier_of(mem));
257 if constexpr (should_extract(key)) {
259 if constexpr (concepts::optional_type<
decltype(out.[:mem:])>) {
261 auto field_result = find_field_unordered(key);
262 if (!field_result.error()) {
263 auto error = field_result.get(out.[:mem:]);
268 return field_result.error();
274 SIMDJSON_TRY((*
this)[key].get(out.[:mem:]));
291simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept
292 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(value)) {}
293simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(error_code error) noexcept
294 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(error) {}
296simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::begin() noexcept {
297 if (error()) {
return error(); }
298 return first.begin();
300simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::end() noexcept {
301 if (error()) {
return error(); }
304simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) &
noexcept {
305 if (error()) {
return error(); }
306 return first.find_field_unordered(key);
308simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) &&
noexcept {
309 if (error()) {
return error(); }
310 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field_unordered(key);
312simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) &
noexcept {
313 if (error()) {
return error(); }
316simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) &&
noexcept {
317 if (error()) {
return error(); }
318 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first)[key];
320simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) &
noexcept {
321 if (error()) {
return error(); }
322 return first.find_field(key);
324simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) &&
noexcept {
325 if (error()) {
return error(); }
326 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field(key);
329simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_pointer(std::string_view json_pointer)
noexcept {
330 if (error()) {
return error(); }
331 return first.at_pointer(json_pointer);
334simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_path(
335 std::string_view json_path)
noexcept {
339 return first.at_path(json_path);
342#if SIMDJSON_SUPPORTS_CONCEPTS
343template <
typename Func>
344 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
346template <
typename Func>
348simdjson_inline
error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback)
noexcept {
349 if (error()) {
return error(); }
350 return first.for_each_at_path_with_wildcard(json_path, std::forward<Func>(callback));
353inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::reset() noexcept {
354 if (error()) {
return error(); }
355 return first.reset();
358inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::is_empty() noexcept {
359 if (error()) {
return error(); }
360 return first.is_empty();
363simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::count_fields() &
noexcept {
364 if (error()) {
return error(); }
365 return first.count_fields();
368simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::raw_json() noexcept {
369 if (error()) {
return error(); }
370 return first.raw_json();
A JSON field (key/value pair) in an object.
simdjson_inline ondemand::value & value() &noexcept
Get the field value.
A forward-only JSON object field iterator.
simdjson_inline object() noexcept=default
Create a new invalid object.
error_code for_each_at_path_with_wildcard(std::string_view json_path, Func &&callback) noexcept
Call the provided callback for each value matching the given JSONPath expression with wildcard suppor...
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.
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 ...
simdjson_warn_unused simdjson_inline error_code consume() noexcept
Go to the end of the object, no matter where you are right now.
simdjson_inline simdjson_result< value > find_field(std::string_view key) &noexcept
Look up a field by name on an object (order-sensitive).
simdjson_result< bool > is_empty() &noexcept
This method scans the beginning of the object and checks whether the object is empty.
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the object.
simdjson_result< value > at_path(std::string_view json_path) noexcept
Get the value associated with the given JSONPath expression.
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
simdjson_inline simdjson_result< object_iterator > begin() noexcept
Get an iterator to the start of the object.
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
simdjson_inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func &&callback) noexcept
Call the provided callback for each value matching the given JSONPath expression with wildcard suppor...
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
@ OUT_OF_ORDER_ITERATION
tried to iterate an array or object out of order (checked when SIMDJSON_DEVELOPMENT_CHECKS=1)
@ NO_SUCH_FIELD
JSON field not found in object.
@ INVALID_JSON_POINTER
Invalid JSON pointer syntax.
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.
simdjson_inline error_code error() const noexcept
The error.