1 #if SIMDJSON_SUPPORTS_DESERIALIZATION
3 #ifndef SIMDJSON_ONDEMAND_DESERIALIZE_H
4 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
5 #define SIMDJSON_ONDEMAND_DESERIALIZE_H
6 #include "simdjson/generic/ondemand/array.h"
7 #include "simdjson/generic/ondemand/base.h"
15 constexpr
bool require_custom_serialization =
false;
21 template <std::
unsigned_
integral T>
22 requires(!require_custom_serialization<T>)
23 error_code tag_invoke(deserialize_tag,
auto &val, T &out) noexcept {
24 using limits = std::numeric_limits<T>;
27 SIMDJSON_TRY(val.get_uint64().get(x));
28 if (x > (limits::max)()) {
31 out =
static_cast<T
>(x);
35 template <std::
floating_po
int T>
36 requires(!require_custom_serialization<T>)
37 error_code tag_invoke(deserialize_tag,
auto &val, T &out) noexcept {
39 SIMDJSON_TRY(val.get_double().get(x));
40 out =
static_cast<T
>(x);
44 template <std::
signed_
integral T>
45 requires(!require_custom_serialization<T>)
46 error_code tag_invoke(deserialize_tag,
auto &val, T &out) noexcept {
47 using limits = std::numeric_limits<T>;
50 SIMDJSON_TRY(val.get_int64().get(x));
51 if (x > (limits::max)() || x < (limits::min)()) {
54 out =
static_cast<T
>(x);
65 template <concepts::appendable_containers T,
typename ValT>
66 requires(!require_custom_serialization<T>)
67 error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(
false) {
68 using value_type =
typename std::remove_cvref_t<T>::value_type;
70 deserializable<value_type, ValT>,
71 "The specified type inside the container must itself be deserializable");
73 std::is_default_constructible_v<value_type>,
74 "The specified type inside the container must default constructible.");
76 SIMDJSON_IMPLEMENTATION::ondemand::array arr;
77 SIMDJSON_TRY(val.get_array().get(arr));
79 if constexpr (concepts::returns_reference<T>) {
80 if (
auto const err = v.get<value_type>().get(concepts::emplace_one(out));
86 if constexpr (requires { out.pop_back(); }) {
87 static_cast<void>(out.pop_back());
93 if (
auto const err = v.get<value_type>().get(temp); err) {
96 concepts::emplace_one(out, std::move(temp));
118 template <concepts::smart_po
inter T,
typename ValT>
119 requires(!require_custom_serialization<T>)
120 error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(nothrow_deserializable<
typename std::remove_cvref_t<T>::element_type, ValT>) {
121 using element_type =
typename std::remove_cvref_t<T>::element_type;
126 deserializable<element_type, ValT>,
127 "The specified type inside the unique_ptr must itself be deserializable");
129 std::is_default_constructible_v<element_type>,
130 "The specified type inside the unique_ptr must default constructible.");
133 if (ptr ==
nullptr) {
136 SIMDJSON_TRY(val.template get<element_type>(*ptr));
144 template <concepts::optional_type T,
typename ValT>
145 requires(!require_custom_serialization<T>)
146 error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(nothrow_deserializable<
typename std::remove_cvref_t<T>::value_type, ValT>) {
147 using value_type =
typename std::remove_cvref_t<T>::value_type;
150 deserializable<value_type, ValT>,
151 "The specified type inside the unique_ptr must itself be deserializable");
153 std::is_default_constructible_v<value_type>,
154 "The specified type inside the unique_ptr must default constructible.");
159 SIMDJSON_TRY(val.template get<value_type>(out.value()));
element_type
The actual concrete type of a JSON element This is the type it is most easily cast to with get<>.
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
@ NUMBER_OUT_OF_RANGE
JSON number does not fit in 64 bits.
@ MEMALLOC
Error allocating memory, most likely out of memory.