/src/jsoncons/include/jsoncons/json_options.hpp
Line  | Count  | Source  | 
1  |  | // Copyright 2013-2025 Daniel Parker  | 
2  |  | // Distributed under the Boost license, Version 1.0.  | 
3  |  | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)  | 
4  |  |  | 
5  |  | // See https://github.com/danielaparker/jsoncons for latest version  | 
6  |  |  | 
7  |  | #ifndef JSONCONS_JSON_OPTIONS_HPP  | 
8  |  | #define JSONCONS_JSON_OPTIONS_HPP  | 
9  |  |  | 
10  |  | #include <cstdint>  | 
11  |  | #include <cwchar>  | 
12  |  | #include <functional>  | 
13  |  | #include <string>  | 
14  |  | #include <system_error>  | 
15  |  |  | 
16  |  | #include <jsoncons/config/compiler_support.hpp>  | 
17  |  | #include <jsoncons/json_error.hpp>  | 
18  |  | #include <jsoncons/ser_util.hpp>  | 
19  |  |  | 
20  |  | namespace jsoncons { | 
21  |  |  | 
22  |  | enum class float_chars_format : uint8_t {general,fixed,scientific,hex}; | 
23  |  |  | 
24  |  | enum class indenting : uint8_t {no_indent = 0, indent = 1}; | 
25  |  |  | 
26  |  | enum class line_split_kind  : uint8_t {same_line=1, new_line, multi_line}; | 
27  |  |  | 
28  |  | enum class bignum_format_kind : uint8_t {raw,  | 
29  |  | #if !defined(JSONCONS_NO_DEPRECATED)  | 
30  |  |     number=raw, // deprecated, use raw instead   | 
31  |  | #endif      | 
32  |  |     base10,   | 
33  |  |     base64,   | 
34  |  |     base64url};  | 
35  |  |  | 
36  |  | #if !defined(JSONCONS_NO_DEPRECATED)  | 
37  |  | JSONCONS_DEPRECATED_MSG("Instead, use bignum_format_kind") typedef bignum_format_kind bigint_chars_format; | 
38  |  | #endif  | 
39  |  |  | 
40  |  | enum class byte_string_chars_format : uint8_t {none=0,base16,base64,base64url}; | 
41  |  |  | 
42  |  | enum class spaces_option : uint8_t {no_spaces=0,space_after,space_before,space_before_and_after}; | 
43  |  |  | 
44  |  |  | 
45  |  | struct default_json_parsing  | 
46  |  | { | 
47  |  |     bool operator()(json_errc ec, const ser_context&) noexcept   | 
48  | 0  |     { | 
49  | 0  |         return ec == json_errc::illegal_comment;  | 
50  | 0  |     }  | 
51  |  | };  | 
52  |  |  | 
53  |  | struct strict_json_parsing  | 
54  |  | { | 
55  |  |     bool operator()(json_errc, const ser_context&) noexcept  | 
56  | 0  |     { | 
57  | 0  |         return false;  | 
58  | 0  |     }  | 
59  |  | };  | 
60  |  |  | 
61  |  | struct allow_trailing_commas  | 
62  |  | { | 
63  |  |     bool operator()(const std::error_code& ec, const ser_context&) noexcept   | 
64  | 0  |     { | 
65  | 0  |         return ec == json_errc::illegal_comment || ec == jsoncons::json_errc::extra_comma;  | 
66  | 0  |     }  | 
67  |  | };  | 
68  |  |  | 
69  |  | template <typename CharT>  | 
70  |  | class basic_json_options;  | 
71  |  |  | 
72  |  | template <typename CharT>  | 
73  |  | class basic_json_options_common  | 
74  |  | { | 
75  |  |     friend class basic_json_options<CharT>;  | 
76  |  | public:  | 
77  |  |     using char_type = CharT;  | 
78  |  |     using string_type = std::basic_string<CharT>;  | 
79  |  | private:  | 
80  |  |  | 
81  |  |     bool enable_nan_to_num_:1;  | 
82  |  |     bool enable_inf_to_num_:1;  | 
83  |  |     bool enable_neginf_to_num_:1;  | 
84  |  |     bool enable_nan_to_str_:1;  | 
85  |  |     bool enable_inf_to_str_:1;  | 
86  |  |     bool enable_neginf_to_str_:1;  | 
87  |  |     bool enable_str_to_nan_:1;  | 
88  |  |     bool enable_str_to_inf_:1;  | 
89  |  |     bool enable_str_to_neginf_:1;  | 
90  |  |  | 
91  |  |     string_type nan_to_num_;  | 
92  |  |     string_type inf_to_num_;  | 
93  |  |     string_type neginf_to_num_;  | 
94  |  |     string_type nan_to_str_;  | 
95  |  |     string_type inf_to_str_;  | 
96  |  |     string_type neginf_to_str_;  | 
97  |  |     int max_nesting_depth_;  | 
98  |  |  | 
99  |  | protected:  | 
100  |  |     basic_json_options_common()  | 
101  |  |        :  | 
102  |  |         enable_nan_to_num_(false),  | 
103  |  |         enable_inf_to_num_(false),  | 
104  |  |         enable_neginf_to_num_(false),  | 
105  |  |         enable_nan_to_str_(false),  | 
106  |  |         enable_inf_to_str_(false),  | 
107  |  |         enable_neginf_to_str_(false),  | 
108  |  |         enable_str_to_nan_(false),  | 
109  |  |         enable_str_to_inf_(false),  | 
110  |  |         enable_str_to_neginf_(false),  | 
111  |  |         max_nesting_depth_(1024)  | 
112  |  |     {} | 
113  |  |  | 
114  |  |     virtual ~basic_json_options_common() = default;  | 
115  |  |  | 
116  |  |     basic_json_options_common(const basic_json_options_common&) = default;  | 
117  |  |     basic_json_options_common& operator=(const basic_json_options_common&) = default;  | 
118  |  |     basic_json_options_common(basic_json_options_common&&) = default;  | 
119  |  |     //basic_json_options_common& operator=(basic_json_options_common&&) = default;  | 
120  |  |  | 
121  |  | public:  | 
122  |  |  | 
123  |  |     bool enable_nan_to_num() const  | 
124  |  |     { | 
125  |  |         return enable_nan_to_num_;  | 
126  |  |     }  | 
127  |  |  | 
128  |  |     bool enable_inf_to_num() const  | 
129  |  |     { | 
130  |  |         return enable_inf_to_num_;  | 
131  |  |     }  | 
132  |  |  | 
133  |  |     bool enable_neginf_to_num() const  | 
134  |  |     { | 
135  |  |         return enable_neginf_to_num_ || enable_inf_to_num_;  | 
136  |  |     }  | 
137  |  |  | 
138  |  |     bool enable_nan_to_str() const  | 
139  |  |     { | 
140  |  |         return enable_nan_to_str_;  | 
141  |  |     }  | 
142  |  |  | 
143  |  |     bool enable_str_to_nan() const  | 
144  | 0  |     { | 
145  | 0  |         return enable_str_to_nan_;  | 
146  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_options_common<char>::enable_str_to_nan() const Unexecuted instantiation: jsoncons::basic_json_options_common<wchar_t>::enable_str_to_nan() const  | 
147  |  |  | 
148  |  |     bool enable_inf_to_str() const  | 
149  |  |     { | 
150  |  |         return enable_inf_to_str_;  | 
151  |  |     }  | 
152  |  |  | 
153  |  |     bool enable_str_to_inf() const  | 
154  | 0  |     { | 
155  | 0  |         return enable_str_to_inf_;  | 
156  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_options_common<char>::enable_str_to_inf() const Unexecuted instantiation: jsoncons::basic_json_options_common<wchar_t>::enable_str_to_inf() const  | 
157  |  |  | 
158  |  |     bool enable_neginf_to_str() const  | 
159  |  |     { | 
160  |  |         return enable_neginf_to_str_ || enable_inf_to_str_;  | 
161  |  |     }  | 
162  |  |  | 
163  |  |     bool enable_str_to_neginf() const  | 
164  | 0  |     { | 
165  | 0  |         return enable_str_to_neginf_ || enable_str_to_inf_;  | 
166  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_options_common<char>::enable_str_to_neginf() const Unexecuted instantiation: jsoncons::basic_json_options_common<wchar_t>::enable_str_to_neginf() const  | 
167  |  |  | 
168  |  |     string_type nan_to_num() const  | 
169  |  |     { | 
170  |  |         if (enable_nan_to_num_)  | 
171  |  |         { | 
172  |  |             return nan_to_num_;  | 
173  |  |         }  | 
174  |  |         else  | 
175  |  |         { | 
176  |  |             return nan_to_num_; // empty string  | 
177  |  |         }  | 
178  |  |     }  | 
179  |  |  | 
180  |  |     string_type inf_to_num() const  | 
181  |  |     { | 
182  |  |         if (enable_inf_to_num_)  | 
183  |  |         { | 
184  |  |             return inf_to_num_;  | 
185  |  |         }  | 
186  |  |         else  | 
187  |  |         { | 
188  |  |             return inf_to_num_; // empty string  | 
189  |  |         }  | 
190  |  |     }  | 
191  |  |  | 
192  |  |     string_type neginf_to_num() const  | 
193  |  |     { | 
194  |  |         if (enable_neginf_to_num_)  | 
195  |  |         { | 
196  |  |             return neginf_to_num_;  | 
197  |  |         }  | 
198  |  |         else if (enable_inf_to_num_)  | 
199  |  |         { | 
200  |  |             string_type s;  | 
201  |  |             s.push_back('-'); | 
202  |  |             s.append(inf_to_num_);  | 
203  |  |             return s;  | 
204  |  |         }  | 
205  |  |         else  | 
206  |  |         { | 
207  |  |             return neginf_to_num_; // empty string  | 
208  |  |         }  | 
209  |  |     }  | 
210  |  |  | 
211  |  |     string_type nan_to_str() const  | 
212  | 0  |     { | 
213  | 0  |         return nan_to_str_;  | 
214  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_options_common<char>::nan_to_str() const Unexecuted instantiation: jsoncons::basic_json_options_common<wchar_t>::nan_to_str() const  | 
215  |  |  | 
216  |  |     string_type inf_to_str() const  | 
217  | 0  |     { | 
218  | 0  |         return inf_to_str_;  | 
219  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_options_common<char>::inf_to_str() const Unexecuted instantiation: jsoncons::basic_json_options_common<wchar_t>::inf_to_str() const  | 
220  |  |  | 
221  |  |     string_type neginf_to_str() const  | 
222  | 0  |     { | 
223  | 0  |         if (enable_neginf_to_str_)  | 
224  | 0  |         { | 
225  | 0  |             return neginf_to_str_;  | 
226  | 0  |         }  | 
227  | 0  |         else if (enable_inf_to_str_)  | 
228  | 0  |         { | 
229  | 0  |             string_type s;  | 
230  | 0  |             s.push_back('-'); | 
231  | 0  |             s.append(inf_to_str_);  | 
232  | 0  |             return s;  | 
233  | 0  |         }  | 
234  | 0  |         else  | 
235  | 0  |         { | 
236  | 0  |             return neginf_to_str_; // empty string  | 
237  | 0  |         }  | 
238  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_options_common<char>::neginf_to_str() const Unexecuted instantiation: jsoncons::basic_json_options_common<wchar_t>::neginf_to_str() const  | 
239  |  |  | 
240  |  |     int max_nesting_depth() const   | 
241  | 0  |     { | 
242  | 0  |         return max_nesting_depth_;  | 
243  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_options_common<char>::max_nesting_depth() const Unexecuted instantiation: jsoncons::basic_json_options_common<wchar_t>::max_nesting_depth() const  | 
244  |  | };  | 
245  |  |  | 
246  |  | template <typename CharT>  | 
247  |  | class basic_json_decode_options : public virtual basic_json_options_common<CharT>  | 
248  |  | { | 
249  |  |     friend class basic_json_options<CharT>;  | 
250  |  |     using super_type = basic_json_options_common<CharT>;  | 
251  |  | public:  | 
252  |  |     using typename super_type::char_type;  | 
253  |  |     using typename super_type::string_type;  | 
254  |  | private:  | 
255  |  |     bool lossless_number_{false}; | 
256  |  |     bool lossless_bignum_{true}; | 
257  |  |     bool allow_comments_{true}; | 
258  |  |     bool allow_trailing_comma_{false}; | 
259  |  |     std::function<bool(json_errc,const ser_context&)> err_handler_;  | 
260  |  | public:  | 
261  |  |     basic_json_decode_options()  | 
262  |  |         : err_handler_(default_json_parsing())  | 
263  |  |     { | 
264  |  |     }  | 
265  |  |  | 
266  |  |     basic_json_decode_options(const basic_json_decode_options&) = default;  | 
267  |  |  | 
268  |  |     basic_json_decode_options(basic_json_decode_options&& other) noexcept  | 
269  |  |         : super_type(std::move(other)),   | 
270  |  |           lossless_number_(other.lossless_number_),   | 
271  |  |           lossless_bignum_(other.lossless_bignum_),   | 
272  |  |           allow_comments_(other.allow_comments_),   | 
273  |  |           allow_trailing_comma_(other.allow_trailing_comma_),   | 
274  |  |           err_handler_(std::move(other.err_handler_))  | 
275  |  |     { | 
276  |  |     }  | 
277  |  | protected:  | 
278  |  |     basic_json_decode_options& operator=(const basic_json_decode_options&) = default;  | 
279  |  |     basic_json_decode_options& operator=(basic_json_decode_options&&) = default;  | 
280  |  | public:  | 
281  |  |     bool lossless_number() const   | 
282  | 0  |     { | 
283  | 0  |         return lossless_number_;  | 
284  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_decode_options<char>::lossless_number() const Unexecuted instantiation: jsoncons::basic_json_decode_options<wchar_t>::lossless_number() const  | 
285  |  |     bool lossless_bignum() const   | 
286  | 0  |     { | 
287  | 0  |         return lossless_bignum_;  | 
288  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_decode_options<char>::lossless_bignum() const Unexecuted instantiation: jsoncons::basic_json_decode_options<wchar_t>::lossless_bignum() const  | 
289  |  |  | 
290  |  |     bool allow_comments() const   | 
291  | 0  |     { | 
292  | 0  |         return allow_comments_;  | 
293  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_decode_options<char>::allow_comments() const Unexecuted instantiation: jsoncons::basic_json_decode_options<wchar_t>::allow_comments() const  | 
294  |  |  | 
295  |  |     bool allow_trailing_comma() const   | 
296  | 0  |     { | 
297  | 0  |         return allow_trailing_comma_;  | 
298  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_decode_options<char>::allow_trailing_comma() const Unexecuted instantiation: jsoncons::basic_json_decode_options<wchar_t>::allow_trailing_comma() const  | 
299  |  |  | 
300  |  |     const std::function<bool(json_errc,const ser_context&)>& err_handler() const   | 
301  | 0  |     { | 
302  | 0  |         return err_handler_;  | 
303  | 0  |     } Unexecuted instantiation: jsoncons::basic_json_decode_options<char>::err_handler() const Unexecuted instantiation: jsoncons::basic_json_decode_options<wchar_t>::err_handler() const  | 
304  |  |  | 
305  |  | };  | 
306  |  |  | 
307  |  | template <typename CharT>  | 
308  |  | class basic_json_encode_options : public virtual basic_json_options_common<CharT>  | 
309  |  | { | 
310  |  |     friend class basic_json_options<CharT>;  | 
311  |  |     using super_type = basic_json_options_common<CharT>;  | 
312  |  | public:  | 
313  |  |     using typename super_type::char_type;  | 
314  |  |     using typename super_type::string_type;  | 
315  |  |  | 
316  |  |     static constexpr uint8_t indent_size_default = 4;  | 
317  |  |     static constexpr size_t line_length_limit_default = 120;  | 
318  |  | private:  | 
319  |  |     bool escape_all_non_ascii_:1;  | 
320  |  |     bool escape_solidus_:1;  | 
321  |  |     bool pad_inside_object_braces_:1;  | 
322  |  |     bool pad_inside_array_brackets_:1;  | 
323  |  |     float_chars_format float_format_;  | 
324  |  |     byte_string_chars_format byte_string_format_;  | 
325  |  |     bignum_format_kind bignum_format_;  | 
326  |  |     line_split_kind line_splits_;  | 
327  |  |     line_split_kind object_object_line_splits_;  | 
328  |  |     line_split_kind object_array_line_splits_;  | 
329  |  |     line_split_kind array_array_line_splits_;  | 
330  |  |     line_split_kind array_object_line_splits_;  | 
331  |  |     spaces_option spaces_around_colon_;  | 
332  |  |     spaces_option spaces_around_comma_;  | 
333  |  |     int8_t precision_{0}; | 
334  |  |     uint8_t indent_size_{indent_size_default}; | 
335  |  |     std::size_t line_length_limit_{line_length_limit_default}; | 
336  |  |     string_type new_line_chars_;  | 
337  |  | public:  | 
338  |  |     basic_json_encode_options()  | 
339  |  |         : escape_all_non_ascii_(false),  | 
340  |  |           escape_solidus_(false),  | 
341  |  |           pad_inside_object_braces_(false),  | 
342  |  |           pad_inside_array_brackets_(false),  | 
343  |  |           float_format_(float_chars_format::general),  | 
344  |  |           byte_string_format_(byte_string_chars_format::none),  | 
345  |  |           bignum_format_(bignum_format_kind::raw),  | 
346  |  |           line_splits_(line_split_kind::multi_line),  | 
347  |  |           object_object_line_splits_(line_split_kind{}), | 
348  |  |           object_array_line_splits_(line_split_kind{}), | 
349  |  |           array_array_line_splits_(line_split_kind{}), | 
350  |  |           array_object_line_splits_(line_split_kind{}), | 
351  |  |           spaces_around_colon_(spaces_option::space_after),  | 
352  |  |           spaces_around_comma_(spaces_option::space_after)  | 
353  |  |     { | 
354  |  |         new_line_chars_.push_back('\n'); | 
355  |  |     }  | 
356  |  |  | 
357  |  |     basic_json_encode_options(const basic_json_encode_options&) = default;  | 
358  |  |  | 
359  |  |     basic_json_encode_options(basic_json_encode_options&& other) noexcept  | 
360  |  |         : super_type(std::move(other)),  | 
361  |  |           escape_all_non_ascii_(other.escape_all_non_ascii_),  | 
362  |  |           escape_solidus_(other.escape_solidus_),  | 
363  |  |           pad_inside_object_braces_(other.pad_inside_object_braces_),  | 
364  |  |           pad_inside_array_brackets_(other.pad_inside_array_brackets_),  | 
365  |  |           float_format_(other.float_format_),  | 
366  |  |           byte_string_format_(other.byte_string_format_),  | 
367  |  |           bignum_format_(other.bignum_format_),  | 
368  |  |           line_splits_(other.line_splits_),  | 
369  |  |           object_object_line_splits_(other.object_object_line_splits_),  | 
370  |  |           object_array_line_splits_(other.object_array_line_splits_),  | 
371  |  |           array_array_line_splits_(other.array_array_line_splits_),  | 
372  |  |           array_object_line_splits_(other.array_object_line_splits_),  | 
373  |  |           spaces_around_colon_(other.spaces_around_colon_),  | 
374  |  |           spaces_around_comma_(other.spaces_around_comma_),  | 
375  |  |           precision_(other.precision_),  | 
376  |  |           indent_size_(other.indent_size_),  | 
377  |  |           line_length_limit_(other.line_length_limit_),  | 
378  |  |           new_line_chars_(std::move(other.new_line_chars_))  | 
379  |  |     { | 
380  |  |     }  | 
381  |  |       | 
382  |  |     ~basic_json_encode_options() = default;  | 
383  |  | protected:  | 
384  |  |     basic_json_encode_options& operator=(const basic_json_encode_options&) = default;  | 
385  |  |     basic_json_encode_options& operator=(basic_json_encode_options&&) = default;  | 
386  |  | public:  | 
387  |  |     byte_string_chars_format byte_string_format() const  {return byte_string_format_;} | 
388  |  |  | 
389  |  |  | 
390  |  | #if !defined(JSONCONS_NO_DEPRECATED)  | 
391  |  |     JSONCONS_DEPRECATED_MSG("Instead, use bignum_format") | 
392  |  |     bignum_format_kind bigint_format() const  {return bignum_format_;} | 
393  |  | #endif      | 
394  |  |  | 
395  |  |     bignum_format_kind bignum_format() const  {return bignum_format_;} | 
396  |  |  | 
397  |  |     line_split_kind line_splits() const  {return line_splits_;} | 
398  |  |  | 
399  |  |     line_split_kind object_object_line_splits() const  {return object_object_line_splits_ == line_split_kind{} ? line_splits_ : object_object_line_splits_;} | 
400  |  |  | 
401  |  |     line_split_kind array_object_line_splits() const  {return array_object_line_splits_ == line_split_kind{} ? line_splits_ : array_object_line_splits_;} | 
402  |  |  | 
403  |  |     line_split_kind object_array_line_splits() const  {return object_array_line_splits_ == line_split_kind{} ? line_splits_ : object_array_line_splits_;} | 
404  |  |  | 
405  |  |     line_split_kind array_array_line_splits() const  {return array_array_line_splits_ == line_split_kind{} ? line_splits_ : array_array_line_splits_;} | 
406  |  |  | 
407  |  |     uint8_t indent_size() const   | 
408  |  |     { | 
409  |  |         return indent_size_;  | 
410  |  |     }  | 
411  |  |  | 
412  |  |     spaces_option spaces_around_colon() const   | 
413  |  |     { | 
414  |  |         return spaces_around_colon_;  | 
415  |  |     }  | 
416  |  |  | 
417  |  |     spaces_option spaces_around_comma() const   | 
418  |  |     { | 
419  |  |         return spaces_around_comma_;  | 
420  |  |     }  | 
421  |  |  | 
422  |  |     bool pad_inside_object_braces() const   | 
423  |  |     { | 
424  |  |         return pad_inside_object_braces_;  | 
425  |  |     }  | 
426  |  |  | 
427  |  |     bool pad_inside_array_brackets() const   | 
428  |  |     { | 
429  |  |         return pad_inside_array_brackets_;  | 
430  |  |     }  | 
431  |  |  | 
432  |  |     string_type new_line_chars() const   | 
433  |  |     { | 
434  |  |         return new_line_chars_;  | 
435  |  |     }  | 
436  |  |  | 
437  |  |     std::size_t line_length_limit() const   | 
438  |  |     { | 
439  |  |         return line_length_limit_;  | 
440  |  |     }  | 
441  |  |  | 
442  |  |     float_chars_format float_format() const   | 
443  |  |     { | 
444  |  |         return float_format_;  | 
445  |  |     }  | 
446  |  |  | 
447  |  |     int8_t precision() const   | 
448  |  |     { | 
449  |  |         return precision_;  | 
450  |  |     }  | 
451  |  |  | 
452  |  |     bool escape_all_non_ascii() const   | 
453  |  |     { | 
454  |  |         return escape_all_non_ascii_;  | 
455  |  |     }  | 
456  |  |  | 
457  |  |     bool escape_solidus() const   | 
458  |  |     { | 
459  |  |         return escape_solidus_;  | 
460  |  |     }  | 
461  |  |  | 
462  |  | };  | 
463  |  |  | 
464  |  | template <typename CharT>  | 
465  |  | class basic_json_options final: public basic_json_decode_options<CharT>,   | 
466  |  |                                 public basic_json_encode_options<CharT>  | 
467  |  | { | 
468  |  | public:  | 
469  |  |     using char_type = CharT;  | 
470  |  |     using string_type = std::basic_string<CharT>;  | 
471  |  |  | 
472  |  |     using basic_json_options_common<CharT>::max_nesting_depth;  | 
473  |  |  | 
474  |  |     using basic_json_decode_options<CharT>::enable_str_to_nan;  | 
475  |  |     using basic_json_decode_options<CharT>::enable_str_to_inf;  | 
476  |  |     using basic_json_decode_options<CharT>::enable_str_to_neginf;  | 
477  |  |     using basic_json_decode_options<CharT>::nan_to_str;  | 
478  |  |     using basic_json_decode_options<CharT>::inf_to_str;  | 
479  |  |     using basic_json_decode_options<CharT>::neginf_to_str;  | 
480  |  |     using basic_json_decode_options<CharT>::nan_to_num;  | 
481  |  |     using basic_json_decode_options<CharT>::inf_to_num;  | 
482  |  |     using basic_json_decode_options<CharT>::neginf_to_num;  | 
483  |  |  | 
484  |  |     using basic_json_decode_options<CharT>::lossless_number;  | 
485  |  |     using basic_json_decode_options<CharT>::lossless_bignum;  | 
486  |  |     using basic_json_decode_options<CharT>::allow_comments;  | 
487  |  |     using basic_json_decode_options<CharT>::allow_trailing_comma;  | 
488  |  |     using basic_json_decode_options<CharT>::err_handler;  | 
489  |  |  | 
490  |  |     using basic_json_encode_options<CharT>::byte_string_format;  | 
491  |  |     using basic_json_encode_options<CharT>::bignum_format;  | 
492  |  |     using basic_json_encode_options<CharT>::line_splits;  | 
493  |  |     using basic_json_encode_options<CharT>::object_object_line_splits;  | 
494  |  |     using basic_json_encode_options<CharT>::array_object_line_splits;  | 
495  |  |     using basic_json_encode_options<CharT>::object_array_line_splits;  | 
496  |  |     using basic_json_encode_options<CharT>::array_array_line_splits;  | 
497  |  |     using basic_json_encode_options<CharT>::indent_size;  | 
498  |  |     using basic_json_encode_options<CharT>::spaces_around_colon;  | 
499  |  |     using basic_json_encode_options<CharT>::spaces_around_comma;  | 
500  |  |     using basic_json_encode_options<CharT>::pad_inside_object_braces;  | 
501  |  |     using basic_json_encode_options<CharT>::pad_inside_array_brackets;  | 
502  |  |     using basic_json_encode_options<CharT>::new_line_chars;  | 
503  |  |     using basic_json_encode_options<CharT>::line_length_limit;  | 
504  |  |     using basic_json_encode_options<CharT>::float_format;  | 
505  |  |     using basic_json_encode_options<CharT>::precision;  | 
506  |  |     using basic_json_encode_options<CharT>::escape_all_non_ascii;  | 
507  |  |     using basic_json_encode_options<CharT>::escape_solidus;  | 
508  |  | public:  | 
509  |  |  | 
510  |  | //  Constructors  | 
511  |  |  | 
512  |  |     basic_json_options() = default;  | 
513  |  |     basic_json_options(const basic_json_options&) = default;  | 
514  |  |     basic_json_options(basic_json_options&&) = default;  | 
515  |  |     basic_json_options& operator=(const basic_json_options&) = default;  | 
516  |  |     basic_json_options& operator=(basic_json_options&&) = default;  | 
517  |  |  | 
518  |  |     basic_json_options& nan_to_num(const string_type& value)  | 
519  |  |     { | 
520  |  |         this->enable_nan_to_num_ = true;  | 
521  |  |         this->nan_to_str_.clear();  | 
522  |  |         this->nan_to_num_ = value;  | 
523  |  |         return *this;  | 
524  |  |     }  | 
525  |  |  | 
526  |  |     basic_json_options& inf_to_num(const string_type& value)  | 
527  |  |     { | 
528  |  |         this->enable_inf_to_num_ = true;  | 
529  |  |         this->inf_to_str_.clear();  | 
530  |  |         this->inf_to_num_ = value;  | 
531  |  |         return *this;  | 
532  |  |     }  | 
533  |  |  | 
534  |  |     basic_json_options& neginf_to_num(const string_type& value)  | 
535  |  |     { | 
536  |  |         this->enable_neginf_to_num_ = true;  | 
537  |  |         this->neginf_to_str_.clear();  | 
538  |  |         this->neginf_to_num_ = value;  | 
539  |  |         return *this;  | 
540  |  |     }  | 
541  |  |  | 
542  |  |     basic_json_options& nan_to_str(const string_type& value, bool enable_inverse = true)  | 
543  |  |     { | 
544  |  |         this->enable_nan_to_str_ = true;  | 
545  |  |         this->enable_str_to_nan_ = enable_inverse;  | 
546  |  |         this->nan_to_num_.clear();  | 
547  |  |         this->nan_to_str_ = value;  | 
548  |  |         return *this;  | 
549  |  |     }  | 
550  |  |  | 
551  |  |     basic_json_options& inf_to_str(const string_type& value, bool enable_inverse = true)  | 
552  |  |     { | 
553  |  |         this->enable_inf_to_str_ = true;  | 
554  |  |         this->enable_str_to_inf_ = enable_inverse;  | 
555  |  |         this->inf_to_num_.clear();  | 
556  |  |         this->inf_to_str_ = value;  | 
557  |  |         return *this;  | 
558  |  |     }  | 
559  |  |  | 
560  |  |     basic_json_options& neginf_to_str(const string_type& value, bool enable_inverse = true)  | 
561  |  |     { | 
562  |  |         this->enable_neginf_to_str_ = true;  | 
563  |  |         this->enable_str_to_neginf_ = enable_inverse;  | 
564  |  |         this->neginf_to_num_.clear();  | 
565  |  |         this->neginf_to_str_ = value;  | 
566  |  |         return *this;  | 
567  |  |     }  | 
568  |  |  | 
569  |  |     basic_json_options&  byte_string_format(byte_string_chars_format value) {this->byte_string_format_ = value; return *this;} | 
570  |  |  | 
571  |  |  | 
572  |  | #if !defined(JSONCONS_NO_DEPRECATED)  | 
573  |  |     JSONCONS_DEPRECATED_MSG("Instead, use bignum_format") | 
574  |  |     basic_json_options&  bigint_format(bignum_format_kind value) {this->bignum_format_ = value; return *this;} | 
575  |  | #endif      | 
576  |  |  | 
577  |  |     basic_json_options&  bignum_format(bignum_format_kind value) {this->bignum_format_ = value; return *this;} | 
578  |  |  | 
579  |  |     basic_json_options& line_splits(line_split_kind value) {this->line_splits_ = value; return *this;} | 
580  |  |  | 
581  |  |     basic_json_options& object_object_line_splits(line_split_kind value) {this->object_object_line_splits_ = value; return *this;} | 
582  |  |  | 
583  |  |     basic_json_options& array_object_line_splits(line_split_kind value) {this->array_object_line_splits_ = value; return *this;} | 
584  |  |  | 
585  |  |     basic_json_options& object_array_line_splits(line_split_kind value) {this->object_array_line_splits_ = value; return *this;} | 
586  |  |  | 
587  |  |     basic_json_options& array_array_line_splits(line_split_kind value) {this->array_array_line_splits_ = value; return *this;} | 
588  |  |  | 
589  |  |     basic_json_options& indent_size(uint8_t value)  | 
590  |  |     { | 
591  |  |         this->indent_size_ = value;  | 
592  |  |         return *this;  | 
593  |  |     }  | 
594  |  |  | 
595  |  |     basic_json_options& spaces_around_colon(spaces_option value)  | 
596  |  |     { | 
597  |  |         this->spaces_around_colon_ = value;  | 
598  |  |         return *this;  | 
599  |  |     }  | 
600  |  |  | 
601  |  |     basic_json_options& spaces_around_comma(spaces_option value)  | 
602  |  |     { | 
603  |  |         this->spaces_around_comma_ = value;  | 
604  |  |         return *this;  | 
605  |  |     }  | 
606  |  |  | 
607  |  |     basic_json_options& pad_inside_object_braces(bool value)  | 
608  |  |     { | 
609  |  |         this->pad_inside_object_braces_ = value;  | 
610  |  |         return *this;  | 
611  |  |     }  | 
612  |  |  | 
613  |  |     basic_json_options& pad_inside_array_brackets(bool value)  | 
614  |  |     { | 
615  |  |         this->pad_inside_array_brackets_ = value;  | 
616  |  |         return *this;  | 
617  |  |     }  | 
618  |  |  | 
619  |  |     basic_json_options& new_line_chars(const string_type& value)  | 
620  |  |     { | 
621  |  |         this->new_line_chars_ = value;  | 
622  |  |         return *this;  | 
623  |  |     }  | 
624  |  |  | 
625  |  |     basic_json_options& lossless_number(bool value)   | 
626  |  |     { | 
627  |  |         this->lossless_number_ = value;  | 
628  |  |         return *this;  | 
629  |  |     }  | 
630  |  |  | 
631  |  |     basic_json_options& lossless_bignum(bool value)   | 
632  |  |     { | 
633  |  |         this->lossless_bignum_ = value;  | 
634  |  |         return *this;  | 
635  |  |     }  | 
636  |  |  | 
637  |  |     basic_json_options& allow_comments(bool value)   | 
638  |  |     { | 
639  |  |         this->allow_comments_ = value;  | 
640  |  |         return *this;  | 
641  |  |     }  | 
642  |  |  | 
643  |  |     basic_json_options& allow_trailing_comma(bool value)   | 
644  |  |     { | 
645  |  |         this->allow_trailing_comma_ = value;  | 
646  |  |         return *this;  | 
647  |  |     }  | 
648  |  |  | 
649  |  |     basic_json_options& err_handler(const std::function<bool(json_errc,const ser_context&)>& value)   | 
650  |  |     { | 
651  |  |         this->err_handler_ = value;  | 
652  |  |         return *this;  | 
653  |  |     }  | 
654  |  |  | 
655  |  |     basic_json_options& line_length_limit(std::size_t value)  | 
656  |  |     { | 
657  |  |         this->line_length_limit_ = value;  | 
658  |  |         return *this;  | 
659  |  |     }  | 
660  |  |  | 
661  |  |     basic_json_options& float_format(float_chars_format value)  | 
662  |  |     { | 
663  |  |         this->float_format_ = value;  | 
664  |  |         return *this;  | 
665  |  |     }  | 
666  |  |  | 
667  |  |     basic_json_options& precision(int8_t value)  | 
668  |  |     { | 
669  |  |         this->precision_ = value;  | 
670  |  |         return *this;  | 
671  |  |     }  | 
672  |  |  | 
673  |  |     basic_json_options& escape_all_non_ascii(bool value)  | 
674  |  |     { | 
675  |  |         this->escape_all_non_ascii_ = value;  | 
676  |  |         return *this;  | 
677  |  |     }  | 
678  |  |  | 
679  |  |     basic_json_options& escape_solidus(bool value)  | 
680  |  |     { | 
681  |  |         this->escape_solidus_ = value;  | 
682  |  |         return *this;  | 
683  |  |     }  | 
684  |  |  | 
685  |  |     basic_json_options& max_nesting_depth(int value)  | 
686  |  |     { | 
687  |  |         this->max_nesting_depth_ = value;  | 
688  |  |         return *this;  | 
689  |  |     }  | 
690  |  |  | 
691  |  | private:  | 
692  |  |     enum class input_state {initial,begin_quote,character,end_quote,escape,error}; | 
693  |  |     bool is_string(const string_type& s) const  | 
694  |  |     { | 
695  |  |         input_state state = input_state::initial;  | 
696  |  |         for (char_type c : s)  | 
697  |  |         { | 
698  |  |             switch (c)  | 
699  |  |             { | 
700  |  |             case '\t': case ' ': case '\n': case'\r':  | 
701  |  |                 break;  | 
702  |  |             case '\\':  | 
703  |  |                 state = input_state::escape;  | 
704  |  |                 break;  | 
705  |  |             case '\"':  | 
706  |  |                 switch (state)  | 
707  |  |                 { | 
708  |  |                 case input_state::initial:  | 
709  |  |                     state = input_state::begin_quote;  | 
710  |  |                     break;  | 
711  |  |                 case input_state::begin_quote:  | 
712  |  |                 case input_state::character:  | 
713  |  |                     state = input_state::end_quote;  | 
714  |  |                     break;  | 
715  |  |                 case input_state::end_quote:  | 
716  |  |                     state = input_state::error;  | 
717  |  |                     break;  | 
718  |  |                 case input_state::escape:  | 
719  |  |                     state = input_state::character;  | 
720  |  |                     break;  | 
721  |  |                 default:  | 
722  |  |                     state = input_state::character;  | 
723  |  |                     break;  | 
724  |  |                 }  | 
725  |  |                 break;  | 
726  |  |             default:  | 
727  |  |                 break;  | 
728  |  |             }  | 
729  |  |  | 
730  |  |         }  | 
731  |  |         return state == input_state::end_quote;  | 
732  |  |     }  | 
733  |  | };  | 
734  |  |  | 
735  |  | using json_options = basic_json_options<char>;  | 
736  |  | using wjson_options = basic_json_options<wchar_t>;  | 
737  |  |  | 
738  |  | } // namespace jsoncons  | 
739  |  |  | 
740  |  | #endif // JSONCONS_JSON_OPTIONS_HPP  |