49 template <
typename R>
class decoder_archive {
62 return owner_.options();
66 return reader_.kind();
70 return reader_.size();
73 bool read_bool()
const {
74 return reader_.read_bool();
77 std::int64_t read_signed()
const {
78 return reader_.read_signed();
81 std::uint64_t read_unsigned()
const {
82 return reader_.read_unsigned();
85 double read_floating()
const {
86 return reader_.read_floating();
89 std::string_view read_string()
const {
90 return reader_.read_string();
93 auto element(std::size_t index)
const {
94 return reader_.element(index);
97 auto member(std::size_t index)
const {
98 return reader_.member(index);
102 basic_deserializer &owner_;
108 explicit depth_guard(basic_deserializer &
s) : s_(
s), ok_(
s.depth_ <
s.options_.max_depth) {
125 basic_deserializer &s_;
132 if constexpr (
requires {
r.value().source_offset(); }) {
133 result.error().offset =
r.value().source_offset();
140 using U = detail::unqualified_t<T>;
144 } -> std::same_as<serialization_result<U>>;
148 }
else if constexpr (std::same_as<U, std::nullptr_t> || std::same_as<U, std::monostate>) {
153 }
else if constexpr (std::same_as<U, bool>) {
157 return r.read_bool();
158 }
else if constexpr (std::is_integral_v<U> && std::is_signed_v<U>) {
160 const auto n =
r.read_signed();
161 if (
n <
static_cast<std::int64_t
>(std::numeric_limits<U>::min()) ||
162 n >
static_cast<std::int64_t
>(std::numeric_limits<U>::max())) {
164 "integer value is out of range");
166 return static_cast<U>(
n);
169 const auto n =
r.read_unsigned();
170 if (
n >
static_cast<std::uint64_t
>(std::numeric_limits<U>::max())) {
172 "integer value is out of range");
174 return static_cast<U>(
n);
177 }
else if constexpr (std::is_integral_v<U> && std::is_unsigned_v<U>) {
179 const auto n =
r.read_unsigned();
180 if (
n >
static_cast<std::uint64_t
>(std::numeric_limits<U>::max())) {
182 "integer value is out of range");
184 return static_cast<U>(
n);
187 const auto n =
r.read_signed();
188 if (
n < 0 ||
static_cast<std::uint64_t
>(
n) >
189 static_cast<std::uint64_t
>(std::numeric_limits<U>::max())) {
191 "integer value is out of range");
193 return static_cast<U>(
n);
196 "expected unsigned integer");
197 }
else if constexpr (std::is_floating_point_v<U>) {
200 n =
r.read_floating();
204 n =
r.read_unsigned();
208 if (!std::isfinite(
static_cast<long double>(
n)) ||
n < std::numeric_limits<U>::lowest() ||
209 n > std::numeric_limits<U>::max()) {
211 "floating point value is out of range");
213 return static_cast<U>(
n);
214 }
else if constexpr (std::same_as<U, std::string>) {
218 return std::string(
r.read_string());
219 }
else if constexpr (std::is_enum_v<U>) {
231 using B = std::underlying_type_t<U>;
236 U v =
static_cast<U>(*n);
243 }
else if constexpr (detail::is_optional_v<U>) {
251 return U{std::move(*
v)};
252 }
else if constexpr (detail::is_sequence_v<U>) {
258 "container size exceeds configured limit");
260 depth_guard
guard(*
this);
263 "maximum deserialization depth exceeded");
266 if constexpr (
requires {
out.reserve(
r.size()); }) {
267 out.reserve(
r.size());
269 for (std::size_t
i = 0;
i <
r.size(); ++
i) {
274 out.emplace_back(std::move(*
v));
277 }
else if constexpr (detail::is_set_v<U>) {
283 "container size exceeds configured limit");
285 depth_guard
guard(*
this);
288 "maximum deserialization depth exceeded");
291 for (std::size_t
i = 0;
i <
r.size(); ++
i) {
299 "duplicate set element", std::to_string(
i));
303 }
else if constexpr (detail::is_array_v<U> || detail::is_pair_v<U> ||
304 detail::is_tuple_like_v<U>) {
305 constexpr auto N = std::tuple_size_v<U>;
308 "container size exceeds configured limit");
315 "tuple length does not match input");
317 depth_guard
guard(*
this);
320 "maximum deserialization depth exceeded");
323 }
else if constexpr (detail::is_map_v<U>) {
326 "expected map array/object");
330 "container size exceeds configured limit");
332 depth_guard
guard(*
this);
335 "maximum deserialization depth exceeded");
338 using K =
typename detail::map_traits<U>::key_type;
339 using V =
typename detail::map_traits<U>::mapped_type;
340 if constexpr (detail::is_string_key_v<K>) {
345 auto decode_member = [&](
const auto &
m) -> serialization_result<void> {
350 out.emplace(std::string(
m.name), std::move(*
v));
355 for (std::size_t
i =
r.size();
i != 0; --
i) {
356 auto m =
r.member(
i - 1);
357 if (
out.find(std::string(
m.name)) !=
out.end()) {
366 for (std::size_t
i = 0;
i <
r.size(); ++
i) {
367 auto m =
r.member(
i);
368 if (
out.find(std::string(
m.name)) !=
out.end()) {
371 "duplicate map key", std::string(
m.name));
384 "expected map entry array");
386 for (std::size_t
i = 0;
i <
r.size(); ++
i) {
387 auto e =
r.element(
i);
390 "map entry must contain key and value",
396 "maximum deserialization depth exceeded",
410 "duplicate map key", std::to_string(
i));
415 }
else if constexpr (detail::is_variant_v<U>) {
418 "expected variant object");
422 "container size exceeds configured limit");
424 depth_guard
guard(*
this);
427 "maximum deserialization depth exceeded");
429 std::optional<Reader> index;
431 for (std::size_t
i = 0;
i <
r.size(); ++
i) {
432 auto m =
r.member(
i);
433 auto select = [&](std::optional<Reader> &
field) -> serialization_result<void> {
437 "duplicate variant field", std::string(
m.name));
444 field.emplace(std::move(
m.value));
447 if (
m.name ==
"index") {
452 }
else if (
m.name ==
"value") {
459 "unknown variant field", std::string(
m.name));
464 "variant requires index and value");
468 "variant index must be unsigned",
"index");
473 "variant index is out of range",
"index");
483 static_assert(detail::always_false_v<U>,
484 "type is not deserializable; specialize serialization_traits<T>");
488 template <
typename T, std::size_t...
I>
490 auto decode_element = [&]<std::size_t
J>() -> serialization_result<std::tuple_element_t<J, T>> {
497 std::tuple<serialization_result<std::tuple_element_t<I, T>>...>
vals{
500 serialization_error
e;
503 auto check = [&](
auto &
x) {
515 return T{std::move(*std::get<I>(
vals))...};
518 template <
typename V, std::
size_t I = 0>
520 if constexpr (
I == std::variant_size_v<V>) {
522 "variant index is out of range");
523 }
else if (index ==
I) {
528 return V{std::in_place_index<I>, std::move(*
v)};
535 static_assert(detail::valid_schema<T>(),
536 "serialization_schema contains an unknown/duplicate member "
537 "or an overlapping wire/alias name");
543 "container size exceeds configured limit");
545 depth_guard
guard(*
this);
548 "maximum deserialization depth exceeded");
554 template <
typename T, std::size_t...
I>
561 std::array<std::optional<selected_input>,
sizeof...(I)>
selected;
562 std::tuple<std::optional<member_type_t<I, T>>...>
slots;
564 serialization_error error;
566 auto m =
r.member(
pos);
568 auto one = [&]<std::size_t
J>() {
569 using D = detail::field_descriptor_t<T, J>;
570 if (D::matches(
m.name)) {
574 "input field matches multiple schema members",
575 std::string(
m.name));
578 if constexpr (!D::is_transient) {
583 "duplicate object field", std::string(
m.name));
599 std::string(
m.name));
605 for (std::size_t
p = 0;
p <
r.size() &&
ok; ++
p) {
622 std::get<J>(
slots).emplace(std::move(*
v));
625 auto missing = [&]<std::size_t
J>() {
626 auto &
s = std::get<J>(
slots);
629 using D = detail::field_descriptor_t<T, J>;
630 if constexpr (D::has_default) {
632 }
else if constexpr (D::is_transient && std::default_initializable<M>) {
634 }
else if constexpr (detail::is_optional_v<M>) {
635 s.emplace(std::nullopt);
639 "missing required object field",
640 std::string(detail::descriptor_wire<D>()));
648 return T{std::move(*std::get<I>(
slots))...};
652 deserialization_options options_;
653 std::size_t depth_ = 0;