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