DAW JSON Link
daw_json_link_types_fwd.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
11#include "daw_json_enums.h"
14#include "daw_json_parse_name.h"
16#include "daw_json_traits.h"
17#include "version.h"
18
19#include <daw/cpp_17.h>
20#include <daw/daw_arith_traits.h>
21#include <daw/daw_fwd_pack_apply.h>
22#include <daw/daw_traits.h>
23#include <daw/daw_utility.h>
24
25#include <chrono>
26#include <ciso646>
27#include <optional>
28#include <string>
29
30namespace daw::json {
31 inline namespace DAW_JSON_VER {
48 template<JSONNAMETYPE Name, typename T,
49 typename Constructor = default_constructor<T>,
51 struct json_class;
52
61 template<JSONNAMETYPE Name, typename T,
62 typename Constructor = nullable_constructor<T>,
65 json_class<Name, T, Constructor,
66 json_details::class_opts_set<Options, JsonNullDefault>>;
67
78 template<JSONNAMETYPE Name, typename T = double,
80 typename Constructor = default_constructor<T>>
81 struct json_number;
91 template<JSONNAMETYPE Name, typename T = std::optional<double>,
92 json_details::json_options_t Options = number_opts_def,
93 typename Constructor = nullable_constructor<T>>
95 json_number<Name, T,
96 json_details::number_opts_set<Options, JsonNullDefault>,
97 Constructor>;
98
108 template<JSONNAMETYPE Name, typename T = double,
110 typename Constructor = default_constructor<T>>
112 Name, T,
113 json_details::number_opts_set<Options, JsonRangeCheck::CheckForNarrowing>,
114 Constructor>;
115
124 template<JSONNAMETYPE Name, typename T = bool,
126 typename Constructor = default_constructor<T>>
127 struct json_bool;
128
136 template<JSONNAMETYPE Name, typename T = std::optional<bool>,
137 json_details::json_options_t Options = bool_opts_def,
138 typename Constructor = default_constructor<T>>
141 Constructor>;
142
155 template<JSONNAMETYPE Name, typename String = std::string,
157 typename Constructor = default_constructor<String>>
158 struct json_string;
159
172 template<JSONNAMETYPE Name, typename String = std::optional<std::string>,
173 json_details::json_options_t Options = string_opts_def,
174 typename Constructor = nullable_constructor<String>>
176 json_string<Name, String,
177 json_details::string_opts_set<Options, JsonNullDefault>,
178 Constructor>;
179
195 template<JSONNAMETYPE Name, typename String = std::string,
197 typename Constructor = default_constructor<String>>
198 struct json_string_raw;
199
214 template<JSONNAMETYPE Name, typename String = std::optional<std::string>,
215 json_details::json_options_t Options = string_raw_opts_def,
216 typename Constructor = nullable_constructor<String>>
218 Name, String, json_details::string_raw_opts_set<Options, JsonNullDefault>,
219 Constructor>;
220
229 template<JSONNAMETYPE Name, typename T = std::optional<double>,
230 json_details::json_options_t Options = number_opts_def,
231 typename Constructor = nullable_constructor<T>>
233 Name, T,
236 Constructor>;
237
252 template<JSONNAMETYPE Name, typename Container,
253 typename JsonValueType = typename Container::mapped_type,
254 typename JsonKeyType = json_base::json_string<std::string>,
255 typename Constructor = default_constructor<Container>,
257 struct json_key_value;
258
272 template<JSONNAMETYPE Name, typename Container,
273 typename JsonValueType = typename Container::mapped_type,
274 typename JsonKeyType = typename Container::key_type,
275 typename Constructor = nullable_constructor<Container>>
277 json_key_value<Name, Container, JsonValueType, JsonKeyType, Constructor,
279
289 template<JSONNAMETYPE Name,
290 typename T = std::chrono::time_point<std::chrono::system_clock,
291 std::chrono::milliseconds>,
292 typename Constructor =
295 struct json_date;
296
304 template<JSONNAMETYPE Name,
305 typename T = std::optional<std::chrono::time_point<
306 std::chrono::system_clock, std::chrono::milliseconds>>,
307 typename Constructor =
310
311 /***
312 * A type to hold the types for parsing tagged variants.
313 * @tparam JsonElements a list of types that can be parsed,
314 */
315 template<typename... JsonElements>
319 fwd_pack<json_details::json_deduced_type<JsonElements>...>;
320 };
321
333 template<JSONNAMETYPE Name, typename JsonElement,
334 typename Container = json_deduce_type,
335 typename Constructor = json_deduce_type,
337 struct json_array;
338
349 template<JSONNAMETYPE Name, typename JsonElement,
350 typename Container = json_deduce_type,
351 typename Constructor = json_deduce_type>
354
370 template<JSONNAMETYPE Name, typename Container,
371 typename JsonValueType = typename Container::mapped_type,
372 typename JsonKeyType = typename Container::key_type,
373 typename Constructor = default_constructor<Container>,
376
391 template<JSONNAMETYPE Name, typename Container,
392 typename JsonValueType = typename Container::mapped_type,
393 typename JsonKeyType = typename Container::key_type,
394 typename Constructor = nullable_constructor<Container>>
396 json_key_value_array<Name, Container, JsonValueType, JsonKeyType,
397 Constructor, JsonNullDefault>;
398
412 template<JSONNAMETYPE Name, typename T,
413 typename FromJsonConverter = default_from_json_converter_t<T>,
414 typename ToJsonConverter = default_to_json_converter_t<T>,
416 struct json_custom;
417
427 template<JSONNAMETYPE Name, typename T,
428 typename FromJsonConverter = default_from_json_converter_t<T>,
429 typename ToJsonConverter = default_to_json_converter_t<T>,
432 json_custom<Name, T, FromJsonConverter, ToJsonConverter,
433 json_details::json_custom_opts_set<Options, JsonNullDefault>>;
434
435 template<JSONNAMETYPE Name, typename T,
436 typename FromJsonConverter = default_from_json_converter_t<T>,
437 typename ToJsonConverter = default_to_json_converter_t<T>,
440 Name, T, FromJsonConverter, ToJsonConverter,
441 json_details::json_custom_opts_set<Options, JsonCustomTypes::Literal>>;
442
443 template<JSONNAMETYPE Name, typename T,
444 typename FromJsonConverter = default_from_json_converter_t<T>,
445 typename ToJsonConverter = default_to_json_converter_t<T>,
448 json_custom<Name, T, FromJsonConverter, ToJsonConverter,
451
452 namespace json_details {
453 template<JsonBaseParseTypes PT>
454 constexpr std::size_t
455 find_json_element( std::initializer_list<JsonBaseParseTypes> pts ) {
456 std::size_t idx = 0;
457 for( auto const &pt : pts ) {
458 if( pt == PT ) {
459 return idx;
460 }
461 ++idx;
462 }
463 return ( daw::numeric_limits<std::size_t>::max )( );
464 }
465
466 template<typename T>
469 };
470
471 template<typename... Ts>
474 };
475
476 template<typename... Ts>
477 [[maybe_unused]] constexpr std::conditional_t<
478 std::conjunction<has_json_deduced_type<Ts>...>::value,
481 get_variant_type_list( std::variant<Ts...> const * );
482
483 template<typename T>
484 using underlying_nullable_type = decltype( *std::declval<T>( ) );
485
486 template<typename T>
488 std::remove_reference_t<daw::detected_t<underlying_nullable_type, T>>;
489
490 template<typename T>
491 using is_nullable_type = daw::is_detected<underlying_nullable_type, T>;
492
493 template<typename T>
495
496 template<typename T>
497 [[maybe_unused]] constexpr unknown_variant_type<T>
499
502 template<typename, typename = void>
504
505 template<typename... Ts>
506 struct variant_alternatives_list<std::variant<Ts...>> {
507 using type = std::conditional_t<
508 std::conjunction<has_json_deduced_type<Ts>...>::value,
511 };
512
513 template<typename, typename = void>
515
516 template<template<class...> class Tuple, typename... Ts>
517 struct tuple_types_list<Tuple<Ts...>> {
518 static_assert( std::conjunction_v<has_deduced_type_mapping<Ts>...>,
519 "Missing mapping for type in tuple" );
520
521 using types = std::tuple<json_deduced_type<Ts>...>;
522 };
523
524 template<JsonNullable, typename>
526
527 template<JsonNullable Nullable, typename Variant>
528 using determine_variant_element_types = std::conditional_t<
529 std::disjunction_v<daw::not_trait<is_nullable_json_value<Nullable>>,
530 daw::not_trait<is_nullable_type<Variant>>>,
532 std::conditional_t<
533 is_nullable_type_v<Variant>,
536
537 template<JsonNullable, typename>
539
540 template<JsonNullable Nullable, typename Tuple>
541 using determine_tuple_element_types = std::conditional_t<
542 std::disjunction_v<daw::not_trait<is_nullable_json_value<Nullable>>,
543 daw::not_trait<is_nullable_type<Tuple>>>,
545 std::conditional_t<is_nullable_type_v<Tuple>,
546 typename tuple_types_list<
549 } // namespace json_details
550
551 /***
552 * Link to a variant like data type. The JSON member can be any one of the
553 * json types. This precludes having more than one class type or array
554 * type(including their specialized keyvalue mappings) or
555 * string-enum/int-enum.
556 * @tparam Name name of JSON member to link to
557 * @tparam T type of value to construct
558 * @tparam JsonElements a json_variant_type_list
559 * @tparam Constructor A callable used to construct T. The
560 * default supports normal and aggregate construction
561 * @tparam Nullable Can the member be missing or have a null value *
562 */
563 template<JSONNAMETYPE Name, typename Variant,
564 typename JsonElements = json_deduce_type,
565 typename Constructor = default_constructor<Variant>,
567 struct json_variant;
568
569 /***
570 * Link to a nullable JSON variant
571 * @tparam Name name of JSON member to link to
572 * @tparam T type that has specialization of
573 * daw::json::json_data_contract
574 * @tparam JsonElements a json_variant_type_list
575 * @tparam Constructor A callable used to construct T. The
576 * default supports normal and aggregate construction
577 */
578 template<JSONNAMETYPE Name, typename Variant,
579 typename JsonElements = json_deduce_type,
580 typename Constructor = nullable_constructor<Variant>>
583
584 /***
585 * Link to a variant like data type that is discriminated via another
586 * member.
587 * @tparam Name name of JSON member to link to
588 * @tparam T type of value to construct
589 * @tparam TagMember JSON element to pass to Switcher. Does not have to be
590 * declared in member list
591 * @tparam Switcher A callable that returns an index into JsonElements when
592 * passed the TagMember object in parent member list
593 * @tparam JsonElements a json_tagged_variant_type_list, defaults to type
594 * elements of T when T is a std::variant and they are all auto mappable
595 * @tparam Constructor A callable used to construct T. The
596 * default supports normal and aggregate construction
597 * @tparam Nullable Can the member be missing or have a null value *
598 */
599 template<JSONNAMETYPE Name, typename T, typename TagMember,
600 typename Switcher, typename JsonElements = json_deduce_type,
601 typename Constructor = default_constructor<T>,
603 struct json_tagged_variant;
604
605 /***
606 * Link to a variant like data type that is discriminated via another
607 * member.
608 * @tparam Name name of JSON member to link to
609 * @tparam T type of value to construct
610 * @tparam TagMember JSON element to pass to Switcher. Does not have to be
611 * declared in member list
612 * @tparam Switcher A callable that returns an index into JsonElements when
613 * passed the TagMember object in parent member list
614 * @tparam JsonElements a json_tagged_variant_type_list, defaults to type
615 * elements of T when T is a std::variant and they are all auto mappable
616 * @tparam Constructor A callable used to construct T. The
617 * default supports normal and aggregate construction
618 * @tparam Nullable Can the member be missing or have a null value *
619 */
620 template<JSONNAMETYPE Name, typename Variant, typename TagMember,
621 typename Switcher, typename JsonElements = json_deduce_type,
622 typename Constructor = default_constructor<Variant>,
625
626 template<JSONNAMETYPE Name, typename JsonElement, typename SizeMember,
627 typename Container =
628 json_deduce_type, /* std::vector<
629typename json_details::json_deduced_type<JsonElement>::parse_to_t>,*/
630 typename Constructor =
631 json_deduce_type, /* default_constructor<Container>,*/
633 struct json_sized_array;
634
635 /***
636 * Link to a nullable variant like data type that is discriminated via
637 * another member.
638 * @tparam Name name of JSON member to link to
639 * @tparam T type of value to construct
640 * @tparam TagMember JSON element to pass to Switcher. Does not have to be
641 * @tparam Switcher A callable that returns an index into JsonElements when
642 * passed the TagMember object in parent member list
643 * @tparam JsonElements a json_tagged_variant_type_list, defaults to type
644 * elements of T when T is a std::variant and they are all auto mappable
645 * @tparam Constructor A callable used to construct T. The
646 * default supports normal and aggregate construction
647 */
648 template<JSONNAMETYPE Name, typename Variant, typename TagMember,
649 typename Switcher, typename JsonElements = json_deduce_type,
650 typename Constructor = nullable_constructor<Variant>>
652 json_tagged_variant<Name, Variant, TagMember, Switcher, JsonElements,
653 Constructor, JsonNullDefault>;
654
655 template<typename... Ts>
657 static_assert(
658 std::conjunction_v<json_details::has_json_deduced_type<Ts>...>,
659 "Missing mapping for type in tuple" );
660 using types = std::tuple<json_details::json_deduced_type<Ts>...>;
661 };
662
669 template<JSONNAMETYPE Name, typename Tuple,
670 typename Constructor = json_deduce_type,
672 typename JsonTupleTypesList = json_deduce_type>
673 struct json_tuple;
674
675 template<JSONNAMETYPE Name, typename Tuple,
676 typename Constructor = json_deduce_type,
678 typename JsonTupleTypesList = json_deduce_type>
680 json_tuple<Name, Tuple, Constructor,
681 json_details::tuple_opts_set<Options, JsonNullDefault>,
682 JsonTupleTypesList>;
683
684 namespace json_details {
685 template<typename T>
687 static_assert( is_a_json_type_v<T>,
688 "The supplied type does not have a json_data_contract" );
689 using type = T;
690 };
691
692 template<typename T>
694 } // namespace json_details
695 } // namespace DAW_JSON_VER
696} // namespace daw::json
#define JSONNAMETYPE
Definition: daw_json_name.h:111
constexpr bool is_nullable_type_v
Definition: daw_json_link_types_fwd.h:494
constexpr std::size_t find_json_element(std::initializer_list< JsonBaseParseTypes > pts)
Definition: daw_json_link_types_fwd.h:455
std::uint32_t json_options_t
Definition: daw_json_option_bits.h:23
constexpr std::conditional_t< std::conjunction< has_json_deduced_type< Ts >... >::value, json_variant_type_list< json_deduced_type< Ts >... >, missing_default_type_mapping< json_deduced_type< Ts >... > > get_variant_type_list(std::variant< Ts... > const *)
std::remove_reference_t< daw::detected_t< underlying_nullable_type, T > > detected_underlying_nullable_type
Definition: daw_json_link_types_fwd.h:488
typename ensure_mapped< T >::type ensure_mapped_t
Definition: daw_json_link_types_fwd.h:693
constexpr json_options_t number_opts_set
Definition: daw_json_parse_common.h:298
std::conditional_t< std::disjunction_v< daw::not_trait< is_nullable_json_value< Nullable > >, daw::not_trait< is_nullable_type< Tuple > > >, typename tuple_types_list< Tuple >::type, std::conditional_t< is_nullable_type_v< Tuple >, typename tuple_types_list< detected_underlying_nullable_type< Tuple > >::type, cannot_deduce_tuple_types_list< Nullable, Tuple > > > determine_tuple_element_types
Definition: daw_json_link_types_fwd.h:548
std::conditional_t< std::disjunction_v< daw::not_trait< is_nullable_json_value< Nullable > >, daw::not_trait< is_nullable_type< Variant > > >, variant_alternatives_list< Variant >, std::conditional_t< is_nullable_type_v< Variant >, variant_alternatives_list< detected_underlying_nullable_type< Variant > >, cannot_deduce_variant_element_types< Nullable, Variant > > > determine_variant_element_types
Definition: daw_json_link_types_fwd.h:535
decltype(*std::declval< T >()) underlying_nullable_type
Definition: daw_json_link_types_fwd.h:484
constexpr json_options_t json_custom_opts_set
Definition: daw_json_parse_common.h:314
daw::is_detected< underlying_nullable_type, T > is_nullable_type
Definition: daw_json_link_types_fwd.h:491
constexpr json_details::json_options_t string_opts_def
Definition: daw_json_type_options.h:163
@ Variant
Can be a literal, string, or either and allows for some customized parsing.
@ Tuple
A variant type where the Switcher is based on a submember of the class being parsed.
constexpr json_details::json_options_t json_custom_opts_def
Definition: daw_json_type_options.h:253
constexpr json_details::json_options_t tuple_opts_def
Definition: daw_json_type_options.h:219
JsonNullable
Definition: daw_json_enums.h:90
constexpr json_details::json_options_t class_opts_def
Definition: daw_json_type_options.h:208
constexpr json_details::json_options_t bool_opts_def
Definition: daw_json_type_options.h:121
constexpr json_details::json_options_t string_raw_opts_def
Definition: daw_json_type_options.h:196
constexpr JsonNullable JsonNullDefault
Definition: daw_json_enums.h:96
constexpr json_details::json_options_t number_opts_def
Definition: daw_json_type_options.h:108
Definition: daw_from_json.h:22
Definition: daw_json_link_types_iso8601.h:28
Definition: daw_json_traits.h:199
Definition: to_daw_json_string.h:195
Definition: to_daw_json_string.h:115
Definition: daw_json_link_types.h:1319
Definition: daw_json_link_types.h:553
Definition: daw_json_link_types.h:858
Definition: daw_json_link_types.h:1199
Definition: daw_json_link_types.h:785
Definition: daw_json_link_types_fwd.h:686
T type
Definition: daw_json_link_types_fwd.h:689
void i_am_variant_type_list
Definition: daw_json_link_types_fwd.h:473
std::tuple< json_deduced_type< Ts >... > types
Definition: daw_json_link_types_fwd.h:521
Definition: daw_json_link_types_fwd.h:514
Definition: daw_json_link_types_fwd.h:467
void i_am_variant_type_list
Definition: daw_json_link_types_fwd.h:468
Allow specialization of variant like types to extract the alternative pack.
Definition: daw_json_link_types_fwd.h:503
Definition: daw_json_link_types.h:1866
Definition: daw_json_link_types.h:1644
Definition: daw_json_link_types.h:1531
Definition: daw_json_link_types.h:449
Definition: daw_json_link_types.h:1418
Definition: daw_json_link_types.h:638
Definition: daw_json_link_types.h:720
Definition: daw_json_link_types.h:1106
Definition: daw_json_link_types_fwd.h:656
std::tuple< json_details::json_deduced_type< Ts >... > types
Definition: daw_json_link_types_fwd.h:660
Map a tuple like type to a a JSON tuple/heterogeneous array.
Definition: daw_json_link_types.h:1735
Definition: daw_json_link_types_fwd.h:316
fwd_pack< json_details::json_deduced_type< JsonElements >... > element_map_t
Definition: daw_json_link_types_fwd.h:319
void i_am_variant_type_list
Definition: daw_json_link_types_fwd.h:317
Definition: daw_json_link_types.h:984
Definition: daw_json_traits.h:351
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition: version.h:16