simdjson 4.6.3
Ridiculously Fast JSON
Loading...
Searching...
No Matches
value.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_VALUE_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_VALUE_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/deserialize.h"
9#include <vector>
10#endif // SIMDJSON_CONDITIONAL_INCLUDE
11
12#include <type_traits>
13
14namespace simdjson {
15
16namespace SIMDJSON_IMPLEMENTATION {
17namespace ondemand {
22class value {
23public:
29 simdjson_inline value() noexcept = default;
30
43 template <typename T>
44 simdjson_inline simdjson_result<T> get()
45#if SIMDJSON_SUPPORTS_CONCEPTS
46 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
47#else
48 noexcept
49#endif
50 {
51 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
52 T out{};
53 SIMDJSON_TRY(get<T>(out));
54 return out;
55 }
56
57
68 template <typename T>
69 simdjson_warn_unused simdjson_inline error_code get(T &out)
70#if SIMDJSON_SUPPORTS_CONCEPTS
71 noexcept(custom_deserializable<T, value> ? nothrow_custom_deserializable<T, value> : true)
72#else
73 noexcept
74#endif
75 {
76 #if SIMDJSON_SUPPORTS_CONCEPTS
77 if constexpr (custom_deserializable<T, value>) {
78 return deserialize(*this, out);
79 } else if constexpr (concepts::optional_type<T>) {
80 using value_type = typename std::remove_cvref_t<T>::value_type;
81
82 // Check if the value is null
83 bool is_null_value;
84 SIMDJSON_TRY( is_null().get(is_null_value) );
85 if (is_null_value) {
86 out.reset(); // Set to nullopt
87 return SUCCESS;
88 }
89
90 if (!out) {
91 out.emplace();
92 }
93 return get<value_type>(out.value());
94 } else {
95 static_assert(!sizeof(T), "The get<T> method with type T is not implemented by the simdjson library. "
96 "And you do not seem to have added support for it. Indeed, we have that "
97 "simdjson::custom_deserializable<T> is false and the type T is not a default type "
98 "such as ondemand::object, ondemand::array, raw_json_string, std::string_view, uint64_t, "
99 "int64_t, double, or bool.");
100 static_cast<void>(out); // to get rid of unused errors
101 return UNINITIALIZED;
102 }
103#else // SIMDJSON_SUPPORTS_CONCEPTS
104 // Unless the simdjson library or the user provides an inline implementation, calling this method should
105 // immediately fail.
106 static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library. "
107 "The supported types are ondemand::object, ondemand::array, raw_json_string, std::string_view, uint64_t, "
108 "int64_t, double, and bool. We recommend you use get_double(), get_bool(), get_uint64(), get_int64(), "
109 " get_object(), get_array(), get_raw_json_string(), or get_string() instead of the get template."
110 " You may also add support for custom types, see our documentation.");
111 static_cast<void>(out); // to get rid of unused errors
112 return UNINITIALIZED;
113#endif
114 }
115
122 simdjson_inline simdjson_result<array> get_array() noexcept;
123
130 simdjson_inline simdjson_result<object> get_object() noexcept;
131
138 simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
139
146 simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
147
154 simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
155
162 simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
163
173 simdjson_inline simdjson_result<uint32_t> get_uint32() noexcept;
174
184 simdjson_inline simdjson_result<int32_t> get_int32() noexcept;
185
192 simdjson_inline simdjson_result<double> get_double() noexcept;
193
200 simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
201
227 simdjson_inline simdjson_result<std::string_view> get_string(bool allow_replacement = false) noexcept;
228
242 template <typename string_type>
243 simdjson_warn_unused simdjson_inline error_code get_string(string_type& receiver, bool allow_replacement = false) noexcept;
244
258 simdjson_inline simdjson_result<std::string_view> get_wobbly_string() noexcept;
267 simdjson_inline simdjson_result<raw_json_string> get_raw_json_string() noexcept;
268
275 simdjson_inline simdjson_result<bool> get_bool() noexcept;
276
285 simdjson_inline simdjson_result<bool> is_null() noexcept;
286
287#if SIMDJSON_EXCEPTIONS
297 template <class T>
298 explicit simdjson_inline operator T() noexcept(false);
305 simdjson_inline operator array() noexcept(false);
312 simdjson_inline operator object() noexcept(false);
319 simdjson_inline operator uint64_t() noexcept(false);
326 simdjson_inline operator int64_t() noexcept(false);
333 simdjson_inline operator double() noexcept(false);
345 simdjson_inline operator std::string_view() noexcept(false);
354 simdjson_inline operator raw_json_string() noexcept(false);
361 simdjson_inline operator bool() noexcept(false);
362#endif
363
371 simdjson_inline simdjson_result<array_iterator> begin() & noexcept;
377 simdjson_inline simdjson_result<array_iterator> end() & noexcept;
392 simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
410 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
418 simdjson_inline simdjson_result<value> at(size_t index) noexcept;
443 simdjson_inline simdjson_result<value> find_field(std::string_view key) noexcept;
445 simdjson_inline simdjson_result<value> find_field(const char *key) noexcept;
446
470 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) noexcept;
472 simdjson_inline simdjson_result<value> find_field_unordered(const char *key) noexcept;
474 simdjson_inline simdjson_result<value> operator[](std::string_view key) noexcept;
476 simdjson_inline simdjson_result<value> operator[](const char *key) noexcept;
477 simdjson_result<value> operator[](int) noexcept = delete;
478
492 simdjson_inline simdjson_result<json_type> type() noexcept;
493
501 simdjson_inline simdjson_result<bool> is_scalar() noexcept;
508 simdjson_inline simdjson_result<bool> is_string() noexcept;
509
515 simdjson_inline bool is_negative() noexcept;
529 simdjson_inline simdjson_result<bool> is_integer() noexcept;
554 simdjson_inline simdjson_result<number_type> get_number_type() noexcept;
555
588 simdjson_warn_unused simdjson_inline simdjson_result<number> get_number() noexcept;
589
615 simdjson_inline std::string_view raw_json_token() noexcept;
616
625 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
626
630 simdjson_inline simdjson_result<const char *> current_location() noexcept;
631
641 simdjson_inline int32_t current_depth() const noexcept;
642
685 simdjson_inline simdjson_result<value> at_pointer(std::string_view json_pointer) noexcept;
686
698 simdjson_inline simdjson_result<value> at_path(std::string_view at_path) noexcept;
699
709#if SIMDJSON_SUPPORTS_CONCEPTS
710 template <typename Func>
711 requires std::invocable<Func, value>
712#else
713 template <typename Func>
714#endif
715 simdjson_inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
716
717protected:
721 simdjson_inline value(const value_iterator &iter) noexcept;
722
726 simdjson_inline void skip() noexcept;
727
733 static simdjson_inline value start(const value_iterator &iter) noexcept;
734
738 static simdjson_inline value resume(const value_iterator &iter) noexcept;
739
743 simdjson_inline simdjson_result<object> start_or_resume_object() noexcept;
744
745 // simdjson_inline void log_value(const char *type) const noexcept;
746 // simdjson_inline void log_error(const char *message) const noexcept;
747
748 value_iterator iter{};
749
750 friend class document;
751 friend class array_iterator;
752 friend class field;
753 friend class object;
754 friend struct simdjson_result<value>;
755 friend struct simdjson_result<field>;
756 friend class field;
757};
758
759} // namespace ondemand
760} // namespace SIMDJSON_IMPLEMENTATION
761} // namespace simdjson
762
763namespace simdjson {
764
765template<>
766struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value> {
767public:
768 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::value &&value) noexcept;
769 simdjson_inline simdjson_result(error_code error) noexcept;
770 simdjson_inline simdjson_result() noexcept = default;
771
772 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> get_array() noexcept;
773 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> get_object() noexcept;
774
775 simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
776 simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
777 simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
778 simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
779 simdjson_inline simdjson_result<uint32_t> get_uint32() noexcept;
780 simdjson_inline simdjson_result<int32_t> get_int32() noexcept;
781 simdjson_inline simdjson_result<double> get_double() noexcept;
782 simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
783 simdjson_inline simdjson_result<std::string_view> get_string(bool allow_replacement = false) noexcept;
784 template <typename string_type>
785 simdjson_warn_unused simdjson_inline error_code get_string(string_type& receiver, bool allow_replacement = false) noexcept;
786 simdjson_inline simdjson_result<std::string_view> get_wobbly_string() noexcept;
787 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> get_raw_json_string() noexcept;
788 simdjson_inline simdjson_result<bool> get_bool() noexcept;
789 simdjson_inline simdjson_result<bool> is_null() noexcept;
790
791 template<typename T> simdjson_inline simdjson_result<T> get() noexcept;
792
793 template<typename T> simdjson_inline error_code get(T &out) noexcept;
794
795#if SIMDJSON_EXCEPTIONS
796 template <class T>
797 explicit simdjson_inline operator T() noexcept(false);
798 simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false);
799 simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false);
800 simdjson_inline operator uint64_t() noexcept(false);
801 simdjson_inline operator int64_t() noexcept(false);
802 simdjson_inline operator double() noexcept(false);
803 simdjson_inline operator std::string_view() noexcept(false);
804 simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false);
805 simdjson_inline operator bool() noexcept(false);
806#endif
807 simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
808 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
809 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
812
835 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) noexcept;
837 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(const char *key) noexcept;
838
859 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) noexcept;
861 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(const char *key) noexcept;
863 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) noexcept;
865 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](const char *key) noexcept;
866 simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](int) noexcept = delete;
867
891 simdjson_inline simdjson_result<bool> is_scalar() noexcept;
892 simdjson_inline simdjson_result<bool> is_string() noexcept;
893 simdjson_inline simdjson_result<bool> is_negative() noexcept;
894 simdjson_inline simdjson_result<bool> is_integer() noexcept;
895 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> get_number_type() noexcept;
896 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> get_number() noexcept;
897
899 simdjson_inline simdjson_result<std::string_view> raw_json_token() noexcept;
900 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
901
903 simdjson_inline simdjson_result<const char *> current_location() noexcept;
905 simdjson_inline simdjson_result<int32_t> current_depth() const noexcept;
906 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
907 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
908#if SIMDJSON_SUPPORTS_CONCEPTS
909 template <typename Func>
910 requires std::invocable<Func, SIMDJSON_IMPLEMENTATION::ondemand::value>
911#else
912 template <typename Func>
913#endif
914 simdjson_inline error_code for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept;
915};
916
917// Forward-declare explicit specializations so MSVC /permissive- sees them before
918// any template instantiation that would resolve element.get(val) to the primary.
919template<> simdjson_inline error_code
924
925} // namespace simdjson
926
927#endif // SIMDJSON_GENERIC_ONDEMAND_VALUE_H
A JSON field (key/value pair) in an object.
Definition field.h:22
A forward-only JSON object field iterator.
Definition object.h:21
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
Definition value.h:22
static simdjson_inline value start(const value_iterator &iter) noexcept
Start a value at the current position.
Definition value-inl.h:25
simdjson_inline simdjson_result< bool > is_null() noexcept
Checks if this JSON value is null.
Definition value-inl.h:92
simdjson_inline simdjson_result< const char * > current_location() noexcept
Returns the current location in the document if in bounds.
Definition value-inl.h:258
simdjson_inline simdjson_result< value > find_field(std::string_view key) noexcept
Look up a field by name on an object (order-sensitive).
Definition value-inl.h:180
simdjson_inline simdjson_result< T > get() noexcept
Get this value as the given type.
Definition value.h:44
simdjson_inline bool is_negative() noexcept
Checks whether the value is a negative number.
Definition value-inl.h:220
simdjson_inline simdjson_result< number_type > get_number_type() noexcept
Determine the number type (integer or floating-point number) as quickly as possible.
Definition value-inl.h:227
simdjson_inline simdjson_result< array > get_array() noexcept
Cast this JSON value to an array.
Definition value-inl.h:32
simdjson_inline std::string_view raw_json_token() noexcept
Get the raw JSON for this token.
Definition value-inl.h:234
simdjson_inline simdjson_result< object > get_object() noexcept
Cast this JSON value to an object.
Definition value-inl.h:35
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
Definition value-inl.h:158
simdjson_inline simdjson_result< int64_t > get_int64_in_string() noexcept
Cast this JSON value (inside string) to a signed integer.
Definition value-inl.h:74
simdjson_inline simdjson_result< size_t > count_fields() &noexcept
This method scans the object and counts the number of key-value pairs.
Definition value-inl.h:168
simdjson_inline simdjson_result< bool > is_integer() noexcept
Checks whether the value is an integer number.
Definition value-inl.h:224
simdjson_inline simdjson_result< array_iterator > end() &noexcept
Sentinel representing the end of the array.
Definition value-inl.h:155
simdjson_inline simdjson_result< value > at_path(std::string_view at_path) noexcept
Get the value associated with the given JSONPath expression.
Definition value-inl.h:304
simdjson_inline simdjson_result< object > start_or_resume_object() noexcept
Get the object, starting or resuming it as necessary.
Definition value-inl.h:38
simdjson_inline simdjson_result< array_iterator > begin() &noexcept
Begin array iteration.
Definition value-inl.h:152
simdjson_inline simdjson_result< uint64_t > get_uint64_in_string() noexcept
Cast this JSON value (inside string) to a unsigned integer.
Definition value-inl.h:68
simdjson_inline int32_t current_depth() const noexcept
Returns the current depth in the document if in bounds.
Definition value-inl.h:262
simdjson_inline simdjson_result< bool > is_scalar() noexcept
Checks whether the value is a scalar (string, number, null, Boolean).
Definition value-inl.h:205
simdjson_inline operator std::string_view() noexcept(false)
Cast this JSON value to a string.
Definition value-inl.h:141
simdjson_inline simdjson_result< std::string_view > raw_json() noexcept
Get a string_view pointing at this value in the JSON document.
Definition value-inl.h:238
static simdjson_inline value resume(const value_iterator &iter) noexcept
Resume a value.
Definition value-inl.h:28
simdjson_inline simdjson_result< double > get_double() noexcept
Cast this JSON value to a double.
Definition value-inl.h:59
simdjson_inline simdjson_result< std::string_view > get_string(bool allow_replacement=false) noexcept
Cast this JSON value to a string.
Definition value-inl.h:49
simdjson_inline simdjson_result< bool > get_bool() noexcept
Cast this JSON value to a bool.
Definition value-inl.h:89
simdjson_warn_unused simdjson_inline simdjson_result< number > get_number() noexcept
Attempt to parse an ondemand::number.
Definition value-inl.h:230
simdjson_inline simdjson_result< value > at_pointer(std::string_view json_pointer) noexcept
Get the value associated with the given JSON pointer.
Definition value-inl.h:286
simdjson_inline simdjson_result< std::string_view > get_wobbly_string() noexcept
Cast this JSON value to a "wobbly" string.
Definition value-inl.h:56
simdjson_inline simdjson_result< int64_t > get_int64() noexcept
Cast this JSON value to a signed integer.
Definition value-inl.h:71
simdjson_inline value() noexcept=default
Create a new invalid value.
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...
simdjson_warn_unused simdjson_inline error_code get(T &out) noexcept
Get this value as the given type.
Definition value.h:69
simdjson_inline simdjson_result< json_type > type() noexcept
Get the type of this JSON value.
Definition value-inl.h:201
simdjson_inline simdjson_result< double > get_double_in_string() noexcept
Cast this JSON value (inside string) to a double.
Definition value-inl.h:62
simdjson_inline simdjson_result< raw_json_string > get_raw_json_string() noexcept
Cast this JSON value to a raw_json_string.
Definition value-inl.h:46
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 value-inl.h:187
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index in the array.
Definition value-inl.h:175
simdjson_inline simdjson_result< uint64_t > get_uint64() noexcept
Cast this JSON value to an unsigned integer.
Definition value-inl.h:65
simdjson_inline void skip() noexcept
Skip this value, allowing iteration to continue.
simdjson_inline simdjson_result< int32_t > get_int32() noexcept
Cast this JSON value to a 32-bit signed integer.
Definition value-inl.h:83
simdjson_inline simdjson_result< uint32_t > get_uint32() noexcept
Cast this JSON value to a 32-bit unsigned integer.
Definition value-inl.h:77
simdjson_inline simdjson_result< bool > is_string() noexcept
Checks whether the value is a string.
Definition value-inl.h:212
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
@ SUCCESS
No error.
Definition error.h:20
@ UNINITIALIZED
unknown error, or uninitialized document
Definition error.h:32
A type representing a JSON number.
Definition json_type.h:33
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
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.
Definition error-inl.h:163