summaryrefslogtreecommitdiff
path: root/vendor/jsoncons-0.99.2/jsoncons/json_input_handler.hpp
blob: 566209e529c7860d851a2b451bcd3267c5961cea (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
// 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_INPUT_HANDLER_HPP
#define JSONCONS_JSON_INPUT_HANDLER_HPP

#include <string>
#include "jsoncons/jsoncons.hpp"

namespace jsoncons {

template<typename CharT>
uint64_t string_to_uinteger(const CharT *s, size_t length) throw(std::overflow_error)
{
    static const uint64_t max_value = std::numeric_limits<uint64_t>::max JSONCONS_NO_MACRO_EXP();
    static const uint64_t max_value_div_10 = max_value / 10;
    uint64_t n = 0;
    for (size_t i = 0; i < length; ++i)
    {
        uint64_t x = s[i] - '0';
        if (n > max_value_div_10)
        {
            throw std::overflow_error("Unsigned overflow");
        }
        n = n * 10;
        if (n > max_value - x)
        {
            throw std::overflow_error("Unsigned overflow");
        }

        n += x;
    }
    return n;
}

template<typename CharT>
int64_t string_to_integer(bool has_neg, const CharT *s, size_t length) throw(std::overflow_error)
{
    const long long max_value = std::numeric_limits<int64_t>::max JSONCONS_NO_MACRO_EXP();
    const long long max_value_div_10 = max_value / 10;

    long long n = 0;
    for (size_t i = 0; i < length; ++i)
    {
        long long x = s[i] - '0';
        if (n > max_value_div_10)
        {
            throw std::overflow_error("Integer overflow");
        }
        n = n * 10;
        if (n > max_value - x)
        {
            throw std::overflow_error("Integer overflow");
        }

        n += x;
    }
    return has_neg ? -n : n;
}

template <typename CharT>
class basic_parsing_context;

template <typename CharT>
class basic_json_input_handler
{
public:
    virtual ~basic_json_input_handler() {}

    void begin_json()
    {
        do_begin_json();
    }

    void end_json()
    {
        do_end_json();
    }

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

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

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

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

    void name(const std::basic_string<CharT>& name, const basic_parsing_context<CharT>& context)
    {
        do_name(name.data(), name.length(), context);
    }

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

    void value(const std::basic_string<CharT>& value, const basic_parsing_context<CharT>& context) 
    {
        do_string_value(value.data(), value.length(), context);
    }

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

    void value(const CharT* p, const basic_parsing_context<CharT>& context) 
    {
        do_string_value(p, std::char_traits<CharT>::length(p), context);
    }

    void value(int value, const basic_parsing_context<CharT>& context) 
    {
        do_integer_value(value,context);
    }

    void value(long value, const basic_parsing_context<CharT>& context) 
    {
        do_integer_value(value,context);
    }

    void value(long long value, const basic_parsing_context<CharT>& context) 
    {
        do_integer_value(value,context);
    }

    void value(unsigned int value, const basic_parsing_context<CharT>& context) 
    {
        do_uinteger_value(value,context);
    }

    void value(unsigned long value, const basic_parsing_context<CharT>& context) 
    {
        do_uinteger_value(value,context);
    }

    void value(unsigned long long value, const basic_parsing_context<CharT>& context) 
    {
        do_uinteger_value(value,context);
    }

    void value(float value, uint8_t precision, const basic_parsing_context<CharT>& context)
    {
        do_double_value(value, precision, context);
    }

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

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

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

private:
    virtual void do_begin_json() = 0;

    virtual void do_end_json() = 0;

    virtual void do_begin_object(const basic_parsing_context<CharT>& context) = 0;

    virtual void do_end_object(const basic_parsing_context<CharT>& context) = 0;

    virtual void do_begin_array(const basic_parsing_context<CharT>& context) = 0;

    virtual void do_end_array(const basic_parsing_context<CharT>& context) = 0;

    virtual void do_name(const CharT* name, size_t length, const basic_parsing_context<CharT>& context) = 0;

    virtual void do_null_value(const basic_parsing_context<CharT>& context) = 0;

    virtual void do_string_value(const CharT* value, size_t length, const basic_parsing_context<CharT>& context) = 0;

    virtual void do_double_value(double value, uint8_t precision, const basic_parsing_context<CharT>& context) = 0;

    virtual void do_integer_value(int64_t value, const basic_parsing_context<CharT>& context) = 0;

    virtual void do_uinteger_value(uint64_t value, const basic_parsing_context<CharT>& context) = 0;

    virtual void do_bool_value(bool value, const basic_parsing_context<CharT>& context) = 0;
};


template <typename CharT>
class basic_empty_json_input_handler : public basic_json_input_handler<CharT>
{
public:
    static basic_json_input_handler<CharT>& instance()
    {
        static basic_empty_json_input_handler<CharT> instance;
        return instance;
    }
private:
    void do_begin_json() override
    {
    }

    void do_end_json() override
    {
    }

    void do_begin_object(const basic_parsing_context<CharT>&) override
    {
    }

    void do_end_object(const basic_parsing_context<CharT>&) override
    {
    }

    void do_begin_array(const basic_parsing_context<CharT>&) override
    {
    }

    void do_end_array(const basic_parsing_context<CharT>&) override
    {
    }

    void do_name(const CharT* p, size_t length, const basic_parsing_context<CharT>&) override
    {
        (void)p;
        (void)length;
    }

    void do_null_value(const basic_parsing_context<CharT>&) override
    {
    }

    void do_string_value(const CharT* p, size_t length, const basic_parsing_context<CharT>&) override
    {
        (void)p;
        (void)length;
    }

    void do_double_value(double, uint8_t, const basic_parsing_context<CharT>&) override
    {
    }

    void do_integer_value(int64_t, const basic_parsing_context<CharT>&) override
    {
    }

    void do_uinteger_value(uint64_t, const basic_parsing_context<CharT>&) override
    {
    }

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

typedef basic_json_input_handler<char> json_input_handler;
typedef basic_json_input_handler<wchar_t> wjson_input_handler;

typedef basic_empty_json_input_handler<char> empty_json_input_handler;
typedef basic_empty_json_input_handler<wchar_t> wempty_json_input_handler;

}

#endif