summaryrefslogtreecommitdiff
path: root/vendor/jsoncons-0.99.2/jsoncons/output_format.hpp
blob: 54e74874e00b35ee0ce0444b81e31fdbffaeac93 (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
325
326
327
328
329
330
// 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_OUTPUT_FORMAT_HPP
#define JSONCONS_OUTPUT_FORMAT_HPP

#include <string>
#include <sstream>
#include <vector>
#include <istream>
#include <ostream>
#include <cstdlib>
#include <limits>
#include <cwchar>

namespace jsoncons {

enum class block_options {next_line,same_line};

template <typename CharT>
class buffered_ostream;

template <typename CharT>
class basic_output_format
{
    int indent_;
    uint8_t precision_;
    bool replace_nan_;
    bool replace_pos_inf_;
    bool replace_neg_inf_;
    std::basic_string<CharT> nan_replacement_;
    std::basic_string<CharT> pos_inf_replacement_;
    std::basic_string<CharT> neg_inf_replacement_;
    bool escape_all_non_ascii_;
    bool escape_solidus_;
    block_options object_array_block_option_;
    block_options array_array_block_option_;
    block_options object_object_block_option_;
    block_options array_object_block_option_;
public:
    static const size_t default_indent = 4;

//  Constructors

    basic_output_format()
        :
        indent_(default_indent),
        precision_(16),
        replace_nan_(true),
        replace_pos_inf_(true),
        replace_neg_inf_(true),
        nan_replacement_(json_literals<CharT>::null_literal().first),
        pos_inf_replacement_(json_literals<CharT>::null_literal().first),
        neg_inf_replacement_(json_literals<CharT>::null_literal().first),
        escape_all_non_ascii_(false),
        escape_solidus_(false),
        object_array_block_option_(block_options::same_line),
        array_array_block_option_(block_options::next_line),
        object_object_block_option_(block_options::same_line),
        array_object_block_option_(block_options::next_line)
    {
    }

//  Accessors

    block_options object_array_block_option()
    {
        return object_array_block_option_;
    }

    basic_output_format<CharT>& object_array_block_option(block_options value)
    {
        object_array_block_option_ = value;
        return *this;
    }

    block_options object_object_block_option()
    {
        return object_object_block_option_;
    }

    basic_output_format<CharT>& object_object_block_option(block_options value)
    {
        object_object_block_option_ = value;
        return *this;
    }

    block_options array_array_block_option()
    {
        return array_array_block_option_;
    }

    basic_output_format<CharT>& array_array_block_option(block_options value)
    {
        array_array_block_option_ = value;
        return *this;
    }

    block_options array_object_block_option()
    {
        return array_object_block_option_;
    }

    basic_output_format<CharT>& array_object_block_option(block_options value)
    {
        array_object_block_option_ = value;
        return *this;
    }

    int indent() const
    {
        return indent_;
    }

    uint8_t precision() const
    {
        return precision_;
    }

    bool escape_all_non_ascii() const
    {
        return escape_all_non_ascii_;
    }

    bool escape_solidus() const
    {
        return escape_solidus_;
    }

    bool replace_nan() const {return replace_nan_;}

    bool replace_pos_inf() const {return replace_pos_inf_;}

    bool replace_neg_inf() const {return replace_neg_inf_;}

    std::basic_string<CharT> nan_replacement() const
    {
        return nan_replacement_;
    }

    std::basic_string<CharT> pos_inf_replacement() const
    {
        return pos_inf_replacement_;
    }

    std::basic_string<CharT> neg_inf_replacement() const
    {
        return neg_inf_replacement_;
    }

//  Modifiers

    basic_output_format<CharT>& precision(uint8_t prec)
    {
        precision_ = prec;
        return *this;
    }

    basic_output_format<CharT>& escape_all_non_ascii(bool value)
    {
        escape_all_non_ascii_ = value;
        return *this;
    }

    basic_output_format<CharT>& escape_solidus(bool value)
    {
        escape_solidus_ = value;
        return *this;
    }

    basic_output_format<CharT>& replace_nan(bool replace)
    {
        replace_nan_ = replace;
        return *this;
    }

    basic_output_format<CharT>& replace_inf(bool replace)
    {
        replace_pos_inf_ = replace;
        replace_neg_inf_ = replace;
        return *this;
    }

    basic_output_format<CharT>& replace_pos_inf(bool replace)
    {
        replace_pos_inf_ = replace;
        return *this;
    }

    basic_output_format<CharT>& replace_neg_inf(bool replace)
    {
        replace_neg_inf_ = replace;
        return *this;
    }

    basic_output_format<CharT>& nan_replacement(const std::basic_string<CharT>& replacement)
    {
        nan_replacement_ = replacement;
        return *this;
    }

    basic_output_format<CharT>& pos_inf_replacement(const std::basic_string<CharT>& replacement)
    {
        pos_inf_replacement_ = replacement;
        return *this;
    }

    basic_output_format<CharT>& neg_inf_replacement(const std::basic_string<CharT>& replacement)
    {
        neg_inf_replacement_ = replacement;
        return *this;
    }

    basic_output_format<CharT>& indent(int value)
    {
        indent_ = value;
        return *this;
    }
};

template<typename CharT>
void escape_string(const CharT* s,
                   size_t length,
                   const basic_output_format<CharT>& format,
                   buffered_ostream<CharT>& os)
{
    const CharT* begin = s;
    const CharT* end = s + length;
    for (const CharT* it = begin; it != end; ++it)
    {
        CharT c = *it;
        switch (c)
        {
        case '\\':
            os.put('\\'); 
            os.put('\\');
            break;
        case '"':
            os.put('\\'); 
            os.put('\"');
            break;
        case '\b':
            os.put('\\'); 
            os.put('b');
            break;
        case '\f':
            os.put('\\');
            os.put('f');
            break;
        case '\n':
            os.put('\\');
            os.put('n');
            break;
        case '\r':
            os.put('\\');
            os.put('r');
            break;
        case '\t':
            os.put('\\');
            os.put('t');
            break;
        default:
            uint32_t u(c >= 0 ? c : 256 + c);
            if (format.escape_solidus() && c == '/')
            {
                os.put('\\');
                os.put('/');
            }
            else if (is_control_character(u) || format.escape_all_non_ascii())
            {
                // convert utf8 to codepoint
                uint32_t cp = json_char_traits<CharT, sizeof(CharT)>::convert_char_to_codepoint(it, end);
                if (is_non_ascii_character(cp) || is_control_character(u))
                {
                    if (cp > 0xFFFF)
                    {
                        cp -= 0x10000;
                        uint32_t first = (cp >> 10) + 0xD800;
                        uint32_t second = ((cp & 0x03FF) + 0xDC00);

                        os.put('\\');
                        os.put('u');
                        os.put(to_hex_character(first >> 12 & 0x000F));
                        os.put(to_hex_character(first >> 8  & 0x000F));
                        os.put(to_hex_character(first >> 4  & 0x000F));
                        os.put(to_hex_character(first     & 0x000F));
                        os.put('\\');
                        os.put('u');
                        os.put(to_hex_character(second >> 12 & 0x000F));
                        os.put(to_hex_character(second >> 8  & 0x000F));
                        os.put(to_hex_character(second >> 4  & 0x000F));
                        os.put(to_hex_character(second     & 0x000F));
                    }
                    else
                    {
                        os.put('\\');
                        os.put('u');
                        os.put(to_hex_character(cp >> 12 & 0x000F));
                        os.put(to_hex_character(cp >> 8  & 0x000F));
                        os.put(to_hex_character(cp >> 4  & 0x000F));
                        os.put(to_hex_character(cp     & 0x000F));
                    }
                }
                else
                {
                    os.put(c);
                }
            }
            else if (format.escape_solidus() && c == '/')
            {
                os.put('\\');
                os.put('/');
            }
            else
            {
                os.put(c);
            }
            break;
        }
    }
}

typedef basic_output_format<char> output_format;
typedef basic_output_format<wchar_t> woutput_format;

}
#endif