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
|
// Copyright 2013 Daniel Parker
// Distributed under the Boost license, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See https://github.com/danielaparker/jsoncons for latest version
#ifndef JSONCONS_JSON_FILTER_HPP
#define JSONCONS_JSON_FILTER_HPP
#include <string>
#include <jsoncons/json_visitor.hpp>
namespace jsoncons {
template <class CharT>
class basic_json_filter : public basic_json_visitor<CharT>
{
public:
using typename basic_json_visitor<CharT>::char_type;
using typename basic_json_visitor<CharT>::string_view_type;
private:
basic_json_visitor<char_type>* destination_;
// noncopyable
basic_json_filter(const basic_json_filter&) = delete;
basic_json_filter& operator=(const basic_json_filter&) = delete;
public:
basic_json_filter(basic_json_visitor<char_type>& visitor)
: destination_(std::addressof(visitor))
{
}
// moveable
basic_json_filter(basic_json_filter&&) = default;
basic_json_filter& operator=(basic_json_filter&&) = default;
basic_json_visitor<char_type>& destination()
{
return *destination_;
}
#if !defined(JSONCONS_NO_DEPRECATED)
JSONCONS_DEPRECATED_MSG("Instead, use destination()")
basic_json_visitor<char_type>& to_handler()
{
return destination_;
}
JSONCONS_DEPRECATED_MSG("Instead, use destination")
basic_json_visitor<char_type>& input_handler()
{
return destination_;
}
JSONCONS_DEPRECATED_MSG("Instead, use destination")
basic_json_visitor<char_type>& downstream_handler()
{
return destination_;
}
JSONCONS_DEPRECATED_MSG("Instead, use destination")
basic_json_visitor<char_type>& destination_handler()
{
return destination_;
}
#endif
private:
void visit_flush() override
{
destination_->flush();
}
bool visit_begin_object(semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->begin_object(tag, context, ec);
}
bool visit_begin_object(std::size_t length, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->begin_object(length, tag, context, ec);
}
bool visit_end_object(const ser_context& context, std::error_code& ec) override
{
return destination_->end_object(context, ec);
}
bool visit_begin_array(semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->begin_array(tag, context, ec);
}
bool visit_begin_array(std::size_t length, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->begin_array(length, tag, context, ec);
}
bool visit_end_array(const ser_context& context, std::error_code& ec) override
{
return destination_->end_array(context, ec);
}
bool visit_key(const string_view_type& name,
const ser_context& context,
std::error_code& ec) override
{
return destination_->key(name, context, ec);
}
bool visit_string(const string_view_type& value,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->string_value(value, tag, context, ec);
}
bool visit_byte_string(const byte_string_view& b,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->byte_string_value(b, tag, context, ec);
}
bool visit_byte_string(const byte_string_view& b,
uint64_t ext_tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->byte_string_value(b, ext_tag, context, ec);
}
bool visit_uint64(uint64_t value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->uint64_value(value, tag, context, ec);
}
bool visit_int64(int64_t value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->int64_value(value, tag, context, ec);
}
bool visit_half(uint16_t value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->half_value(value, tag, context, ec);
}
bool visit_double(double value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->double_value(value, tag, context, ec);
}
bool visit_bool(bool value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->bool_value(value, tag, context, ec);
}
bool visit_null(semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->null_value(tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint8_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint16_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint32_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint64_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int8_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int16_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int32_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int64_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(half_arg_t,
const jsoncons::span<const uint16_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(half_arg, s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const float>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const double>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_begin_multi_dim(const jsoncons::span<const size_t>& shape,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->begin_multi_dim(shape, tag, context, ec);
}
bool visit_end_multi_dim(const ser_context& context,
std::error_code& ec) override
{
return destination_->end_multi_dim(context, ec);
}
};
template <class CharT>
class basic_rename_object_key_filter : public basic_json_filter<CharT>
{
public:
using typename basic_json_filter<CharT>::string_view_type;
private:
std::basic_string<CharT> name_;
std::basic_string<CharT> new_name_;
public:
basic_rename_object_key_filter(const std::basic_string<CharT>& name,
const std::basic_string<CharT>& new_name,
basic_json_visitor<CharT>& visitor)
: basic_json_filter<CharT>(visitor),
name_(name), new_name_(new_name)
{
}
private:
bool visit_key(const string_view_type& name,
const ser_context& context,
std::error_code& ec) override
{
if (name == name_)
{
return this->destination().key(new_name_,context, ec);
}
else
{
return this->destination().key(name,context,ec);
}
}
};
template <class From,class To>
class json_visitor_adaptor_base : public From
{
public:
using typename From::string_view_type;
private:
To* destination_;
// noncopyable
json_visitor_adaptor_base(const json_visitor_adaptor_base&) = delete;
json_visitor_adaptor_base& operator=(const json_visitor_adaptor_base&) = delete;
public:
json_visitor_adaptor_base(To& visitor)
: destination_(std::addressof(visitor))
{
}
// moveable
json_visitor_adaptor_base(json_visitor_adaptor_base&&) = default;
json_visitor_adaptor_base& operator=(json_visitor_adaptor_base&&) = default;
To& destination()
{
return *destination_;
}
private:
void visit_flush() override
{
destination_->flush();
}
bool visit_begin_object(semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->begin_object(tag, context, ec);
}
bool visit_begin_object(std::size_t length,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->begin_object(length, tag, context, ec);
}
bool visit_end_object(const ser_context& context, std::error_code& ec) override
{
return destination_->end_object(context, ec);
}
bool visit_begin_array(semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->begin_array(tag, context, ec);
}
bool visit_begin_array(std::size_t length,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->begin_array(length, tag, context, ec);
}
bool visit_end_array(const ser_context& context, std::error_code& ec) override
{
return destination_->end_array(context, ec);
}
bool visit_byte_string(const byte_string_view& b,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->byte_string_value(b, tag, context, ec);
}
bool visit_byte_string(const byte_string_view& b,
uint64_t ext_tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->byte_string_value(b, ext_tag, context, ec);
}
bool visit_half(uint16_t value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->half_value(value, tag, context, ec);
}
bool visit_double(double value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->double_value(value, tag, context, ec);
}
bool visit_int64(int64_t value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->int64_value(value, tag, context, ec);
}
bool visit_uint64(uint64_t value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->uint64_value(value, tag, context, ec);
}
bool visit_bool(bool value, semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->bool_value(value, tag, context, ec);
}
bool visit_null(semantic_tag tag, const ser_context& context, std::error_code& ec) override
{
return destination_->null_value(tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint8_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint16_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint32_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const uint64_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int8_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int16_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int32_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const int64_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(half_arg_t,
const jsoncons::span<const uint16_t>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(half_arg, s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const float>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_typed_array(const jsoncons::span<const double>& s,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->typed_array(s, tag, context, ec);
}
bool visit_begin_multi_dim(const jsoncons::span<const size_t>& shape,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination_->begin_multi_dim(shape, tag, context, ec);
}
bool visit_end_multi_dim(const ser_context& context,
std::error_code& ec) override
{
return destination_->end_multi_dim(context, ec);
}
};
template <class From,class To,class Enable=void>
class json_visitor_adaptor
{
};
template <class From,class To>
class json_visitor_adaptor<From,To,typename std::enable_if<type_traits::is_narrow_character<typename From::char_type>::value &&
type_traits::is_narrow_character<typename To::char_type>::value>::type> : public json_visitor_adaptor_base<From,To>
{
using supertype = json_visitor_adaptor_base<From,To>;
using to_char_type = typename To::char_type;
using from_char_type = typename From::char_type;
public:
using typename From::string_view_type;
using supertype::destination;
private:
// noncopyable
json_visitor_adaptor(const json_visitor_adaptor&) = delete;
json_visitor_adaptor& operator=(const json_visitor_adaptor&) = delete;
public:
json_visitor_adaptor(To& visitor)
: supertype(visitor)
{
}
// moveable
json_visitor_adaptor(json_visitor_adaptor&&) = default;
json_visitor_adaptor& operator=(json_visitor_adaptor&&) = default;
private:
bool visit_key(const string_view_type& key,
const ser_context& context,
std::error_code& ec) override
{
return destination().key(string_view_type(reinterpret_cast<const to_char_type*>(key.data()),key.size()), context, ec);
}
bool visit_string(const string_view_type& value,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
return destination().string_value(string_view_type(reinterpret_cast<const to_char_type*>(value.data()),value.size()), tag, context, ec);
}
};
template <class From,class To>
class json_visitor_adaptor<From,To,typename std::enable_if<!(type_traits::is_narrow_character<typename From::char_type>::value &&
type_traits::is_narrow_character<typename To::char_type>::value)>::type> : public json_visitor_adaptor_base<From,To>
{
using supertype = json_visitor_adaptor_base<From,To>;
public:
using typename From::string_view_type;
using supertype::destination;
private:
// noncopyable
json_visitor_adaptor(const json_visitor_adaptor&) = delete;
json_visitor_adaptor& operator=(const json_visitor_adaptor&) = delete;
public:
json_visitor_adaptor(To& visitor)
: supertype(visitor)
{
}
// moveable
json_visitor_adaptor(json_visitor_adaptor&&) = default;
json_visitor_adaptor& operator=(json_visitor_adaptor&&) = default;
private:
bool visit_key(const string_view_type& name,
const ser_context& context,
std::error_code& ec) override
{
std::basic_string<typename To::char_type> target;
auto result = unicode_traits::convert(name.data(), name.size(), target, unicode_traits::conv_flags::strict);
if (result.ec != unicode_traits::conv_errc())
{
ec = result.ec;
}
return destination().key(target, context, ec);
}
bool visit_string(const string_view_type& value,
semantic_tag tag,
const ser_context& context,
std::error_code& ec) override
{
std::basic_string<typename To::char_type> target;
auto result = unicode_traits::convert(value.data(), value.size(),
target,unicode_traits::conv_flags::strict);
if (result.ec != unicode_traits::conv_errc())
{
JSONCONS_THROW(ser_error(result.ec));
}
return destination().string_value(target, tag, context, ec);
}
};
template <class From,class To>
json_visitor_adaptor<From,To> make_json_visitor_adaptor(To& to)
{
return json_visitor_adaptor<From, To>(to);
}
using json_filter = basic_json_filter<char>;
using wjson_filter = basic_json_filter<wchar_t>;
using rename_object_key_filter = basic_rename_object_key_filter<char>;
using wrename_object_key_filter = basic_rename_object_key_filter<wchar_t>;
#if !defined(JSONCONS_NO_DEPRECATED)
template <class CharT>
using basic_json_content_filter = basic_json_filter<CharT>;
JSONCONS_DEPRECATED_MSG("Instead, use json_filter") typedef basic_json_filter<char> json_content_filter;
JSONCONS_DEPRECATED_MSG("Instead, use wjson_filter") typedef basic_json_filter<wchar_t> wjson_content_filter;
JSONCONS_DEPRECATED_MSG("Instead, use rename_object_key_filter") typedef basic_rename_object_key_filter<char> rename_name_filter;
JSONCONS_DEPRECATED_MSG("Instead, use wrename_object_key_filter") typedef basic_rename_object_key_filter<wchar_t> wrename_name_filter;
JSONCONS_DEPRECATED_MSG("Instead, use rename_object_key_filter") typedef basic_rename_object_key_filter<char> rename_object_member_filter;
JSONCONS_DEPRECATED_MSG("Instead, use wrename_object_key_filter") typedef basic_rename_object_key_filter<wchar_t> wrename_object_member_filter;
#endif
}
#endif
|