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#if SIMDJSON_STATIC_REFLECTION
13#include "simdjson/generic/ondemand/json_string_builder.h"
19namespace SIMDJSON_IMPLEMENTATION {
24 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(has_value) );
26 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
29 return value(iter.child());
33 SIMDJSON_TRY( iter.find_field_unordered_raw(key).get(has_value) );
35 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
38 return value(iter.child());
41 return find_field_unordered(key);
43simdjson_inline simdjson_result<value> object::operator[](
const std::string_view key) &&
noexcept {
44 return std::forward<object>(*this).find_field_unordered(key);
48 SIMDJSON_TRY( iter.find_field_raw(key).get(has_value) );
50 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
53 return value(iter.child());
57 SIMDJSON_TRY( iter.find_field_raw(key).get(has_value) );
59 logger::log_line(iter.json_iter(),
"ERROR: ",
"Cannot find key %.*s",
"", -1, 0, logger::log_level::error,
static_cast<int>(key.size()), key.data());
62 return value(iter.child());
66 SIMDJSON_TRY( iter.start_object().error() );
69simdjson_inline simdjson_result<object> object::start_root(value_iterator &iter)
noexcept {
70 SIMDJSON_TRY( iter.start_root_object().error() );
74 if(iter.is_at_key()) {
90 if (error) { iter.abandon();
return error; };
92 if ((error = iter.field_value())) { iter.abandon();
return error; }
94 auto error_skip = iter.json_iter().skip_child(iter.depth()-1);
102 if(error) {
return error; }
108 SIMDJSON_TRY( iter.started_object().error() );
112simdjson_inline
object object::resume(
const value_iterator &iter)
noexcept {
116simdjson_inline
object::object(
const value_iterator &_iter) noexcept
121simdjson_inline simdjson_result<object_iterator> object::begin() noexcept {
122#if SIMDJSON_DEVELOPMENT_CHECKS
125 return object_iterator(iter);
127simdjson_inline simdjson_result<object_iterator> object::end() noexcept {
128 return object_iterator(iter);
140 size_t escape = key.find(
'~');
141 if (
escape != std::string_view::npos) {
156 }
while (
escape != std::string::npos);
159 child = find_field(key);
165 if (
slash != std::string_view::npos) {
182 for(simdjson_unused
auto v : *
this) { count++; }
184 if(iter.error()) {
return iter.error(); }
194 if(error) {
return error; }
199 return iter.reset_object();
202#if SIMDJSON_SUPPORTS_CONCEPTS && SIMDJSON_STATIC_REFLECTION
204template<constevalutil::fixed_string...
FieldNames,
typename T>
205 requires(std::is_class_v<T> && (
sizeof...(FieldNames) > 0))
206simdjson_warn_unused simdjson_inline
error_code object::extract_into(
T&
out) &
noexcept {
213 template for (
constexpr auto mem : std::define_static_array(
214 std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()))) {
216 if constexpr (!std::meta::is_const(mem) && std::meta::is_public(mem)) {
217 constexpr std::string_view key = std::define_static_string(std::meta::identifier_of(mem));
220 if constexpr (should_extract(key)) {
222 if constexpr (concepts::optional_type<
decltype(out.[:mem:])>) {
224 auto field_result = find_field_unordered(key);
225 if (!field_result.error()) {
226 auto error = field_result.get(out.[:mem:]);
231 return field_result.error();
237 SIMDJSON_TRY((*
this)[key].get(out.[:mem:]));
254simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::object &&value) noexcept
255 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(value)) {}
256simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::simdjson_result(error_code error) noexcept
257 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::object>(error) {}
259simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::begin() noexcept {
260 if (error()) {
return error(); }
261 return first.begin();
263simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object_iterator> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::end() noexcept {
264 if (error()) {
return error(); }
267simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) &
noexcept {
268 if (error()) {
return error(); }
269 return first.find_field_unordered(key);
271simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field_unordered(std::string_view key) &&
noexcept {
272 if (error()) {
return error(); }
273 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field_unordered(key);
275simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) &
noexcept {
276 if (error()) {
return error(); }
279simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::operator[](std::string_view key) &&
noexcept {
280 if (error()) {
return error(); }
281 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first)[key];
283simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) &
noexcept {
284 if (error()) {
return error(); }
285 return first.find_field(key);
287simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::find_field(std::string_view key) &&
noexcept {
288 if (error()) {
return error(); }
289 return std::forward<SIMDJSON_IMPLEMENTATION::ondemand::object>(first).find_field(key);
292simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_pointer(std::string_view json_pointer)
noexcept {
293 if (error()) {
return error(); }
294 return first.at_pointer(json_pointer);
297simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::at_path(
298 std::string_view json_path)
noexcept {
302 return first.at_path(json_path);
305inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::reset() noexcept {
306 if (error()) {
return error(); }
307 return first.reset();
310inline simdjson_result<bool> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::is_empty() noexcept {
311 if (error()) {
return error(); }
312 return first.is_empty();
315simdjson_inline simdjson_result<size_t> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::count_fields() &
noexcept {
316 if (error()) {
return error(); }
317 return first.count_fields();
320simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object>::raw_json() noexcept {
321 if (error()) {
return error(); }
322 return first.raw_json();
A forward-only JSON array.
simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
A forward-only JSON object field iterator.
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.
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< 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.
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.