DAW JSON Link
Loading...
Searching...
No Matches
daw_to_json.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
12
18
19#include <daw/daw_enable_requires.h>
20#include <daw/daw_traits.h>
21
22#include <iterator>
23#include <string>
24#include <type_traits>
25
26namespace daw::json {
27 inline namespace DAW_JSON_VER {
28 namespace json_details {
29 template<typename output_t, auto... PolicyFlags, typename WritableType>
30 DAW_ATTRIB_INLINE constexpr auto apply_policy_flags( WritableType &&it ) {
31 if constexpr( is_serialization_policy_v<
32 daw::remove_cvref_t<WritableType>> ) {
33 if constexpr( sizeof...( PolicyFlags ) == 0 ) {
34 return it;
35 } else {
36 return serialization_policy<typename output_t::iterator_type,
37 json_details::serialization::set_bits(
38 output_t::policy_flags( ),
39 PolicyFlags... )>( it.get( ) );
40 }
41 } else {
43 daw::remove_cvref_t<WritableType>,
44 options::output_flags_t<PolicyFlags...>::value>( it );
45 }
46 }
47 } // namespace json_details
48
49 template<
50 typename JsonClass, typename Value, typename WritableType,
51 auto... PolicyFlags DAW_ENABLEIF2( concepts::is_writable_output_type_v<
52 daw::remove_cvref_t<WritableType>> )>
54 concepts::is_writable_output_type_v<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...> ) {
58
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,
63 JsonClass>>::type;
64
65 using output_t = daw::rvalue_to_value_t<WritableType>;
66 if constexpr( std::is_pointer_v<daw::remove_cvref_t<WritableType>> ) {
67 daw_json_ensure( it != nullptr, ErrorReason::NullOutputIterator );
68 }
69 auto out_it =
70 json_details::apply_policy_flags<output_t, PolicyFlags...>( it );
71
72 return json_details::member_to_string<json_class_t>( out_it, value )
73 .get( );
74 }
75
76 template<typename JsonClass, typename Value, auto... PolicyFlags>
77 DAW_CPP20_CX_ALLOC std::string
78 to_json( Value const &value,
79 options::output_flags_t<PolicyFlags...> flgs ) {
80 std::string result{ };
81 result.reserve( 4096 );
82 (void)to_json( value, result, flgs );
83 result.shrink_to_fit( );
84 return result;
85 }
86
87 template<
88 typename JsonElement, typename Container, typename WritableType,
89 auto... PolicyFlags DAW_ENABLEIF2( concepts::is_writable_output_type_v<
90 daw::remove_cvref_t<WritableType>> )>
92 concepts::is_writable_output_type_v<daw::remove_cvref_t<WritableType>> )
93 constexpr daw::rvalue_to_value_t<WritableType> to_json_array(
94 Container const &c, WritableType &&it,
95 options::output_flags_t<PolicyFlags...> ) {
96 static_assert(
97 daw::traits::is_container_like_v<daw::remove_cvref_t<Container>>,
98 "Supplied container must support begin( )/end( )" );
99 using output_t = daw::rvalue_to_value_t<WritableType>;
100
101 if constexpr( std::is_pointer_v<daw::remove_cvref_t<output_t>> ) {
102 daw_json_ensure( it != nullptr, ErrorReason::InvalidNull );
103 }
104 auto out_it = [&] {
105 if constexpr( is_serialization_policy_v<
106 daw::remove_cvref_t<WritableType>> ) {
107 if constexpr( sizeof...( PolicyFlags ) == 0 ) {
108 return it;
109 } else {
110 return serialization_policy<typename output_t::iterator_type,
111 json_details::serialization::set_bits(
112 output_t::policy_flags( ),
113 PolicyFlags... )>( it.get( ) );
114 }
115 } else {
116 return serialization_policy<
117 daw::remove_cvref_t<WritableType>,
118 options::output_flags_t<PolicyFlags...>::value>( it );
119 }
120 }( );
121 out_it.put( '[' );
122 out_it.add_indent( );
123 // Not const & as some types(vector<bool>::const_reference are not ref
124 // types
125 auto first = std::begin( c );
126 auto last = std::end( c );
127 bool const has_elements = first != last;
128 while( first != last ) {
129 (void)[&out_it]( auto &&v ) {
130 using v_type = DAW_TYPEOF( v );
131 using JsonMember = typename daw::conditional_t<
132 std::is_same_v<JsonElement, use_default>,
133 json_details::ident_trait<json_details::json_deduced_type, v_type>,
134 json_details::ident_trait<json_details::json_deduced_type,
135 JsonElement>>::type;
136
137 static_assert(
138 not std::is_same_v<
139 JsonMember,
140 missing_json_data_contract_for_or_unknown_type<JsonElement>>,
141 "Unable to detect unnamed mapping" );
142 // static_assert( not std::is_same_v<JsonElement, JsonMember> );
143 out_it.next_member( );
144
145 out_it = json_details::member_to_string<JsonMember>( out_it, v );
146 }
147 ( *first );
148 ++first;
149 if( first != last ) {
150 out_it.put( ',' );
151 }
152 }
153 // The last character will be a ',' prior to this
154 out_it.del_indent( );
155 if( has_elements ) {
156 out_it.output_newline( );
157 }
158 out_it.put( ']' );
159 return out_it.get( );
160 }
161
162 template<typename JsonElement, typename Container, auto... PolicyFlags>
163 DAW_CPP20_CX_ALLOC std::string
164 to_json_array( Container const &c,
165 options::output_flags_t<PolicyFlags...> flgs ) {
166 static_assert( not std::is_same_v<std::string, JsonElement> );
167 auto result = std::string{ };
168 result.reserve( 4096 );
169 (void)to_json_array( c, result, flgs );
170 result.shrink_to_fit( );
171 return result;
172 }
173 } // namespace DAW_JSON_VER
174} // namespace daw::json
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
DAW_REQUIRES(daw::json::json_details::is_container_opted_into_json_iostreams_v< Container >) std
An opt in ostream interface for containers of types that have JSON mappings.
DAW_CPP20_CX_ALLOC std::string to_json(Value const &value, options::output_flags_t< PolicyFlags... > flgs)
Definition daw_to_json.h:78
DAW_CPP20_CX_ALLOC std::string to_json_array(Container const &c, options::output_flags_t< PolicyFlags... > flgs)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20