15#if defined( DAW_JSON_HAS_REFLECTION )
19#include <daw/daw_bind_args_at.h>
20#include <daw/daw_concepts.h>
21#include <daw/daw_move.h>
22#include <daw/daw_pipelines.h>
34 struct refl_ignored_value : refl_annotation_base, refl_ignored_base {
37 explicit constexpr refl_ignored_value( D value )
38 : default_value( value ) {}
41 constexpr operator T( )
const {
42 if constexpr( std::is_convertible_v<D, T> ) {
44 }
else if constexpr(
requires( D v ) {
45 { v( ) }->std::convertible_to<T>;
47 return default_value( );
52 struct refl_ignored : refl_annotation_base, refl_ignored_base {
53 explicit consteval refl_ignored( ) =
default;
56 static consteval auto operator( )( T &&rhs ) {
57 return refl_ignored_value<T>{ DAW_FWD( rhs ) };
61 consteval operator T( )
const {
66 struct member_reflection_t {};
68 template<
typename T, std::
size_t Idx>
69 using submember_type_t = std::tuple_element_t<Idx, to_tuple_t<T>>;
72 constexpr E enum_from_string( std::string_view name ) {
73 template for(
constexpr auto enumerator : enumerators_of( ^^E ) ) {
75 if( name == identifier_of( enumerator ) ) {
76 return [:enumerator:];
79 daw_json_ensure( daw::pipelines::Contains( name )( enumerators_of( ^^E ) ),
80 ErrorReason::InvalidString );
84 constexpr std::string_view enum_to_string( E value ) {
85 static constexpr auto enums =
86 reflect_constant_array( enumerators_of( ^^E ) );
87 template for(
constexpr auto enumerator : [:enums:] ) {
88 if( value == [:enumerator:] ) {
89 return identifier_of( enumerator );
92 return std::string_view{ };
96 struct reflect_enum_as_string {
97 static constexpr E operator( )( std::string_view name ) {
98 return enum_from_string<E>( name );
101 static constexpr std::string_view operator( )( E value ) {
102 return enum_to_string( value );
105 template<
typename T, std::size_t... Idx>
106 consteval std::meta::info
107 get_json_members_list_impl( std::index_sequence<Idx...> ) {
108 return ^^json_member_list<get_member_link_t<T, Idx>...>;
112 consteval std::meta::info get_json_member_list( ) {
113 static constexpr auto sz =
114 get_non_ignored_reflectible_members<T>( ).size( );
115 return get_json_members_list_impl<T>( std::make_index_sequence<sz>{ } );
118 template<EnumType E, json_options_t Options>
120 reflect_enum_as_string<E>, Options> {
123 template<Reflectable T>
124 consteval bool has_reflected_submembers( ) {
125 static constexpr auto members = [:as_stdarray(
126 get_reflectible_members<T>( ) ):];
127 template for(
constexpr auto member : members ) {
128 if( not annotations_of_with_base_type( member, ^^refl_annotation_base )
142 struct reflect_base_t {
146 template<json_name Name>
147 static constexpr auto rename = refl_details::refl_rename{ Name.m_data };
149 template<
typename JsonMember>
150 static constexpr auto map_as = refl_details::refl_map_as{ ^^JsonMember };
155 static constexpr auto ignored = refl_details::refl_ignored{ };
158 template<json_options_t Options>
159 static constexpr auto enum_string_with_options =
160 refl_details::refl_enum_string{ Options };
162 static constexpr auto enum_string =
163 enum_string_with_options<json_custom_opts_def>;
165 struct reflect_all_t : reflect_base_t, refl_details::reflect_all_t {};
166 struct reflect_t : reflect_base_t {
167 static constexpr auto unchecked = reflect_all_t{ };
170 inline constexpr auto reflect = reflect_t{ };
175 inline constexpr bool enable_reflection_for =
false;
178 concept ReflectionEnabled =
179 enable_reflection_for<T> or
180 refl_details::has_annotation<reflect_base_t, T>( ) or
181 refl_details::has_reflected_submembers<T>( );
183 template<ReflectionEnabled T>
184 struct json_data_contract<T> {
185 using constructor_t = refl_details::reflected_constructor<T>;
187 using type =
typename[:refl_details::get_json_member_list<T>( ):];
189 DAW_ATTRIB_INLINE
static constexpr auto to_json_data( T
const &value ) {
190 return refl_details::to_tuple( value );
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
json_base::json_custom< T, FromJsonConverter, ToJsonConverter, Options > json_custom_no_name
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.