-
Notifications
You must be signed in to change notification settings - Fork 401
/
jsonhelper.hpp
950 lines (850 loc) · 30.3 KB
/
jsonhelper.hpp
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
#pragma once
#include <string>
#include <cstring>
#include <cstdint>
#include <utility>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/string.hpp>
#include <openvpn/common/number.hpp>
#include <openvpn/common/file.hpp>
#include <openvpn/common/jsonlib.hpp>
#include <openvpn/common/jsonhelperfmt.hpp>
#include <openvpn/buffer/bufstr.hpp>
#include <openvpn/buffer/bufstream.hpp>
#ifndef HAVE_JSON
#error no JSON library available
#endif
namespace openvpn::json {
OPENVPN_EXCEPTION(json_parse);
/* Workaround warnings in gcc 13+, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109642
* Could use [[gnu::no_dangling]] in gcc 14+ but that would then cause warnings in older
* compilers...
*/
#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 13
#define DISABLE_DANGLING_WARNINGS() \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wdangling-reference\"")
#define REENABLE_DANGLING_WARNINGS() \
_Pragma("GCC diagnostic pop")
#else
#define DISABLE_DANGLING_WARNINGS()
#define REENABLE_DANGLING_WARNINGS()
#endif
template <typename TITLE>
inline Json::Value parse(const std::string &str, const TITLE &title)
{
#ifdef OPENVPN_JSON
return Json::Value::parse(str, StringTempl::to_string(title));
#else
Json::CharReaderBuilder builder;
builder["collectComments"] = false;
Json::Value root;
std::string errors;
std::istringstream instr(str);
if (!Json::parseFromStream(builder, instr, &root, &errors))
throw json_parse(StringTempl::to_string(title) + " : " + errors);
return root;
#endif
}
inline Json::Value parse(const std::string &str)
{
return parse(str, "json");
}
inline Json::Value parse_from_file(const std::string &fn)
{
return parse(read_text_utf8(fn), fn);
}
template <typename BUFFER, typename TITLE>
inline Json::Value parse_from_buffer(const BUFFER &buf, const TITLE &title)
{
#ifdef OPENVPN_JSON
return Json::Value::parse(buf, StringTempl::to_string(title));
#else
Json::CharReaderBuilder builder;
builder["collectComments"] = false;
Json::Value root;
std::string errors;
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (!reader->parse(reinterpret_cast<const char *>(buf.c_data()), reinterpret_cast<const char *>(buf.c_data()) + buf.size(), &root, &errors))
throw json_parse(StringTempl::to_string(title) + " : " + errors);
return root;
#endif
}
#ifdef OPENVPN_JSON_INTERNAL
DISABLE_DANGLING_WARNINGS()
template <typename NAME, typename TITLE>
inline const Json::Value &cast(const Json::ValueType target_type,
const Json::Value &value,
const NAME &name,
const bool optional,
const TITLE &title)
{
if (value.isNull())
{
if (optional)
return value;
throw json_parse(Json::Value::type_string(target_type) + " cast " + fmt_name(name, title) + " is null");
}
if (!value.isConvertibleTo(target_type))
throw json_parse(Json::Value::type_string(target_type) + " cast " + fmt_name(name, title) + " is of incorrect type (" + value.type_string() + ')');
return value;
}
REENABLE_DANGLING_WARNINGS()
template <typename NAME>
inline const Json::Value &cast(const Json::ValueType target_type,
const Json::Value &value,
const NAME &name,
const bool optional)
{
return cast(target_type, value, name, optional, nullptr);
}
template <typename NAME, typename TITLE>
inline Json::Value cast(const Json::ValueType target_type,
Json::Value &&value,
const NAME &name,
const bool optional,
const TITLE &title)
{
Json::Value ret = std::move(value);
cast(target_type, ret, name, optional, title);
return ret;
}
template <typename NAME>
inline Json::Value cast(const Json::ValueType target_type,
Json::Value &&value,
const NAME &name,
const bool optional)
{
return cast(target_type, std::move(value), name, optional, nullptr);
}
#endif
template <typename T, typename NAME>
inline void from_vector(Json::Value &root, const T &vec, const NAME &name)
{
Json::Value array(Json::arrayValue);
for (auto &e : vec)
array.append(e.to_json());
if (array.size())
root[name] = std::move(array);
}
template <typename TITLE>
inline void assert_dict(const Json::Value &obj, const TITLE &title)
{
if (!obj.isObject())
throw json_parse(fmt_name_cast(title) + " is not a JSON dictionary");
}
template <typename TITLE>
inline bool is_dict(const Json::Value &obj, const TITLE &title)
{
if (obj.isNull())
return false;
assert_dict(obj, title);
return true;
}
template <typename NAME>
inline bool exists(const Json::Value &root, const NAME &name)
{
if (!root.isObject())
return false;
return !root[name].isNull();
}
template <typename NAME>
inline bool string_exists(const Json::Value &root, const NAME &name)
{
if (!root.isObject())
return false;
return root[name].isString();
}
template <typename T, typename NAME, typename TITLE>
inline void to_vector(const Json::Value &root, T &vec, const NAME &name, const TITLE &title)
{
const Json::Value &array = root[name];
if (array.isNull())
return;
if (!array.isArray())
throw json_parse("array " + fmt_name(name, title) + " is of incorrect type");
for (unsigned int i = 0; i < array.size(); ++i)
{
vec.emplace_back();
vec.back().from_json(array[i], fmt_name(name, title));
}
}
template <typename NAME, typename TITLE>
inline std::string get_string(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("string " + fmt_name(name, title) + " is missing");
if (!value.isString())
throw json_parse("string " + fmt_name(name, title) + " is of incorrect type");
return value.asString();
}
template <typename NAME>
inline std::string get_string(const Json::Value &root, const NAME &name)
{
return get_string(root, name, nullptr);
}
#ifdef OPENVPN_JSON_INTERNAL
template <typename NAME, typename TITLE>
inline const std::string &get_string_ref(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("string " + fmt_name(name, title) + " is missing");
if (!value.isString())
throw json_parse("string " + fmt_name(name, title) + " is of incorrect type");
return value.asStringRef();
}
template <typename NAME>
inline const std::string &get_string_ref(const Json::Value &root, const NAME &name)
{
return get_string_ref(root, name, nullptr);
}
// for safety, define an rvalue ref overload that falls back to get_string()
template <typename NAME, typename TITLE>
inline std::string get_string_ref(Json::Value &&root,
const NAME &name,
const TITLE &title)
{
return get_string(root, name, title);
}
template <typename NAME, typename TITLE>
inline const std::string *get_string_ptr(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return nullptr;
if (!value.isString())
throw json_parse("string " + fmt_name(name, title) + " is of incorrect type");
return value.asStringPtr();
}
template <typename NAME>
inline const std::string *get_string_ptr(const Json::Value &root, const NAME &name)
{
return get_string_ptr(root, name, nullptr);
}
#endif
template <typename NAME, typename TITLE>
inline std::string get_string_optional(const Json::Value &root,
const NAME &name,
const std::string &default_value,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return default_value;
if (!value.isString())
throw json_parse("string " + fmt_name(name, title) + " is of incorrect type");
return value.asString();
}
template <typename NAME>
inline std::string get_string_optional(const Json::Value &root,
const NAME &name,
const std::string &default_value)
{
return get_string_optional(root, name, default_value, nullptr);
}
template <typename TITLE>
inline std::string get_string_from_array(const Json::Value &root,
const Json::ArrayIndex index,
const TITLE &title)
{
const Json::Value &value = root[index];
if (value.isNull())
throw json_parse("string " + fmt_name(index, title) + " is missing");
if (!value.isString())
throw json_parse("string " + fmt_name(index, title) + " is of incorrect type");
return value.asString();
}
inline std::string get_string_from_array(const Json::Value &root,
const Json::ArrayIndex index)
{
return get_string_from_array(root, index, nullptr);
}
template <typename NAME, typename TITLE>
inline int get_int(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("int " + fmt_name(name, title) + " is missing");
if (!value.isInt())
throw json_parse("int " + fmt_name(name, title) + " is of incorrect type");
return value.asInt();
}
template <typename NAME>
inline int get_int(const Json::Value &root, const NAME &name)
{
return get_int(root, name, nullptr);
}
template <typename NAME, typename TITLE>
inline int get_int_optional(const Json::Value &root,
const NAME &name,
const int default_value,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return default_value;
if (!value.isInt())
throw json_parse("int " + fmt_name(name, title) + " is of incorrect type");
return value.asInt();
}
template <typename NAME>
inline int get_int_optional(const Json::Value &root,
const NAME &name,
const int default_value)
{
return get_int_optional(root, name, default_value, nullptr);
}
template <typename NAME, typename TITLE>
inline unsigned int get_uint(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("uint " + fmt_name(name, title) + " is missing");
if (!value.isUInt())
throw json_parse("uint " + fmt_name(name, title) + " is of incorrect type");
return value.asUInt();
}
template <typename NAME>
inline unsigned int get_uint(const Json::Value &root, const NAME &name)
{
return get_uint(root, name, nullptr);
}
template <typename NAME, typename TITLE>
inline unsigned int get_uint_optional(const Json::Value &root,
const NAME &name,
const unsigned int default_value,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return default_value;
if (!value.isUInt())
throw json_parse("uint " + fmt_name(name, title) + " is of incorrect type");
return value.asUInt();
}
template <typename NAME>
inline unsigned int get_uint_optional(const Json::Value &root,
const NAME &name,
const unsigned int default_value)
{
return get_uint_optional(root, name, default_value, nullptr);
}
template <typename NAME, typename TITLE>
inline unsigned int get_uint_via_string(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("uint-via-string " + fmt_name(name, title) + " is missing");
if (!value.isString())
throw json_parse("uint-via-string " + fmt_name(name, title) + " is of incorrect type");
unsigned int ret;
if (!parse_number(value.asString(), ret))
throw json_parse("uint-via-string " + fmt_name(name, title) + " failed to parse");
return ret;
}
template <typename NAME>
inline unsigned int get_uint_via_string(const Json::Value &root,
const NAME &name)
{
return get_uint_via_string(root, name, nullptr);
}
template <typename NAME, typename TITLE>
inline unsigned int get_uint_optional_via_string(const Json::Value &root,
const NAME &name,
const unsigned int default_value,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return default_value;
if (!value.isString())
throw json_parse("uint-via-string " + fmt_name(name, title) + " is of incorrect type");
unsigned int ret;
if (!parse_number(value.asString(), ret))
throw json_parse("uint-via-string " + fmt_name(name, title) + " failed to parse");
return ret;
}
template <typename NAME>
inline unsigned int get_uint_optional_via_string(const Json::Value &root,
const NAME &name,
const unsigned int default_value)
{
return get_uint_optional_via_string(root, name, default_value, nullptr);
}
template <typename NAME, typename TITLE>
inline std::uint64_t get_uint64(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("uint64 " + fmt_name(name, title) + " is missing");
if (!value.isUInt64())
throw json_parse("uint64 " + fmt_name(name, title) + " is of incorrect type");
return value.asUInt64();
}
template <typename NAME>
inline std::uint64_t get_uint64(const Json::Value &root, const NAME &name)
{
return get_uint64(root, name, nullptr);
}
template <typename NAME, typename TITLE>
inline std::uint64_t get_uint64_optional(const Json::Value &root,
const NAME &name,
const std::uint64_t default_value,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return default_value;
if (!value.isUInt64())
throw json_parse("uint64 " + fmt_name(name, title) + " is of incorrect type");
return value.asUInt64();
}
template <typename NAME, typename TITLE>
inline std::int64_t get_int64_optional(const Json::Value &root,
const NAME &name,
const std::uint64_t default_value,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return default_value;
if (!value.isInt64())
throw json_parse("int64 " + fmt_name(name, title) + " is of incorrect type");
return value.asInt64();
}
template <typename NAME>
inline std::uint64_t get_uint64_optional(const Json::Value &root,
const NAME &name,
const std::uint64_t default_value)
{
return get_uint64_optional(root, name, default_value, nullptr);
}
/*
* The get_integer_optional function are used to select the right
* method based on the default_value parameter
*/
template <typename NAME, typename TITLE>
std::uint64_t get_integer_optional(const Json::Value &root,
const NAME &name,
const std::uint64_t default_value,
const TITLE &title)
{
return get_uint64_optional(root, name, default_value, title);
}
template <typename NAME, typename TITLE>
std::int64_t get_integer_optional(const Json::Value &root,
const NAME &name,
const std::int64_t default_value,
const TITLE &title)
{
return get_int64_optional(root, name, default_value, title);
}
template <typename NAME, typename TITLE>
inline unsigned int get_integer_optional(const Json::Value &root,
const NAME &name,
const unsigned int default_value,
const TITLE &title)
{
return get_uint_optional(root, name, default_value, title);
}
template <typename NAME, typename TITLE>
inline int get_integer_optional(const Json::Value &root,
const NAME &name,
const int default_value,
const TITLE &title)
{
return get_int_optional(root, name, default_value, title);
}
template <typename NAME, typename TITLE>
inline std::uint64_t get_uint64_via_string(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("uint64-via-string " + fmt_name(name, title) + " is missing");
if (!value.isString())
throw json_parse("uint64-via-string " + fmt_name(name, title) + " is of incorrect type");
std::uint64_t ret;
if (!parse_number(value.asString(), ret))
throw json_parse("uint64-via-string " + fmt_name(name, title) + " failed to parse");
return ret;
}
template <typename NAME>
inline std::uint64_t get_uint64_via_string(const Json::Value &root,
const NAME &name)
{
return get_uint64_via_string(root, name, nullptr);
}
template <typename NAME, typename TITLE>
inline std::uint64_t get_uint64_optional_via_string(const Json::Value &root,
const NAME &name,
const std::uint64_t default_value,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
return default_value;
if (!value.isString())
throw json_parse("uint64-via-string " + fmt_name(name, title) + " is of incorrect type");
std::uint64_t ret;
if (!parse_number(value.asString(), ret))
throw json_parse("uint64-via-string " + fmt_name(name, title) + " failed to parse");
return ret;
}
template <typename NAME>
inline std::uint64_t get_uint64_optional_via_string(const Json::Value &root,
const NAME &name,
const std::uint64_t default_value)
{
return get_uint64_optional_via_string(root, name, default_value, nullptr);
}
template <typename NAME, typename TITLE>
inline bool get_bool(const Json::Value &root,
const NAME &name,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
throw json_parse("bool " + fmt_name(name, title) + " is missing");
if (!value.isBool())
throw json_parse("bool " + fmt_name(name, title) + " is of incorrect type");
return value.asBool();
}
template <typename NAME>
inline bool get_bool(const Json::Value &root, const NAME &name)
{
return get_bool(root, name, nullptr);
}
template <typename NAME>
inline bool get_bool_optional(const Json::Value &root,
const NAME &name,
const bool default_value = false)
{
const Json::Value &jv = root[name];
if (jv.isConvertibleTo(Json::booleanValue))
return jv.asBool();
else
return default_value;
}
template <typename NAME>
inline int get_bool_tristate(const Json::Value &root,
const NAME &name)
{
const Json::Value &jv = root[name];
if (jv.isConvertibleTo(Json::booleanValue))
return jv.asBool() ? 1 : 0;
else
return -1;
}
DISABLE_DANGLING_WARNINGS()
template <typename NAME, typename TITLE>
inline const Json::Value &get_dict(const Json::Value &root,
const NAME &name,
const bool optional,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
{
if (optional)
return value;
throw json_parse("dictionary " + fmt_name(name, title) + " is missing");
}
if (!value.isObject())
throw json_parse("dictionary " + fmt_name(name, title) + " is of incorrect type");
return value;
}
REENABLE_DANGLING_WARNINGS()
template <typename NAME>
inline const Json::Value &get_dict(const Json::Value &root,
const NAME &name,
const bool optional)
{
return get_dict(root, name, optional, nullptr);
}
template <typename NAME, typename TITLE>
inline Json::Value get_dict(Json::Value &&root,
const NAME &name,
const bool optional,
const TITLE &title)
{
Json::Value r = std::move(root);
return get_dict(r, name, optional, title);
}
template <typename NAME>
inline Json::Value get_dict(Json::Value &&root,
const NAME &name,
const bool optional)
{
return get_dict(std::move(root), name, optional, nullptr);
}
DISABLE_DANGLING_WARNINGS()
template <typename TITLE>
inline const Json::Value &cast_dict(const Json::Value &value,
const bool optional,
const TITLE &title)
{
if (value.isNull())
{
if (optional)
return value;
throw json_parse("dictionary cast " + fmt_name_cast(title) + " is null");
}
if (!value.isObject())
throw json_parse("dictionary cast " + fmt_name_cast(title) + " is of incorrect type");
return value;
}
REENABLE_DANGLING_WARNINGS()
inline const Json::Value &cast_dict(const Json::Value &value,
const bool optional)
{
return cast_dict(value, optional, nullptr);
}
template <typename TITLE>
inline Json::Value cast_dict(Json::Value &&value,
const bool optional,
const TITLE &title)
{
Json::Value ret = std::move(value);
cast_dict(ret, optional, title);
return ret;
}
inline Json::Value cast_dict(Json::Value &&value,
const bool optional)
{
return cast_dict(std::move(value), optional, nullptr);
}
DISABLE_DANGLING_WARNINGS()
template <typename NAME, typename TITLE>
inline const Json::Value &get_array(const Json::Value &root,
const NAME &name,
const bool optional,
const TITLE &title)
{
const Json::Value &value = root[name];
if (value.isNull())
{
if (optional)
return value;
throw json_parse("array " + fmt_name(name, title) + " is missing");
}
if (!value.isArray())
throw json_parse("array " + fmt_name(name, title) + " is of incorrect type");
return value;
}
REENABLE_DANGLING_WARNINGS()
template <typename NAME>
inline const Json::Value &get_array(const Json::Value &root,
const NAME &name,
const bool optional)
{
return get_array(root, name, optional, nullptr);
}
template <typename NAME, typename TITLE>
inline Json::Value get_array(Json::Value &&root,
const NAME &name,
const bool optional,
const TITLE &title)
{
Json::Value r = std::move(root);
return get_array(r, name, optional, title);
}
template <typename NAME>
inline Json::Value get_array(Json::Value &&root,
const NAME &name,
const bool optional)
{
return get_array(std::move(root), name, optional, nullptr);
}
DISABLE_DANGLING_WARNINGS()
template <typename TITLE>
inline const Json::Value &cast_array(const Json::Value &value,
const bool optional,
const TITLE &title)
{
if (value.isNull())
{
if (optional)
return value;
throw json_parse("array cast " + fmt_name_cast(title) + " is null");
}
if (!value.isArray())
throw json_parse("array cast " + fmt_name_cast(title) + " is of incorrect type");
return value;
}
REENABLE_DANGLING_WARNINGS()
inline const Json::Value &cast_array(const Json::Value &value,
const bool optional)
{
return cast_array(value, optional, nullptr);
}
template <typename TITLE>
inline Json::Value cast_array(Json::Value &&value,
const bool optional,
const TITLE &title)
{
Json::Value ret = std::move(value);
cast_array(ret, optional, title);
return ret;
}
inline Json::Value cast_array(Json::Value &&value,
const bool optional)
{
return cast_array(std::move(value), optional, nullptr);
}
template <typename NAME, typename TITLE>
inline void to_string(const Json::Value &root,
std::string &dest,
const NAME &name,
const TITLE &title)
{
dest = get_string(root, name, title);
}
template <typename NAME, typename TITLE>
inline void to_string_optional(const Json::Value &root,
std::string &dest,
const NAME &name,
const std::string &default_value,
const TITLE &title)
{
dest = get_string_optional(root, name, default_value, title);
}
template <typename NAME, typename TITLE>
inline void to_int(const Json::Value &root,
int &dest,
const NAME &name,
const TITLE &title)
{
dest = get_int(root, name, title);
}
template <typename NAME, typename TITLE>
inline void to_uchar(const Json::Value &root,
unsigned char &dest,
const NAME &name,
const TITLE &title)
{
auto temp = get_int(root, name, title);
dest = clamp_notify<unsigned char>(temp,
[](decltype(temp) temp) -> unsigned char
{
auto why = std::string("Conversion error [" + std::to_string(temp) + "] to unsigned char");
throw json_parse(std::move(why)); });
}
template <typename NAME, typename TITLE>
inline void to_uint(const Json::Value &root,
unsigned int &dest,
const NAME &name,
const TITLE &title)
{
dest = get_uint(root, name, title);
}
template <typename NAME, typename TITLE>
inline void to_uint_optional(const Json::Value &root,
unsigned int &dest,
const NAME &name,
const unsigned int default_value,
const TITLE &title)
{
dest = get_uint_optional(root, name, default_value, title);
}
template <typename NAME, typename TITLE>
inline void to_uint64(const Json::Value &root,
std::uint64_t &dest,
const NAME &name,
const TITLE &title)
{
dest = get_uint64(root, name, title);
}
template <typename NAME, typename TITLE>
inline void to_bool(const Json::Value &root,
bool &dest,
const NAME &name,
const TITLE &title)
{
dest = get_bool(root, name, title);
}
inline void format_compact(const Json::Value &root, Buffer &buf)
{
#ifdef OPENVPN_JSON
root.toCompactString(buf);
#else
Json::StreamWriterBuilder json_builder;
json_builder.settings_["indentation"] = "";
BufferStreamOut os(buf);
std::unique_ptr<Json::StreamWriter> sw(json_builder.newStreamWriter());
sw->write(root, &os);
#endif
}
inline std::string format_compact(const Json::Value &root,
const size_t size_hint = 256)
{
auto bp = BufferAllocated(size_hint, BufAllocFlags::GROW);
format_compact(root, bp);
return buf_to_string(bp);
}
inline void format(const Json::Value &root, Buffer &buf)
{
#ifdef OPENVPN_JSON
root.toStyledString(buf);
#else
Json::StreamWriterBuilder json_builder;
json_builder.settings_["indentation"] = " ";
BufferStreamOut os(buf);
std::unique_ptr<Json::StreamWriter> sw(json_builder.newStreamWriter());
sw->write(root, &os);
#endif
}
inline std::string format(const Json::Value &root)
{
return root.toStyledString();
}
inline std::string error(const Json::Value &root)
{
const Json::Value &je = root["error"];
if (je.isString())
return je.asString();
else
return std::string();
}
// Guarantee that json object jr is a dictionary.
// Do this by encapsulating jr in a dictionary
// { "result": jr } if it is not already one.
inline Json::Value dict_result(Json::Value jr)
{
if (jr.isObject())
return jr;
else
{
Json::Value jret(Json::objectValue);
jret["result"] = std::move(jr);
return jret;
}
}
} // namespace openvpn::json