forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_ops.h
418 lines (365 loc) · 13.9 KB
/
json_ops.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#ifndef CPPREST_JSON_OPS_H
#define CPPREST_JSON_OPS_H
#include "cpprest/json.h"
// more comparison operators for json::number, json::object, json::array and json::value
namespace web
{
namespace json
{
inline bool operator!=(const web::json::number& lhs, const web::json::number& rhs)
{
return !(lhs == rhs);
}
inline bool operator<(const web::json::number& lhs, const web::json::number& rhs)
{
if (!lhs.is_integral() && !rhs.is_integral()) return lhs.to_double() < rhs.to_double();
if (lhs.is_int64() && rhs.is_int64()) return lhs.to_int64() < rhs.to_int64();
if (lhs.is_uint64() && rhs.is_uint64()) return lhs.to_uint64() < rhs.to_uint64();
return false;
}
inline bool operator>(const web::json::number& lhs, const web::json::number& rhs)
{
return rhs < lhs;
}
inline bool operator<=(const web::json::number& lhs, const web::json::number& rhs)
{
return !(rhs > lhs);
}
inline bool operator>=(const web::json::number& lhs, const web::json::number& rhs)
{
return !(lhs < rhs);
}
// json::object comparison respects keep_order
inline bool operator==(const web::json::object& lhs, const web::json::object& rhs)
{
if (lhs.size() != rhs.size())
return false;
using std::begin;
using std::end;
return std::equal(begin(lhs), end(lhs), begin(rhs));
}
inline bool operator!=(const web::json::object& lhs, const web::json::object& rhs)
{
return !(lhs == rhs);
}
inline bool operator<(const web::json::object& lhs, const web::json::object& rhs)
{
using std::begin;
using std::end;
return std::lexicographical_compare(begin(lhs), end(lhs), begin(rhs), end(rhs));
}
inline bool operator>(const web::json::object& lhs, const web::json::object& rhs)
{
return rhs < lhs;
}
inline bool operator<=(const web::json::object& lhs, const web::json::object& rhs)
{
return !(rhs > lhs);
}
inline bool operator>=(const web::json::object& lhs, const web::json::object& rhs)
{
return !(lhs < rhs);
}
inline bool operator==(const web::json::array& lhs, const web::json::array& rhs)
{
if (lhs.size() != rhs.size())
return false;
using std::begin;
using std::end;
return std::equal(begin(lhs), end(lhs), begin(rhs));
}
inline bool operator!=(const web::json::array& lhs, const web::json::array& rhs)
{
return !(lhs == rhs);
}
inline bool operator<(const web::json::array& lhs, const web::json::array& rhs)
{
using std::begin;
using std::end;
return std::lexicographical_compare(begin(lhs), end(lhs), begin(rhs), end(rhs));
}
inline bool operator>(const web::json::array& lhs, const web::json::array& rhs)
{
return rhs < lhs;
}
inline bool operator<=(const web::json::array& lhs, const web::json::array& rhs)
{
return !(rhs > lhs);
}
inline bool operator>=(const web::json::array& lhs, const web::json::array& rhs)
{
return !(lhs < rhs);
}
inline bool operator<(const web::json::value& lhs, const web::json::value& rhs)
{
if (lhs.type() < rhs.type()) return true;
if (rhs.type() == lhs.type())
{
switch (lhs.type())
{
case web::json::value::Number: return lhs.as_number() < rhs.as_number();
case web::json::value::Boolean: return lhs.as_bool() < rhs.as_bool();
case web::json::value::String: return lhs.as_string() < rhs.as_string();
case web::json::value::Object: return lhs.as_object() < rhs.as_object();
case web::json::value::Array: return lhs.as_array() < rhs.as_array();
case web::json::value::Null: return false;
default: return false;
}
}
return false;
}
inline bool operator>(const web::json::value& lhs, const web::json::value& rhs)
{
return rhs < lhs;
}
inline bool operator<=(const web::json::value& lhs, const web::json::value& rhs)
{
return !(rhs > lhs);
}
inline bool operator>=(const web::json::value& lhs, const web::json::value& rhs)
{
return !(lhs < rhs);
}
}
}
// json::value 'as' accessor
namespace web
{
namespace json
{
namespace details
{
// the 'as' accessor is implemented by means of a value_as function object template
// in order to pass through the various const-ref, ref or value return types
template <typename T> struct value_as;
template <> struct value_as<web::json::value>
{
const web::json::value& operator()(const web::json::value& value) const { return value; }
web::json::value& operator()(web::json::value& value) const { return value; }
};
template <> struct value_as<web::json::number>
{
const web::json::number& operator()(const web::json::value& value) const { return value.as_number(); }
};
template <> struct value_as<web::json::object>
{
const web::json::object& operator()(const web::json::value& value) const { return value.as_object(); }
web::json::object& operator()(web::json::value& value) const { return value.as_object(); }
};
template <> struct value_as<web::json::array>
{
const web::json::array& operator()(const web::json::value& value) const { return value.as_array(); }
web::json::array& operator()(web::json::value& value) const { return value.as_array(); }
};
template <> struct value_as<utility::string_t>
{
const utility::string_t& operator()(const web::json::value& value) const { return value.as_string(); }
};
template <> struct value_as<bool>
{
bool operator()(const web::json::value& value) const { return value.as_bool(); }
};
template <> struct value_as<int>
{
int operator()(const web::json::value& value) const { return value.as_integer(); }
};
template <> struct value_as<double>
{
double operator()(const web::json::value& value) const { return value.as_double(); }
};
template <> struct value_as<uint32_t>
{
uint32_t operator()(const web::json::value& value) const { return value.as_number().to_uint32(); }
};
template <> struct value_as<int64_t>
{
int64_t operator()(const web::json::value& value) const { return value.as_number().to_int64(); }
};
template <> struct value_as<uint64_t>
{
uint64_t operator()(const web::json::value& value) const { return value.as_number().to_uint64(); }
};
}
template <typename T, typename V>
inline auto as(V& value) -> decltype(details::value_as<T>{}(value))
{
return details::value_as<T>{}(value);
}
}
}
// more json::value accessors and operations
namespace web
{
namespace json
{
// insert a field into the specified value (which must be an object or null),
// only if the value doesn't already contain a field with that key
template <typename KeyValuePair>
inline bool insert(web::json::value& value, const KeyValuePair& field)
{
if (!value.has_field(field.first))
{
value[field.first] = web::json::value{ field.second };
return true;
}
else
{
return false;
}
}
// insert each field into the specified value
template <typename InputIterator>
inline void insert(web::json::value& value, InputIterator first, InputIterator last)
{
for (auto it = first; last != it; ++it)
{
insert(value, *it);
}
}
template <typename Value>
inline void push_back(web::json::value& value, const Value& element)
{
value[value.size()] = web::json::value{ element };
}
inline void push_back(web::json::value& value, web::json::value&& element)
{
value[value.size()] = std::move(element);
}
inline void pop_back(web::json::value& value)
{
value.erase(value.size() - 1);
}
// deprecated, since pop_front is only found on std containers with fast deletion at the beginning
inline void pop_front(web::json::value& value)
{
value.erase(0);
}
inline web::json::value& front(web::json::value& value)
{
return value.at(0);
}
inline const web::json::value& front(const web::json::value& value)
{
return value.at(0);
}
inline web::json::value& back(web::json::value& value)
{
return value.at(value.size() - 1);
}
inline const web::json::value& back(const web::json::value& value)
{
return value.at(value.size() - 1);
}
inline bool empty(const web::json::value& value)
{
return 0 == value.size();
}
}
}
// json::array accessors and operations
namespace web
{
namespace json
{
template <typename Value>
inline void push_back(web::json::array& value, const Value& element)
{
value[value.size()] = web::json::value{ element };
}
inline void push_back(web::json::array& value, web::json::value&& element)
{
value[value.size()] = std::move(element);
}
inline void pop_back(web::json::array& value)
{
value.erase(value.size() - 1);
}
inline web::json::value& front(web::json::array& value)
{
return value.at(0);
}
inline const web::json::value& front(const web::json::array& value)
{
return value.at(0);
}
inline web::json::value& back(web::json::array& value)
{
return value.at(value.size() - 1);
}
inline const web::json::value& back(const web::json::array& value)
{
return value.at(value.size() - 1);
}
inline bool empty(const web::json::array& value)
{
return 0 == value.size();
}
}
}
// json::object accessors and operations
namespace web
{
namespace json
{
inline bool empty(const web::json::object& value)
{
return value.empty();
}
}
}
// json::value initialization
// web::json::value has a rather limited interface for the construction of objects and arrays;
// value_of allows construction of both objects and array from a braced-init-list
namespace web
{
namespace json
{
namespace details
{
// value_init shouldn't be used explicitly, it exists to allow implicit conversions
// from bool and string in braced-init-list arguments to web::json::value_of
struct value_init : public value
{
using value::value;
value_init() {}
value_init(const value& v) : value(v) {}
value_init(value&& v) : value(std::move(v)) {}
value_init(bool b) : value(b) {}
value_init(utility::string_t s) : value(std::move(s)) {}
value_init(const utility::char_t* s) : value(s) {}
private:
// this prevents the surprising implicit conversion from e.g. char* to bool
template <typename T> value_init(T*) = delete;
};
}
// this function allows terse construction of object values using a braced-init-list
// e.g. value_of({ { U("foo"), 42 }, { U("bar"), U("baz") }, { U("qux"), true } })
inline web::json::value value_of(std::initializer_list<std::pair<utility::string_t, web::json::details::value_init>> fields, bool keep_order = false, bool omit_empty_keys = true)
{
web::json::value result = web::json::value::object(keep_order);
for (auto& field : fields)
{
if (!field.first.empty() || !omit_empty_keys) insert(result, field);
}
return result;
}
// this function allows terse construction of array values using a braced-init-list
// (it is a template specialization to resolve ambiguous calls in favour of the non-template function,
// since gcc considers the explicit two-arg value constructors for elements of the braced-init-list)
template <typename = void>
inline web::json::value value_of(std::initializer_list<web::json::details::value_init> elements)
{
web::json::value result = web::json::value::array();
for (auto& element : elements)
{
push_back(result, element);
}
return result;
}
namespace details { class ambiguous; }
// this overload intentionally makes value_of({}) ambiguous
// since it's unclear whether that's intended to be an empty object or an empty array
web::json::value value_of(std::initializer_list<details::ambiguous>);
}
}
#endif