41
41
#include " json.h"
42
42
43
43
#include " id_variant.h"
44
+ #include " collapsingvector.h"
44
45
45
46
#include < optional>
46
47
#include < variant>
47
48
#include < string>
49
+ #include < string_view>
48
50
49
51
50
52
@@ -71,6 +73,32 @@ void variant_to_json(json_t& j, const std::variant<Ts...> &data)
71
73
}
72
74
73
75
76
+ template <typename T>
77
+ void collapsingvector_to_json (json_t & j, const CollapsingVector<T>& data, std::optional<std::string_view> key)
78
+ {
79
+ if (key) {
80
+ data.to_json (j[*key]);
81
+ } else {
82
+ data.to_json (j);
83
+ }
84
+ }
85
+
86
+ template <typename T>
87
+ void collapsing_from_json (const json_t & j, CollapsingVector<T>& data, std::optional<std::string_view> key)
88
+ {
89
+ if (key) {
90
+ const auto it = j.find (*key);
91
+ if (it == j.end ()) {
92
+ // Key not in json
93
+ throw std::runtime_error (" Key " + std::string (*key) + " not found in json" );
94
+ }
95
+ data.from_json (*it);
96
+ } else {
97
+ data.from_json (j);
98
+ }
99
+ }
100
+
101
+
74
102
template <typename T>
75
103
void optional_to_json (json_t & j, const std::optional<T>& data, std::optional<std::string_view> key)
76
104
{
@@ -149,13 +177,20 @@ constexpr bool is_id_variant = false;
149
177
template <char const * id_field_name, typename Seq>
150
178
constexpr bool is_id_variant<IdVariant<id_field_name, Seq>> = true ;
151
179
180
+ template <typename >
181
+ constexpr bool is_collapsing_vector = false ;
182
+ template <typename T>
183
+ constexpr bool is_collapsing_vector<CollapsingVector<T>> = true ;
184
+
152
185
153
186
template <typename T> void extended_to_json (const char * key, json_t & j, const T& value)
154
187
{
155
188
if constexpr (is_optional<T>)
156
189
optional_to_json (j, value, key);
157
190
else if constexpr (is_id_variant<T>)
158
191
id_variant_to_json (j, value, key);
192
+ else if constexpr (is_collapsing_vector<T>)
193
+ collapsingvector_to_json (j, value, key);
159
194
else if constexpr (is_variant<T>)
160
195
variant_to_json (j, value);
161
196
else
@@ -168,6 +203,8 @@ template <typename T> void extended_from_json(const char* key, const json_t& j,
168
203
optional_from_json (j, value, key);
169
204
else if constexpr (is_id_variant<T>)
170
205
id_variant_from_json (j, value, key);
206
+ else if constexpr (is_collapsing_vector<T>)
207
+ collapsing_from_json (j, value, key);
171
208
else if constexpr (is_variant<T>)
172
209
variant_from_json<T>(j, value);
173
210
else
@@ -220,6 +257,20 @@ template <typename... Ts> struct adl_serializer<std::variant<Ts...>>
220
257
};
221
258
222
259
260
+ template <typename T> struct adl_serializer <DRAMUtils::util::CollapsingVector<T>>
261
+ {
262
+ static void to_json (json_t & j, const DRAMUtils::util::CollapsingVector<T>& data)
263
+ {
264
+ DRAMUtils::util::collapsingvector_to_json<T>(j, data, std::nullopt );
265
+ }
266
+
267
+ static void from_json (const json_t & j, DRAMUtils::util::CollapsingVector<T>& data)
268
+ {
269
+ (DRAMUtils::util::collapsing_from_json<T>(j, data, std::nullopt ));
270
+ }
271
+ };
272
+
273
+
223
274
template <const char * id_field_name, typename Seq> struct adl_serializer <DRAMUtils::util::IdVariant<id_field_name, Seq>>
224
275
{
225
276
static void to_json (json_t & j, const DRAMUtils::util::IdVariant<id_field_name, Seq>& data)
0 commit comments