summaryrefslogtreecommitdiff
path: root/vendor/jsoncons-0.99.2/jsoncons/json_filter.hpp
blob: 2019c01d84b70d63eec90c9a04a948dc1eb3a635 (plain)
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
// 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_input_handler.hpp"
#include "jsoncons/json_output_handler.hpp"
#include "jsoncons/parse_error_handler.hpp"

namespace jsoncons {

template <typename CharT>
class basic_json_input_output_adapter : public basic_json_input_handler<CharT>
{
public:
    basic_json_input_output_adapter()
        : writer_(std::addressof(null_json_output_handler<CharT>()))
    {
    }

    basic_json_input_output_adapter(basic_json_output_handler<CharT>& handler)
        : writer_(std::addressof(handler))
    {
    }

private:

    void do_begin_json() override
    {
        writer_->begin_json();
    }

    void do_end_json() override
    {
        writer_->end_json();
    }

    void do_begin_object(const basic_parsing_context<CharT>& context) override
    {
        writer_->begin_object();
    }

    void do_end_object(const basic_parsing_context<CharT>& context) override
    {
        writer_->end_object();
    }

    void do_begin_array(const basic_parsing_context<CharT>& context) override
    {
        writer_->begin_array();
    }

    void do_end_array(const basic_parsing_context<CharT>& context) override
    {
        writer_->end_array();
    }

    void do_name(const CharT* name, size_t length, 
                 const basic_parsing_context<CharT>& context) override
    {
        writer_->name(name, length);
    }

    void do_string_value(const CharT* value, size_t length, 
                         const basic_parsing_context<CharT>& context) override
    {
        writer_->value(value, length);
    }

    void do_integer_value(int64_t value, const basic_parsing_context<CharT>& context) override
    {
        writer_->value(value);
    }

    void do_uinteger_value(uint64_t value, 
                                 const basic_parsing_context<CharT>& context) override
    {
        writer_->value(value);
    }

    void do_double_value(double value, uint8_t precision, const basic_parsing_context<CharT>& context) override
    {
        writer_->value(value, precision);
    }

    void do_bool_value(bool value, const basic_parsing_context<CharT>& context) override
    {
        writer_->value(value);
    }

    void do_null_value(const basic_parsing_context<CharT>& context) override
    {
        writer_->value(null_type());
    }

    basic_json_output_handler<CharT>* writer_;
};

template <typename CharT>
class basic_json_filter : public basic_json_input_handler<CharT>
{
public:
    basic_json_filter(basic_json_input_handler<CharT>& handler)
        : handler_(std::addressof(handler)),
          err_handler_(std::addressof(basic_default_parse_error_handler<CharT>::instance()))
    {
    }

    basic_json_filter(basic_json_input_handler<CharT>& handler,
                      basic_parse_error_handler<CharT>& err_handler)
        : handler_(std::addressof(handler)),
          err_handler_(std::addressof(err_handler))
    {
    }

    basic_json_filter(basic_json_output_handler<CharT>& output_handler)
        : input_output_adapter_(output_handler), handler_(std::addressof(input_output_adapter_)),
          err_handler_(std::addressof(basic_default_parse_error_handler<CharT>::instance()))
    {
    }

    basic_json_filter(basic_json_output_handler<CharT>& output_handler,
                      basic_parse_error_handler<CharT>& err_handler)
        : input_output_adapter_(output_handler), handler_(std::addressof(input_output_adapter_)),
          err_handler_(std::addressof(err_handler))
    {
    }

    basic_json_input_handler<CharT>& input_handler()
    {
        return *handler_;
    }

#if !defined(JSONCONS_NO_DEPRECATED)
    basic_json_input_handler<CharT>& parent()
    {
        return *handler_;
    }
#endif

private:
    void do_begin_json() override
    {
        handler_->begin_json();
    }

    void do_end_json() override
    {
        handler_->end_json();
    }

    void do_begin_object(const basic_parsing_context<CharT>& context) override
    {
        handler_->begin_object(context);
    }

    void do_end_object(const basic_parsing_context<CharT>& context) override
    {
        handler_->end_object(context);
    }

    void do_begin_array(const basic_parsing_context<CharT>& context) override
    {
        handler_->begin_array(context);
    }

    void do_end_array(const basic_parsing_context<CharT>& context) override
    {
        handler_->end_array(context);
    }

    void do_name(const CharT* name, size_t length, const basic_parsing_context<CharT>& context) override
    {
        handler_->name(name, length, context);
    }

    void do_string_value(const CharT* value, size_t length, const basic_parsing_context<CharT>& context) override
    {
        handler_->value(value,length,context);
    }

    void do_double_value(double value, uint8_t precision, const basic_parsing_context<CharT>& context) override
    {
        handler_->value(value,precision,context);
    }

    void do_integer_value(int64_t value, const basic_parsing_context<CharT>& context) override
    {
        handler_->value(value,context);
    }

    void do_uinteger_value(uint64_t value, const basic_parsing_context<CharT>& context) override
    {
        handler_->value(value,context);
    }

    void do_bool_value(bool value, const basic_parsing_context<CharT>& context) override
    {
        handler_->value(value,context);
    }

    void do_null_value(const basic_parsing_context<CharT>& context) override
    {
        handler_->value(null_type(),context);
    }

    basic_json_input_output_adapter<CharT> input_output_adapter_;
    basic_json_input_handler<CharT>* handler_;
    basic_parse_error_handler<CharT>* err_handler_;
};

// Filters out begin_json and end_json events
template <typename CharT>
class basic_begin_end_json_filter : public basic_json_filter<CharT>
{
public:
    basic_begin_end_json_filter(basic_json_input_handler<CharT>& handler)
        : basic_json_filter<CharT>(handler)
    {
    }
private:
    void do_begin_json() override
    {
    }

    void do_end_json() override
    {
    }
};

template <typename CharT>
class basic_json_output_input_adapter : public basic_json_output_handler<CharT>
{
public:
    basic_json_output_input_adapter(basic_json_input_handler<CharT>& input_handler,
                                    const basic_parsing_context<CharT>& context)
        : input_handler_(std::addressof(input_handler)),
          context_(std::addressof(context))
    {
    }

private:

    void do_begin_json() override
    {
        input_handler_->begin_json();
    }

    void do_end_json() override
    {
        input_handler_->end_json();
    }

    void do_begin_object() override
    {
        input_handler_->begin_object(*context_);
    }

    void do_end_object() override
    {
        input_handler_->end_object(*context_);
    }

    void do_begin_array() override
    {
        input_handler_->begin_array(*context_);
    }

    void do_end_array() override
    {
        input_handler_->end_array(*context_);
    }

    void do_name(const CharT* name, size_t length) override
    {
        input_handler_->name(name, length, *context_);
    }

    void do_string_value(const CharT* value, size_t length) override
    {
        input_handler_->value(value, length, *context_);
    }

    void do_integer_value(int64_t value) override
    {
        input_handler_->value(value, *context_);
    }

    void do_uinteger_value(uint64_t value) override
    {
        input_handler_->value(value, *context_);
    }

    void do_double_value(double value, uint8_t precision) override
    {
        input_handler_->value(value, precision, *context_);
    }

    void do_bool_value(bool value) override
    {
        input_handler_->value(value, *context_);
    }

    void do_null_value() override
    {
        input_handler_->value(null_type(), *context_);
    }

    basic_json_input_handler<CharT>* input_handler_;
    const basic_parsing_context<CharT>* context_;
};

typedef basic_json_filter<char> json_filter;
typedef basic_json_filter<wchar_t> wjson_filter;

}

#endif