parse_options.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_PARSE_OPTIONS_HPP
  10. #define BOOST_JSON_PARSE_OPTIONS_HPP
  11. #include <boost/json/detail/config.hpp>
  12. #include <iosfwd>
  13. namespace boost {
  14. namespace json {
  15. /** Enumeration of number parsing modes
  16. These values are used to select the way to parse numbers. The default
  17. mode used by parsing functions is `imprecise`. It has the precision not
  18. less than 15 decimal places (the number required for IEEE `double`). This
  19. is also the number _guaranteed_ by functions in most C++ standard library
  20. implementations.
  21. But those functions often give a more precise result for certain numbers.
  22. For cases where such extra precision is needed the library provides
  23. `precise` mode. That extra precision comes with a performance cost, though.
  24. Finally, users might want a to parse numbers with an external function
  25. when they implement a custom handler for `basic_parser`. Since it is
  26. wasteful to parse a number only to throw the result away, the library
  27. provides a mode to not parse numbers at all. Note: the library still have
  28. to validate that the input is a number, in order to catch syntax errors and
  29. to know when the number ends.
  30. @see
  31. @ref parse_options,
  32. @ref basic_parser,
  33. @ref parser.
  34. */
  35. enum class number_precision : unsigned char
  36. {
  37. /// Fast, but potentially less precise mode.
  38. imprecise,
  39. /// Slower, but precise mode.
  40. precise,
  41. /// The fastest mode, that only validates encountered numbers without
  42. /// parsing them.
  43. none,
  44. };
  45. /** Parser options.
  46. This structure is used for specifying maximum parsing depth, and whether to
  47. allow various non-standard extensions. Default-constructed options set
  48. maximum parsing depth to 32 and specify that only standard JSON is allowed,
  49. @see @ref parse, @ref parser, @ref basic_parser.
  50. */
  51. struct parse_options
  52. {
  53. /** Maximum nesting level of arrays and objects.
  54. This specifies the maximum number of nested structures allowed while
  55. parsing a JSON text. If this limit is exceeded during a parse, an error
  56. is returned.
  57. @see @ref basic_parser, @ref stream_parser.
  58. */
  59. std::size_t max_depth = 32;
  60. /** Number pasing mode.
  61. This selects the way to parse numbers. The default is to parse them
  62. fast, but with possible slight imprecision for floating point numbers
  63. with larger mantissas. Users can also choose to parse numbers slower
  64. but with full precision. Or to not parse them at all, and only validate
  65. numbers. The latter mode is useful for @ref basic_parser instantiations
  66. that wish to treat numbers in a custom way.
  67. @see @ref basic_parser, @ref stream_parser.
  68. */
  69. number_precision numbers = number_precision::imprecise;
  70. /** Non-standard extension option.
  71. Allow C and C++ style comments to appear anywhere that whitespace is
  72. permissible.
  73. @see @ref basic_parser, @ref stream_parser.
  74. */
  75. bool allow_comments = false;
  76. /** Non-standard extension option
  77. Allow a trailing comma to appear after the last element of any array or
  78. object.
  79. @see @ref basic_parser, @ref stream_parser.
  80. */
  81. bool allow_trailing_commas = false;
  82. /** Non-standard extension option
  83. Allow invalid UTF-8 sequences to appear in keys and strings.
  84. @note This increases parsing performance.
  85. @see @ref basic_parser, @ref stream_parser.
  86. */
  87. bool allow_invalid_utf8 = false;
  88. /** Non-standard extension option
  89. Allow invalid UTF-16 surrogate pairs to appear in strings. When
  90. enabled, the parser will not strictly validate the correctness of
  91. UTF-16 encoding, allowing for the presence of illegal leading or
  92. trailing surrogates. In case of invalid sequences, the parser will
  93. replace them with the Unicode replacement character (`U+FFFD`).
  94. @attention Enabling this option may result in the parsing of invalid
  95. UTF-16 sequences without error, potentially leading to the loss of
  96. information.
  97. */
  98. bool allow_invalid_utf16 = false;
  99. /** Non-standard extension option
  100. Allow `Infinity`, `-Infinity`, and `NaN` JSON literals. These values
  101. are produced by some popular JSON implementations for positive
  102. infinity, negative infinity and NaN special numbers respectively.
  103. @see @ref basic_parser, @ref stream_parser.
  104. */
  105. bool allow_infinity_and_nan = false;
  106. /** Set JSON parse options on input stream.
  107. The function stores parse options in the private storage of the stream.
  108. If the stream fails to allocate necessary private storage, `badbit`
  109. will be set on it.
  110. @return Reference to `is`.
  111. @par Complexity
  112. Amortized constant (due to potential memory allocation by the stream).
  113. @par Exception Safety
  114. Strong guarantee.
  115. The stream may throw as configured by @ref std::ios::exceptions.
  116. @param is The input stream.
  117. @param opts The options to store.
  118. */
  119. BOOST_JSON_DECL
  120. friend
  121. std::istream&
  122. operator>>( std::istream& is, parse_options const& opts );
  123. };
  124. } // namespace json
  125. } // namespace boost
  126. #endif