simdjson 4.0.7
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#endif // SIMDJSON_CONDITIONAL_INCLUDE
10
11#include <type_traits>
12
13namespace simdjson {
14
15namespace SIMDJSON_IMPLEMENTATION {
16namespace ondemand {
21class value {
22public:
28 simdjson_inline value() noexcept = default;
29
43 simdjson_inline simdjson_result<T> get()
44#if SIMDJSON_SUPPORTS_CONCEPTS
46#else
47 noexcept
48#endif
49 {
50 static_assert(std::is_default_constructible<T>::value, "The specified type is not default constructible.");
51 T out{};
52 SIMDJSON_TRY(get<T>(out));
53 return out;
54 }
55
56
67 template <typename T>
68 simdjson_warn_unused simdjson_inline error_code get(T &out)
69#if SIMDJSON_SUPPORTS_CONCEPTS
71#else
72 noexcept
73#endif
74 {
75 #if SIMDJSON_SUPPORTS_CONCEPTS
76 if constexpr (custom_deserializable<T, value>) {
77 return deserialize(*this, out);
78 } else if constexpr (concepts::optional_type<T>) {
79 using value_type = typename std::remove_cvref_t<T>::value_type;
80
81 // Check if the value is null
82 bool is_null_value;
83 SIMDJSON_TRY( is_null().get(is_null_value) );
84 if (is_null_value) {
85 out.reset(); // Set to nullopt
86 return SUCCESS;
87 }
88
89 if (!out) {
90 out.emplace();
91 }
92 return get<value_type>(out.value());
93 } else {
94 static_assert(!sizeof(T), "The get<T> method with type T is not implemented by the simdjson library. "
95 "And you do not seem to have added support for it. Indeed, we have that "
96 "simdjson::custom_deserializable<T> is false and the type T is not a default type "
97 "such as ondemand::object, ondemand::array, raw_json_string, std::string_view, uint64_t, "
98 "int64_t, double, or bool.");
99 static_cast<void>(out); // to get rid of unused errors
100 return UNINITIALIZED;
101 }
102#else // SIMDJSON_SUPPORTS_CONCEPTS
103 // Unless the simdjson library or the user provides an inline implementation, calling this method should
104 // immediately fail.
105 static_assert(!sizeof(T), "The get method with given type is not implemented by the simdjson library. "
106 "The supported types are ondemand::object, ondemand::array, raw_json_string, std::string_view, uint64_t, "
107 "int64_t, double, and bool. We recommend you use get_double(), get_bool(), get_uint64(), get_int64(), "
108 " get_object(), get_array(), get_raw_json_string(), or get_string() instead of the get template."
109 " You may also add support for custom types, see our documentation.");
110 static_cast<void>(out); // to get rid of unused errors
111 return UNINITIALIZED;
112#endif
113 }
114
121 simdjson_inline simdjson_result<array> get_array() noexcept;
122
129 simdjson_inline simdjson_result<object> get_object() noexcept;
130
137 simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
138
146
153 simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
154
162
169 simdjson_inline simdjson_result<double> get_double() noexcept;
170
177 simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
178
205
220 simdjson_warn_unused simdjson_inline error_code get_string(string_type& receiver, bool allow_replacement = false) noexcept;
221
245
252 simdjson_inline simdjson_result<bool> get_bool() noexcept;
253
262 simdjson_inline simdjson_result<bool> is_null() noexcept;
263
264#if SIMDJSON_EXCEPTIONS
274 template <class T>
275 explicit simdjson_inline operator T() noexcept(false);
282 simdjson_inline operator array() noexcept(false);
289 simdjson_inline operator object() noexcept(false);
296 simdjson_inline operator uint64_t() noexcept(false);
303 simdjson_inline operator int64_t() noexcept(false);
310 simdjson_inline operator double() noexcept(false);
322 simdjson_inline operator std::string_view() noexcept(false);
331 simdjson_inline operator raw_json_string() noexcept(false);
338 simdjson_inline operator bool() noexcept(false);
339#endif
340
348 simdjson_inline simdjson_result<array_iterator> begin() & noexcept;
354 simdjson_inline simdjson_result<array_iterator> end() & noexcept;
369 simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
387 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
395 simdjson_inline simdjson_result<value> at(size_t index) noexcept;
418 simdjson_inline simdjson_result<value> find_field(std::string_view key) noexcept;
420 simdjson_inline simdjson_result<value> find_field(const char *key) noexcept;
421
444 simdjson_inline simdjson_result<value> find_field_unordered(std::string_view key) noexcept;
446 simdjson_inline simdjson_result<value> find_field_unordered(const char *key) noexcept;
448 simdjson_inline simdjson_result<value> operator[](std::string_view key) noexcept;
450 simdjson_inline simdjson_result<value> operator[](const char *key) noexcept;
451 simdjson_result<value> operator[](int) noexcept = delete;
452
466 simdjson_inline simdjson_result<json_type> type() noexcept;
467
475 simdjson_inline simdjson_result<bool> is_scalar() noexcept;
482 simdjson_inline simdjson_result<bool> is_string() noexcept;
483
489 simdjson_inline bool is_negative() noexcept;
503 simdjson_inline simdjson_result<bool> is_integer() noexcept;
528 simdjson_inline simdjson_result<number_type> get_number_type() noexcept;
529
562 simdjson_warn_unused simdjson_inline simdjson_result<number> get_number() noexcept;
563
589 simdjson_inline std::string_view raw_json_token() noexcept;
590
599 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
600
604 simdjson_inline simdjson_result<const char *> current_location() noexcept;
605
615 simdjson_inline int32_t current_depth() const noexcept;
616
660
673
674
679 simdjson_inline value(const value_iterator &iter) noexcept;
680
684 simdjson_inline void skip() noexcept;
685
691 static simdjson_inline value start(const value_iterator &iter) noexcept;
692
696 static simdjson_inline value resume(const value_iterator &iter) noexcept;
697
701 simdjson_inline simdjson_result<object> start_or_resume_object() noexcept;
702
703 // simdjson_inline void log_value(const char *type) const noexcept;
704 // simdjson_inline void log_error(const char *message) const noexcept;
705
706 value_iterator iter{};
707
708 friend class document;
709 friend class array_iterator;
710 friend class field;
711 friend class object;
712 friend struct simdjson_result<value>;
713 friend struct simdjson_result<field>;
714 friend class field;
715};
716
717} // namespace ondemand
718} // namespace SIMDJSON_IMPLEMENTATION
719} // namespace simdjson
720
721namespace simdjson {
722
723template<>
724struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::value> {
725public:
726 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::value &&value) noexcept;
727 simdjson_inline simdjson_result(error_code error) noexcept;
728 simdjson_inline simdjson_result() noexcept = default;
729
730 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::array> get_array() noexcept;
731 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::object> get_object() noexcept;
732
733 simdjson_inline simdjson_result<uint64_t> get_uint64() noexcept;
734 simdjson_inline simdjson_result<uint64_t> get_uint64_in_string() noexcept;
735 simdjson_inline simdjson_result<int64_t> get_int64() noexcept;
736 simdjson_inline simdjson_result<int64_t> get_int64_in_string() noexcept;
737 simdjson_inline simdjson_result<double> get_double() noexcept;
738 simdjson_inline simdjson_result<double> get_double_in_string() noexcept;
739 simdjson_inline simdjson_result<std::string_view> get_string(bool allow_replacement = false) noexcept;
740 template <typename string_type>
741 simdjson_warn_unused simdjson_inline error_code get_string(string_type& receiver, bool allow_replacement = false) noexcept;
742 simdjson_inline simdjson_result<std::string_view> get_wobbly_string() noexcept;
743 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> get_raw_json_string() noexcept;
744 simdjson_inline simdjson_result<bool> get_bool() noexcept;
745 simdjson_inline simdjson_result<bool> is_null() noexcept;
746
747 template<typename T> simdjson_inline simdjson_result<T> get() noexcept;
748
749 template<typename T> simdjson_inline error_code get(T &out) noexcept;
750
751#if SIMDJSON_EXCEPTIONS
752 template <class T>
753 explicit simdjson_inline operator T() noexcept(false);
754 simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false);
755 simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::object() noexcept(false);
756 simdjson_inline operator uint64_t() noexcept(false);
757 simdjson_inline operator int64_t() noexcept(false);
758 simdjson_inline operator double() noexcept(false);
759 simdjson_inline operator std::string_view() noexcept(false);
760 simdjson_inline operator SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string() noexcept(false);
761 simdjson_inline operator bool() noexcept(false);
762#endif
763 simdjson_inline simdjson_result<size_t> count_elements() & noexcept;
764 simdjson_inline simdjson_result<size_t> count_fields() & noexcept;
765 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at(size_t index) noexcept;
768
789 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(std::string_view key) noexcept;
791 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field(const char *key) noexcept;
792
812 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(std::string_view key) noexcept;
814 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> find_field_unordered(const char *key) noexcept;
816 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](std::string_view key) noexcept;
818 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](const char *key) noexcept;
819 simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> operator[](int) noexcept = delete;
820
844 simdjson_inline simdjson_result<bool> is_scalar() noexcept;
845 simdjson_inline simdjson_result<bool> is_string() noexcept;
846 simdjson_inline simdjson_result<bool> is_negative() noexcept;
847 simdjson_inline simdjson_result<bool> is_integer() noexcept;
848 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::number_type> get_number_type() noexcept;
849 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::number> get_number() noexcept;
850
852 simdjson_inline simdjson_result<std::string_view> raw_json_token() noexcept;
853 simdjson_inline simdjson_result<std::string_view> raw_json() noexcept;
854
856 simdjson_inline simdjson_result<const char *> current_location() noexcept;
858 simdjson_inline simdjson_result<int32_t> current_depth() const noexcept;
859 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_pointer(std::string_view json_pointer) noexcept;
860 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> at_path(std::string_view json_path) noexcept;
861};
862
863} // namespace simdjson
864
865#endif // SIMDJSON_GENERIC_ONDEMAND_VALUE_H
simdjson_result< bool > reset() &noexcept
Reset the iterator so that we are pointing back at the beginning of the array.
Definition array-inl.h:127
A JSON field (key/value pair) in an object.
Definition field.h:22
A forward-only JSON object field iterator.
Definition object.h:20
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
Definition value.h:21
static simdjson_inline value start(const value_iterator &iter) noexcept
Start a value at the current position.
Definition value-inl.h:23
simdjson_inline simdjson_result< bool > is_null() noexcept
Checks if this JSON value is null.
Definition value-inl.h:78
simdjson_inline simdjson_result< const char * > current_location() noexcept
Returns the current location in the document if in bounds.
Definition value-inl.h:240
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:162
simdjson_inline simdjson_result< T > get() noexcept
Get this value as the given type.
Definition value.h:43
simdjson_inline bool is_negative() noexcept
Checks whether the value is a negative number.
Definition value-inl.h:202
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:209
simdjson_inline simdjson_result< array > get_array() noexcept
Cast this JSON value to an array.
Definition value-inl.h:30
simdjson_inline std::string_view raw_json_token() noexcept
Get the raw JSON for this token.
Definition value-inl.h:216
simdjson_inline simdjson_result< object > get_object() noexcept
Cast this JSON value to an object.
Definition value-inl.h:33
simdjson_inline simdjson_result< size_t > count_elements() &noexcept
This method scans the array and counts the number of elements.
Definition value-inl.h:140
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:72
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:150
simdjson_inline simdjson_result< bool > is_integer() noexcept
Checks whether the value is an integer number.
Definition value-inl.h:206
simdjson_inline simdjson_result< array_iterator > end() &noexcept
Sentinel representing the end of the array.
Definition value-inl.h:137
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:286
simdjson_inline simdjson_result< object > start_or_resume_object() noexcept
Get the object, starting or resuming it as necessary.
Definition value-inl.h:36
simdjson_inline simdjson_result< array_iterator > begin() &noexcept
Begin array iteration.
Definition value-inl.h:134
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:66
simdjson_inline int32_t current_depth() const noexcept
Returns the current depth in the document if in bounds.
Definition value-inl.h:244
simdjson_inline simdjson_result< bool > is_scalar() noexcept
Checks whether the value is a scalar (string, number, null, Boolean).
Definition value-inl.h:187
simdjson_inline operator std::string_view() noexcept(false)
Cast this JSON value to a string.
Definition value-inl.h:123
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:220
static simdjson_inline value resume(const value_iterator &iter) noexcept
Resume a value.
Definition value-inl.h:26
simdjson_inline simdjson_result< double > get_double() noexcept
Cast this JSON value to a double.
Definition value-inl.h:57
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:47
simdjson_inline simdjson_result< bool > get_bool() noexcept
Cast this JSON value to a bool.
Definition value-inl.h:75
simdjson_warn_unused simdjson_inline simdjson_result< number > get_number() noexcept
Attempt to parse an ondemand::number.
Definition value-inl.h:212
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:268
simdjson_inline simdjson_result< std::string_view > get_wobbly_string() noexcept
Cast this JSON value to a "wobbly" string.
Definition value-inl.h:54
simdjson_inline simdjson_result< int64_t > get_int64() noexcept
Cast this JSON value to a signed integer.
Definition value-inl.h:69
simdjson_inline value() noexcept=default
Create a new invalid value.
simdjson_warn_unused simdjson_inline error_code get(T &out) noexcept
Get this value as the given type.
Definition value.h:68
simdjson_inline simdjson_result< json_type > type() noexcept
Get the type of this JSON value.
Definition value-inl.h:183
simdjson_inline simdjson_result< double > get_double_in_string() noexcept
Cast this JSON value (inside string) to a double.
Definition value-inl.h:60
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:44
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:169
simdjson_inline simdjson_result< value > at(size_t index) noexcept
Get the value at the given index in the array.
Definition value-inl.h:157
simdjson_inline simdjson_result< uint64_t > get_uint64() noexcept
Cast this JSON value to an unsigned integer.
Definition value-inl.h:63
simdjson_inline void skip() noexcept
Skip this value, allowing iteration to continue.
simdjson_inline simdjson_result< bool > is_string() noexcept
Checks whether the value is a string.
Definition value-inl.h:194
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:278
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