40 template<
typename S,
typename T>
41 struct AllDeserializable {
42 constexpr static auto value =
false;
45 template<
typename S,
typename... Types>
46 struct AllDeserializable<S, meta::List<Types...>> {
47 constexpr static auto value = (concepts::Deserializable<Types, S> && ...);
50 template<
typename S, concepts::TypeList T>
51 constexpr static auto all_deserializable = AllDeserializable<S, T>::value;
59template<concepts::Impl<io::Reader> Reader>
64 auto& key = error.value().inner().key;
74 template<
typename T =
void>
89 template<
typename T, concepts::InstanceOf<reflection::Fields> M>
100 auto code_point =
DI_TRY(peek_next_code_point());
105 if (*code_point == U
'}') {
112 DI_TRY(skip_whitespace());
114 DI_TRY(skip_whitespace());
125 return add_path_to_error(di::move(error), di::move(key));
130 return add_path_to_error(di::move(error), di::move(key));
145 DI_TRY(skip_whitespace());
149 template<
typename T, concepts::InstanceOf<reflection::Enumerators> M>
151 DI_TRY(skip_whitespace());
153 DI_TRY(skip_whitespace());
179 template<
typename T, concepts::InstanceOf<reflection::Atom> M>
180 requires(M::is_bool() || M::is_integer() || M::is_string())
182 if constexpr (M::is_bool()) {
184 DI_TRY(skip_whitespace());
186 }
else if constexpr (M::is_integer()) {
188 DI_TRY(skip_whitespace());
190 }
else if constexpr (M::is_string()) {
192 DI_TRY(skip_whitespace());
197 template<
typename T, concepts::InstanceOf<reflection::Atom> M>
209 template<
typename T, concepts::InstanceOf<reflection::Atom> M>
210 requires(M::is_tuple() && detail::all_deserializable<JsonDeserializer, meta::TupleElements<T>>)
215 DI_TRY(skip_whitespace());
218 auto count = 0_usize;
220 [&]<
typename Value>(Value& value) ->
Result<> {
221 DI_TRY(skip_whitespace());
222 auto code_point =
DI_TRY(peek_next_code_point());
228 if (code_point == U
']') {
238 auto path = di::format(
"[{}]"_sv, count);
239 return add_path_to_error(di::move(error), di::move(path));
245 DI_TRY(skip_whitespace());
247 DI_TRY(skip_whitespace());
251 template<
typename T, concepts::InstanceOf<reflection::Atom> M>
252 requires(M::is_variant() && detail::all_deserializable<JsonDeserializer, meta::VariantTypes<T>>)
254 constexpr auto possible_keys = []<
typename... Types>(
meta::List<Types...>) {
258 DI_TRY(skip_whitespace());
262 DI_TRY(skip_whitespace());
264 DI_TRY(skip_whitespace());
266 auto it =
di::find(possible_keys, key);
267 if (it == possible_keys.end()) {
274 using Value = meta::At<meta::VariantTypes<T>, index>;
275 return serialization::deserialize<Value>(*this).transform_error(
276 [&](json_deserializer::ErrorCode error) {
277 return add_path_to_error(di::move(error), di::move(key));
281 DI_TRY(skip_whitespace());
283 DI_TRY(skip_whitespace());
287 template<
typename T, concepts::InstanceOf<reflection::Atom> M,
typename U = meta::Type<meta::RemoveCVRef<T>>>
290 DI_TRY(skip_whitespace());
293 auto code_point =
DI_TRY(peek_next_code_point());
294 if (code_point == U
'n') {
295 DI_TRY(deserialize_null());
301 template<
typename T, concepts::InstanceOf<reflection::Atom> M>
306 DI_TRY(skip_whitespace());
309 for (
auto i =
usize(0);; i++) {
310 DI_TRY(skip_whitespace());
311 auto code_point =
DI_TRY(peek_next_code_point());
316 if (*code_point == U
']') {
324 auto key = di::format(
"[{}]"_sv, i);
325 return add_path_to_error(di::move(error), di::move(key));
330 DI_TRY(skip_whitespace());
334 template<
typename T, concepts::InstanceOf<reflection::Atom> M>
340 DI_TRY(skip_whitespace());
344 DI_TRY(skip_whitespace());
345 auto code_point =
DI_TRY(peek_next_code_point());
350 if (*code_point == U
'}') {
357 DI_TRY(skip_whitespace());
359 DI_TRY(skip_whitespace());
363 return add_path_to_error(di::move(error), di::move(key));
365 result.insert_or_assign(util::move(key), util::move(value));
369 DI_TRY(skip_whitespace());
375 DI_TRY(skip_whitespace());
381 constexpr auto reader() && ->
Reader&& {
return util::move(*this).m_reader; }
384 constexpr static auto is_whitespace(
c32 code_point) ->
bool {
385 return code_point ==
' ' || code_point ==
'\t' || code_point ==
'\n' || code_point ==
'\r';
389 auto code_point =
DI_TRY(require_next_code_point());
390 if (code_point != expected) {
392 json_deserializer::Error(json_deserializer::UnexpectedCharacterError { code_point, expected },
"."_s));
405 DI_TRY(m_utf8_decoder.flush().transform_error([](
auto) {
406 return json_deserializer::Error(json_deserializer::InvalidUtf8Error {},
"."_s);
410 auto maybe_code_point =
DI_TRY(m_utf8_decoder.decode(
byte[0]).transform_error([](
auto) {
411 return json_deserializer::Error(json_deserializer::InvalidUtf8Error {},
"."_s);
413 if (maybe_code_point) {
414 m_next_code_point = maybe_code_point.value();
421 constexpr auto peek_next_code_point() -> Result<vocab::Optional<c32>> {
423 return vocab::nullopt;
425 if (!m_next_code_point) {
426 DI_TRY(fill_next_code_point());
428 return vocab::nullopt;
431 return *m_next_code_point;
434 constexpr void consume() { m_next_code_point = vocab::nullopt; }
436 constexpr auto next_code_point() -> Result<vocab::Optional<c32>> {
438 return vocab::nullopt;
440 if (!m_next_code_point) {
441 DI_TRY(fill_next_code_point());
443 return vocab::nullopt;
446 return *util::exchange(m_next_code_point, vocab::nullopt);
449 constexpr auto require_next_code_point() -> Result<c32> {
452 return vocab::Unexpected(json_deserializer::Error(json_deserializer::UnexpectedEndOfInputError {},
"."_s));
457 constexpr auto skip_whitespace() -> Result<void> {
460 if (!code_point || !is_whitespace(*code_point)) {
467 constexpr auto deserialize_value() -> Result<json::Value> {
468 DI_TRY(skip_whitespace());
472 return vocab::Unexpected(json_deserializer::Error(json_deserializer::UnexpectedEndOfInputError {},
"."_s));
475 switch (*code_point) {
477 return deserialize_null();
479 return deserialize_true();
481 return deserialize_false();
495 return deserialize_number(in_place_type<json::Number>);
497 return deserialize_object();
499 return deserialize_array();
501 return vocab::Unexpected(
502 json_deserializer::Error(json_deserializer::UnexpectedCharacterError { *
code_point },
"."_s));
506 constexpr auto deserialize_null() -> Result<json::Null> {
507 DI_TRY(skip_whitespace());
516 constexpr auto deserialize_bool() -> Result<json::Bool> {
517 DI_TRY(skip_whitespace());
520 switch (code_point) {
533 return vocab::Unexpected(json_deserializer::Error(json_deserializer::ParseBoolError {},
"."_s));
537 constexpr auto deserialize_true() -> Result<json::Bool> {
538 DI_TRY(skip_whitespace());
546 constexpr auto deserialize_false() -> Result<json::Bool> {
547 DI_TRY(skip_whitespace());
556 constexpr auto from_hex_digit(
c32 code_point) -> Result<u16> {
557 if ((
'0'_m -
'9'_m)(code_point)) {
558 return u16(code_point -
'0');
560 if ((
'A'_m -
'F'_m)(code_point)) {
561 return u16(10u + (code_point -
'A'));
563 if ((
'a'_m -
'f'_m)(code_point)) {
564 return u16(10u + (code_point -
'a'));
566 return vocab::Unexpected(
567 json_deserializer::Error(json_deserializer::UnexpectedCharacterError {
code_point },
"."_s));
570 constexpr auto parse_four_hex_digits() -> Result<u16> {
571 auto result =
u16(0);
572 result |=
DI_TRY(from_hex_digit(
DI_TRY(require_next_code_point()))) << 12;
573 result |=
DI_TRY(from_hex_digit(
DI_TRY(require_next_code_point()))) << 8;
574 result |=
DI_TRY(from_hex_digit(
DI_TRY(require_next_code_point()))) << 4;
575 result |=
DI_TRY(from_hex_digit(
DI_TRY(require_next_code_point()))) << 0;
579 constexpr static auto is_high_surrogate(
u16 code_unit) ->
bool {
return (code_unit >> 10) == 0b110110u; }
580 constexpr static auto is_low_surrogate(
u16 code_unit) ->
bool {
return (code_unit >> 10) == 0b110111u; }
583 DI_TRY(skip_whitespace());
586 auto string = json::String {};
589 if (code_point < 0x20) {
590 return vocab::Unexpected(
591 json_deserializer::Error(json_deserializer::UnexpectedCharacterError {
code_point },
"."_s));
593 if (code_point == U
'\\') {
594 auto escaped =
DI_TRY(require_next_code_point());
597 string.push_back(U
'"');
600 string.push_back(U
'\\');
603 string.push_back(U
'/');
606 string.push_back(U
'\b');
609 string.push_back(U
'\f');
612 string.push_back(U
'\n');
615 string.push_back(U
'\r');
618 string.push_back(U
'\t');
624 if (is_high_surrogate(code_point)) {
628 auto low_code_point =
DI_TRY(parse_four_hex_digits());
629 if (!is_low_surrogate(low_code_point)) {
630 return vocab::Unexpected(json_deserializer::Error(
631 json_deserializer::UnexpectedCharacterError {
code_point },
"."_s));
633 auto actual_code_point =
634 c32((code_point - 0xD800u) << 10) +
c32(low_code_point - 0xDC00u) +
c32(0x10000);
635 string.push_back(actual_code_point);
638 if (is_low_surrogate(code_point)) {
639 return vocab::Unexpected(json_deserializer::Error(
640 json_deserializer::UnexpectedCharacterError {
code_point },
"."_s));
642 string.push_back(
c32(code_point));
646 return vocab::Unexpected(json_deserializer::Error(
647 json_deserializer::UnexpectedCharacterError {
code_point },
"."_s));
651 if (code_point == U
'"') {
654 string.push_back(code_point);
659 template<concepts::Integer T>
660 constexpr auto deserialize_number(InPlaceType<T>) -> Result<json::Number> {
661 DI_TRY(skip_whitespace());
662 auto first_code_point =
DI_TRY(require_next_code_point());
664 auto string = json::String {};
665 if (first_code_point == U
'-') {
666 string.push_back(first_code_point);
667 first_code_point =
DI_TRY(require_next_code_point());
668 if (first_code_point < U
'0' || first_code_point > U
'9') {
669 return vocab::Unexpected(
670 json_deserializer::Error(json_deserializer::UnexpectedCharacterError { first_code_point },
"."_s));
673 if (first_code_point == U
'0') {
677 string.push_back(first_code_point);
685 if (*code_point < U
'0' || *code_point > U
'9') {
689 string.push_back(*code_point);
694 auto result = parser::parse<T>(
string);
696 return vocab::Unexpected(
697 json_deserializer::Error(json_deserializer::ParseNumberError { di::move(
string) },
"."_s));
702 constexpr auto deserialize_array() -> Result<json::Array> {
703 DI_TRY(skip_whitespace());
706 auto array = json::Array {};
708 DI_TRY(skip_whitespace());
711 return vocab::Unexpected(
712 json_deserializer::Error(json_deserializer::UnexpectedEndOfInputError {},
"."_s));
714 if (*code_point == U
']') {
717 if (!array.empty()) {
720 array.push_back(
DI_TRY(deserialize_value()));
727 constexpr auto deserialize_object() -> Result<json::Object> {
728 DI_TRY(skip_whitespace());
731 auto object = json::Object {};
733 DI_TRY(skip_whitespace());
736 return vocab::Unexpected(
737 json_deserializer::Error(json_deserializer::UnexpectedEndOfInputError {},
"."_s));
739 if (*code_point == U
'}') {
742 if (!
object.
empty()) {
744 DI_TRY(skip_whitespace());
747 DI_TRY(skip_whitespace());
750 object.insert_or_assign(util::move(key), util::move(value));
758 vocab::Optional<c32> m_next_code_point;
759 Utf8StrictStreamDecoder m_utf8_decoder;
760 bool m_at_end {
false };
768 struct FromJsonStringFunction {
769 template<
typename... Args>
778template<concepts::Deserializable<JsonDeserializer<StringReader<container::StringView>>> T = json::Value>
783 struct DeserializeJsonFunction {
784 template<concepts::Impl<io::Reader> Reader,
typename... Args>
788 constexpr auto operator()(
Reader&& reader, Args&&... args)
const {
794template<
typename T = json::Value>
799inline namespace literals {
802 template<container::FixedString
string>
803 consteval auto valid_json_literal() ->
bool {
813 template<container::FixedString
string>
814 requires(detail::valid_json_literal<string>())
815 constexpr auto operator""_json() {
823#if !defined(DI_NO_GLOBALS) && !defined(DI_NO_GLOBAL_JSON_LITERALS)
constexpr JsonDeserializer(T &&reader)
Definition json_deserializer.h:81
constexpr auto begin() const
Definition constant_string_interface.h:69
constexpr auto insert(Iterator it, CodePoint code_point)
Definition mutable_string_interface.h:207
constexpr auto append(Con &&container) -> decltype(auto)
Definition mutable_string_interface.h:187
Definition string_reader.h:18
A deserializer for the JSON format.
Definition json_deserializer.h:60
constexpr auto reader() &-> Reader &
Definition json_deserializer.h:379
constexpr auto deserialize(InPlaceType< T >, M) -> Result< T >
Definition json_deserializer.h:181
constexpr auto deserialize(InPlaceType< T >, M) -> Result< T >
Definition json_deserializer.h:253
constexpr auto deserialize(InPlaceType< T >, M fields) -> Result< T >
Definition json_deserializer.h:90
constexpr auto deserialize(InPlaceType< T >, M) -> Result< T >
Definition json_deserializer.h:303
Expected< T, json_deserializer::ErrorCode > Result
Definition json_deserializer.h:75
constexpr auto deserialize(InPlaceType< T >, M enumerators) -> Result< T >
Definition json_deserializer.h:150
constexpr JsonDeserializer(T &&reader)
Definition json_deserializer.h:81
constexpr auto deserialize(InPlaceType< T >, M) -> Result< T > requires(M::is_custom_atom() &&
Definition json_deserializer.h:198
constexpr auto deserialize(InPlaceType< T >, M) -> Result< T >
Definition json_deserializer.h:289
JsonFormat DeserializationFormat
Definition json_deserializer.h:77
auto result
Definition json_deserializer.h:202
constexpr auto reader() &&-> Reader &&
Definition json_deserializer.h:381
constexpr auto reader() const &-> Reader const &
Definition json_deserializer.h:380
constexpr auto deserialize(InPlaceType< T >, M) -> Result< T >
Definition json_deserializer.h:211
constexpr auto deserialize(InPlaceType< json::Null >) -> Result< json::Null >
Definition json_deserializer.h:373
constexpr auto deserialize(InPlaceType< T >, M) -> Result< T >
Definition json_deserializer.h:337
constexpr auto deserialize(InPlaceType< json::Value >) -> Result< json::Value >
Definition json_deserializer.h:83
Definition json_deserializer_error.h:93
Definition expected_forward_declaration.h:8
Definition operations.h:11
Definition deserialize.h:153
#define DI_TRY(...)
Definition monad_try.h:13
#define TRY
Definition monad_try.h:23
string::StringViewImpl< string::Utf8Encoding > StringView
Definition string_view.h:12
constexpr auto empty
Definition empty.h:45
string::StringImpl< string::Utf8Encoding > String
Definition string.h:11
constexpr auto fixed_string_to_utf8_string_view
Definition fixed_string_to_utf8_string_view.h:32
constexpr auto value
Definition value.h:34
constexpr auto index_dispatch
Definition index_dispatch.h:41
constexpr auto read_some
Definition reader.h:32
meta::List< ReadSome > Reader
Definition reader.h:34
Definition json_deserializer.h:800
constexpr auto code_point
Definition code_point_parser.h:35
vocab::StatusCode< ErrorDomain > ErrorCode
Definition json_deserializer_error.h:91
constexpr auto from_json_string
Definition json_deserializer.h:779
constexpr auto deserialize_json
Definition json_deserializer.h:795
constexpr auto json_format
Definition json_serializer.h:475
constexpr auto deserialize_string
Definition deserialize_string.h:27
constexpr auto deserialize
Definition deserialize.h:178
size_t usize
Definition integers.h:33
char32_t c32
Definition char.h:6
__UINT16_TYPE__ u16
Definition integers.h:10
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
Array(T, U...) -> Array< T, 1+sizeof...(U)>
Expected< T, Error > Result
Definition result.h:8
StatusCode< Erased< long > > Error
Definition error.h:8
constexpr auto tuple_sequence
Definition tuple_sequence.h:36
constexpr void tuple_for_each(F &&function, Tup &&tuple)
Definition tuple_for_each.h:22
Unexpected(E &&) -> Unexpected< meta::UnwrapRefDecay< E > >
Definition any_storable.h:9
constexpr auto make_box
Definition box.h:171
constexpr auto ref
Definition reference_wrapper.h:98
constexpr auto find
Definition find.h:35
constexpr auto enumerator
Definition enumerator.h:41
constexpr auto to_owned
Definition to_owned.h:26
constexpr auto json_format
Definition json_serializer.h:475
constexpr auto in_place_type
Definition in_place_type.h:12
constexpr auto count
Definition count.h:37
constexpr auto deserialize
Definition deserialize.h:178
constexpr auto field
Definition field.h:47
constexpr auto parse
Definition parse.h:23
Definition json_deserializer_error.h:63
Definition json_deserializer_error.h:30
Definition json_deserializer_error.h:37
Definition json_deserializer_error.h:20
Definition json_deserializer_error.h:50
Definition in_place_type.h:5