19 #include <daw/daw_traits.h>
23 #include <string_view>
24 #include <type_traits>
28 namespace json_details {
29 template<
typename output_t,
auto... PolicyFlags,
typename WritableType>
30 DAW_ATTRIB_INLINE constexpr
auto apply_policy_flags( WritableType &&it ) {
32 daw::remove_cvref_t<WritableType>> ) {
33 if constexpr(
sizeof...( PolicyFlags ) == 0 ) {
37 json_details::serialization::set_bits(
38 output_t::policy_flags( ),
39 PolicyFlags... )>( it.get( ) );
43 daw::remove_cvref_t<WritableType>,
44 options::output_flags_t<PolicyFlags...>::value>( it );
49 template<
typename JsonClass,
typename Value,
typename WritableType,
52 daw::remove_cvref_t<WritableType>> )>
55 constexpr
daw::rvalue_to_value_t<WritableType>
to_json(
56 Value const &value, WritableType &&it,
57 options::output_flags_t<PolicyFlags...> ) {
59 using json_class_t =
typename daw::conditional_t<
60 std::is_same_v<use_default, JsonClass>,
61 json_details::ident_trait<json_details::json_deduced_type, Value>,
62 json_details::ident_trait<json_details::json_deduced_type,
65 using output_t = daw::rvalue_to_value_t<WritableType>;
66 if constexpr( std::is_pointer_v<daw::remove_cvref_t<WritableType>> ) {
70 json_details::apply_policy_flags<output_t, PolicyFlags...>( it );
72 return json_details::member_to_string<json_class_t>( out_it, value )
76 template<
typename JsonClass,
typename Value,
auto... PolicyFlags>
77 inline std::string
to_json( Value
const &value,
78 options::output_flags_t<PolicyFlags...> flgs ) {
79 std::string result{ };
80 result.reserve( 4096 );
81 (void)
to_json( value, result, flgs );
82 result.shrink_to_fit( );
86 template<
typename JsonElement,
typename Container,
typename WritableType,
89 daw::remove_cvref_t<WritableType>> )>
93 Container const &c, WritableType &&it,
94 options::output_flags_t<PolicyFlags...> ) {
96 daw::traits::is_container_like_v<daw::remove_cvref_t<Container>>,
97 "Supplied container must support begin( )/end( )" );
98 using output_t = daw::rvalue_to_value_t<WritableType>;
100 if constexpr( std::is_pointer_v<daw::remove_cvref_t<output_t>> ) {
105 daw::remove_cvref_t<WritableType>> ) {
106 if constexpr(
sizeof...( PolicyFlags ) == 0 ) {
109 return serialization_policy<
typename output_t::iterator_type,
110 json_details::serialization::set_bits(
111 output_t::policy_flags( ),
112 PolicyFlags... )>( it.get( ) );
115 return serialization_policy<
116 daw::remove_cvref_t<WritableType>,
117 options::output_flags_t<PolicyFlags...>::value>( it );
121 out_it.add_indent( );
124 auto first = std::begin( c );
125 auto last = std::end( c );
126 bool const has_elements = first != last;
127 while( first != last ) {
128 (void)[&out_it](
auto &&v ) {
129 using v_type = DAW_TYPEOF( v );
130 using JsonMember =
typename daw::conditional_t<
131 std::is_same_v<JsonElement, use_default>,
132 json_details::ident_trait<json_details::json_deduced_type, v_type>,
133 json_details::ident_trait<json_details::json_deduced_type,
139 missing_json_data_contract_for_or_unknown_type<JsonElement>>,
140 "Unable to detect unnamed mapping" );
142 out_it.next_member( );
144 out_it = json_details::member_to_string<JsonMember>( out_it, v );
148 if( first != last ) {
153 out_it.del_indent( );
155 out_it.output_newline( );
158 return out_it.get( );
161 template<
typename JsonElement,
typename Container,
auto... PolicyFlags>
164 options::output_flags_t<PolicyFlags...> flgs ) {
165 static_assert( not std::is_same_v<std::string, JsonElement> );
166 std::string result{ };
167 result.reserve( 4096 );
169 result.shrink_to_fit( );
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
#define DAW_JSON_ENABLEIF2(...)
constexpr bool is_serialization_policy_v
std::string to_json(Value const &value, options::output_flags_t< PolicyFlags... > flgs)
std::string to_json_array(Container const &c, options::output_flags_t< PolicyFlags... > flgs)
constexpr bool is_writable_output_type_v
Customization point traits.
DAW_JSON_REQUIRES(boost::describe::has_describe_members< T >::value and use_boost_describe_v< T >) struct json_data_contract< T >
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.