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;
246#if SIMDJSON_SUPPORTS_CONCEPTS
288 template <
typename Selector,
typename Func>
289 requires key_selector_type<Selector> &&
290 std::is_invocable_v<Func&, std::size_t, value>
291 simdjson_inline for_each_result for_each(Func&& on_match)
292 noexcept(std::is_nothrow_invocable_v<Func&, std::size_t, value>);
318 template <
typename Selector,
typename... Handlers>
319 requires key_selector_type<Selector> &&
320 (
sizeof...(Handlers) == Selector::size()) &&
321 (key_selector_for_each_detail::field_handler<Handlers> && ...)
322 simdjson_inline for_each_result for_each(Handlers&&... on_match)
323 noexcept(key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
332 template <constevalutil::fixed_string... Keys, typename... Handlers>
333 requires (sizeof...(Handlers) == sizeof...(Keys)) &&
334 (sizeof...(Keys) >= 1) && (sizeof...(Keys) <= 255) &&
335 (key_selector_for_each_detail::field_handler<Handlers> && ...)
336 simdjson_inline for_each_result for_each(Handlers&&... on_match)
337 noexcept(key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
398#if SIMDJSON_SUPPORTS_CONCEPTS
399 template <
typename Func>
400 requires std::invocable<Func, value>
402 template <
typename Func>
479#if SIMDJSON_SUPPORTS_CONCEPTS
487 template <
typename T>
488 simdjson_warn_unused simdjson_inline
error_code get(T &out)
489 noexcept(nothrow_gettable<T, object>) {
490 static_assert(custom_deserializable<T, object>);
491 return deserialize(*
this, out);
499 template <
typename T>
501 noexcept(nothrow_gettable<T,
object>)
503 static_assert(std::is_default_constructible<T>::value,
"The specified type is not default constructible.");
505 SIMDJSON_TRY(get<T>(out));
509#if SIMDJSON_STATIC_REFLECTION
534 template<constevalutil::fixed_string... FieldNames,
typename T>
535 requires(std::is_class_v<T> && (
sizeof...(FieldNames) > 0))
536 simdjson_warn_unused simdjson_inline
error_code extract_into(T& out) &
noexcept;
544 static simdjson_inline
simdjson_result<
object> start(value_iterator &iter) noexcept;
545 static simdjson_inline
simdjson_result<
object> start_root(value_iterator &iter) noexcept;
546 static simdjson_inline
simdjson_result<
object> started(value_iterator &iter) noexcept;
547 static simdjson_inline
object resume(const value_iterator &iter) noexcept;
548 simdjson_inline
object(const value_iterator &iter) noexcept;
550 simdjson_warn_unused simdjson_inline
error_code find_field_raw(const std::string_view key) noexcept;
552 value_iterator iter{};
553#if SIMDJSON_DEVELOPMENT_CHECKS
555 simdjson_inline
void set_locked(
bool _locked)
noexcept;
559 friend class document;
560 friend struct simdjson_result<
object>;
561#if SIMDJSON_DEVELOPMENT_CHECKS
562 friend class object_iterator;
563 friend struct simdjson_result<object_iterator>;
591#if SIMDJSON_SUPPORTS_CONCEPTS
592 template <
typename Func>
593 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
595 template <
typename Func>
597 simdjson_inline
error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback)
noexcept;
604 #if SIMDJSON_SUPPORTS_CONCEPTS
608 simdjson_inline
simdjson_result<T> get()
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
610 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
613 return first.get<T>();
616 simdjson_warn_unused simdjson_inline
error_code get(T& out)
noexcept(nothrow_gettable<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
618 if constexpr (std::is_same_v<T, SIMDJSON_IMPLEMENTATION::ondemand::object>) {
621 SIMDJSON_TRY( first.get<T>(out) );
633 template <
typename Selector,
typename Func>
634 requires SIMDJSON_IMPLEMENTATION::ondemand::key_selector_type<Selector> &&
635 std::is_invocable_v<Func&, std::size_t, SIMDJSON_IMPLEMENTATION::ondemand::value>
636 simdjson_inline SIMDJSON_IMPLEMENTATION::ondemand::for_each_result for_each(Func&& on_match)
637 noexcept(std::is_nothrow_invocable_v<Func&, std::size_t, SIMDJSON_IMPLEMENTATION::ondemand::value>);
642 template <
typename Selector,
typename... Handlers>
643 requires SIMDJSON_IMPLEMENTATION::ondemand::key_selector_type<Selector> &&
644 (
sizeof...(Handlers) == Selector::size()) &&
645 (SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::field_handler<Handlers> && ...)
646 simdjson_inline SIMDJSON_IMPLEMENTATION::ondemand::for_each_result for_each(Handlers&&... on_match)
647 noexcept(SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
652 template <constevalutil::fixed_string... Keys,
typename... Handlers>
653 requires (
sizeof...(Handlers) ==
sizeof...(Keys)) &&
654 (
sizeof...(Keys) >= 1) && (
sizeof...(Keys) <= 255) &&
655 (SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::field_handler<Handlers> && ...)
656 simdjson_inline SIMDJSON_IMPLEMENTATION::ondemand::for_each_result for_each(Handlers&&... on_match)
657 noexcept(SIMDJSON_IMPLEMENTATION::ondemand::key_selector_for_each_detail::nothrow_field_handlers_v<Handlers...>);
659#if SIMDJSON_STATIC_REFLECTION
661 template<constevalutil::fixed_string... FieldNames,
typename T>
662 requires(std::is_class_v<T> && (
sizeof...(FieldNames) > 0))
663 simdjson_warn_unused simdjson_inline
error_code extract_into(T& out)
noexcept {
665 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< object_iterator > begin() &noexcept
Get an iterator to the start of the object.
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 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.