1#ifndef SIMDJSON_GENERIC_ONDEMAND_OBJECT_H
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_OBJECT_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/implementation_simdjson_result_base.h"
7#include "simdjson/generic/ondemand/value_iterator.h"
8#include "simdjson/generic/ondemand/key_selector.h"
10#if SIMDJSON_STATIC_REFLECTION && SIMDJSON_SUPPORTS_CONCEPTS
11#include "simdjson/generic/ondemand/json_string_builder.h"
16namespace SIMDJSON_IMPLEMENTATION {
19#if SIMDJSON_SUPPORTS_CONCEPTS
39struct [[nodiscard]] for_each_result {
41 std::size_t matched_count{0};
42 constexpr operator error_code() const noexcept {
return error; }
45namespace key_selector_for_each_detail {
57concept field_handler =
58 std::is_invocable_v<std::remove_reference_t<H>&, value> ||
59 ::simdjson::deserializable<std::remove_cvref_t<H>, value>;
68inline constexpr bool nothrow_field_handler_v =
69 std::is_invocable_v<std::remove_reference_t<H>&, value>
70 ? std::is_nothrow_invocable_v<std::remove_reference_t<H>&, value>
71 : ::simdjson::nothrow_deserializable<std::remove_cvref_t<H>, value>;
83template <
typename... Handlers>
84inline constexpr bool nothrow_field_handlers_v =
85 (nothrow_field_handler_v<Handlers> && ...);
118 token_position position{};
122 : position(position_), depth(depth_) {}
137 simdjson_inline
object() noexcept = default;
235#if SIMDJSON_SUPPORTS_CONCEPTS
277 template <
typename Selector,
typename Func>
278 requires key_selector_type<Selector> &&
279 std::is_invocable_v<Func&, std::size_t, value>
280 simdjson_inline for_each_result for_each(Func&& on_match)
281 noexcept(std::is_nothrow_invocable_v<Func&, std::size_t, value>);
307 template <
typename Selector,
typename... Handlers>
308 requires key_selector_type<Selector> &&
309 (
sizeof...(Handlers) == Selector::size()) &&
310 (key_selector_for_each_detail::field_handler<Handlers> && ...)
311 simdjson_inline for_each_result for_each(Handlers&&... on_match)
312 noexcept(key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
321 template <constevalutil::fixed_string... Keys, typename... Handlers>
322 requires (sizeof...(Handlers) == sizeof...(Keys)) &&
323 (sizeof...(Keys) >= 1) && (sizeof...(Keys) <= 255) &&
324 (key_selector_for_each_detail::field_handler<Handlers> && ...)
325 simdjson_inline for_each_result for_each(Handlers&&... on_match)
326 noexcept(key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
387#if SIMDJSON_SUPPORTS_CONCEPTS
388 template <
typename Func>
389 requires std::invocable<Func, value>
391 template <
typename Func>
468#if SIMDJSON_SUPPORTS_CONCEPTS
476 template <
typename T>
477 simdjson_warn_unused simdjson_inline
error_code get(T &out)
478 noexcept(nothrow_gettable<T, object>) {
479 static_assert(custom_deserializable<T, object>);
480 return deserialize(*
this, out);
488 template <
typename T>
490 noexcept(nothrow_gettable<T,
object>)
492 static_assert(std::is_default_constructible<T>::value,
"The specified type is not default constructible.");
494 SIMDJSON_TRY(get<T>(out));
498#if SIMDJSON_STATIC_REFLECTION
523 template<constevalutil::fixed_string... FieldNames,
typename T>
524 requires(std::is_class_v<T> && (
sizeof...(FieldNames) > 0))
525 simdjson_warn_unused simdjson_inline
error_code extract_into(T& out) &
noexcept;
533 static simdjson_inline
simdjson_result<
object> start(value_iterator &iter) noexcept;
534 static simdjson_inline
simdjson_result<
object> start_root(value_iterator &iter) noexcept;
535 static simdjson_inline
simdjson_result<
object> started(value_iterator &iter) noexcept;
536 static simdjson_inline
object resume(const value_iterator &iter) noexcept;
537 simdjson_inline
object(const value_iterator &iter) noexcept;
539 simdjson_warn_unused simdjson_inline
error_code find_field_raw(const std::string_view key) noexcept;
541 value_iterator iter{};
542#if SIMDJSON_DEVELOPMENT_CHECKS
544 simdjson_inline
void set_locked(
bool _locked)
noexcept;
548 friend class document;
549 friend struct simdjson_result<
object>;
550#if SIMDJSON_DEVELOPMENT_CHECKS
551 friend class object_iterator;
552 friend struct simdjson_result<object_iterator>;
579#if SIMDJSON_SUPPORTS_CONCEPTS
580 template <
typename Func>
581 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
583 template <
typename Func>
585 simdjson_inline
error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback)
noexcept;
592 #if SIMDJSON_SUPPORTS_CONCEPTS
596 simdjson_inline
simdjson_result<T> get()
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
598 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
601 return first.get<T>();
604 simdjson_warn_unused simdjson_inline
error_code get(T& out)
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
606 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
609 SIMDJSON_TRY( first.get<T>(out) );
621 template <
typename Selector,
typename Func>
622 requires SIMDJSON_IMPLEMENTATION::ondemand::key_selector_type<Selector> &&
623 std::is_invocable_v<Func&, std::size_t, SIMDJSON_IMPLEMENTATION::ondemand::value>
624 simdjson_inline SIMDJSON_IMPLEMENTATION::ondemand::for_each_result for_each(Func&& on_match)
625 noexcept(std::is_nothrow_invocable_v<Func&, std::size_t, SIMDJSON_IMPLEMENTATION::ondemand::value>);
630 template <
typename Selector,
typename... Handlers>
631 requires SIMDJSON_IMPLEMENTATION::ondemand::key_selector_type<Selector> &&
632 (
sizeof...(Handlers) == Selector::size()) &&
633 (SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::field_handler<Handlers> && ...)
634 simdjson_inline SIMDJSON_IMPLEMENTATION::ondemand::for_each_result for_each(Handlers&&... on_match)
635 noexcept(SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
640 template <constevalutil::fixed_string... Keys,
typename... Handlers>
641 requires (
sizeof...(Handlers) ==
sizeof...(Keys)) &&
642 (
sizeof...(Keys) >= 1) && (
sizeof...(Keys) <= 255) &&
643 (SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::field_handler<Handlers> && ...)
644 simdjson_inline SIMDJSON_IMPLEMENTATION::ondemand::for_each_result for_each(Handlers&&... on_match)
645 noexcept(SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
647#if SIMDJSON_STATIC_REFLECTION
649 template<constevalutil::fixed_string... FieldNames,
typename T>
650 requires(std::is_class_v<T> && (
sizeof...(FieldNames) > 0))
651 simdjson_warn_unused simdjson_inline
error_code extract_into(T& out)
noexcept {
653 return first.extract_into<FieldNames...>(out);
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 object_position get_current_position() const noexcept
Get an opaque token representing the object's current scanning position.
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 error_code revert_position(object_position position) noexcept
Return the object's scanning position to a snapshot previously obtained from get_current_position().
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
An ephemeral JSON value returned during iteration.
int32_t depth_t
Represents the depth of a JSON value (number of nested arrays/objects).
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
The result of a simdjson operation that could fail.
An opaque snapshot of an object's scanning position, obtained from object::get_current_position() and...
simdjson_inline object_position() noexcept=default
Default-constructed so a variable can be declared and assigned later, matching e.g.
The result of a simdjson operation that could fail.
simdjson_inline error_code error() const noexcept
The error.
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.