parse_options.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.
  17. @see
  18. @ref parse_options,
  19. @ref basic_parser,
  20. @ref parser.
  21. */
  22. enum class number_precision : unsigned char
  23. {
  24. /// Fast, but potentially less precise mode.
  25. imprecise,
  26. /// Slower, but precise mode.
  27. precise,
  28. /// The fastest mode, that only validates encountered numbers without
  29. /// parsing them.
  30. none,
  31. };
  32. /** Parser options
  33. This structure is used for specifying
  34. maximum parsing depth, and whether
  35. to allow various non-standard extensions.
  36. Default-constructed options set maximum
  37. parsing depth to 32 and specify that only
  38. standard JSON is allowed,
  39. @see
  40. @ref basic_parser,
  41. @ref parser.
  42. */
  43. struct parse_options
  44. {
  45. /** Maximum nesting level of arrays and objects.
  46. This specifies the maximum number of nested
  47. structures allowed while parsing a JSON text. If
  48. this limit is exceeded during a parse, an
  49. error is returned.
  50. @see
  51. @ref basic_parser,
  52. @ref stream_parser.
  53. */
  54. std::size_t max_depth = 32;
  55. /** Number pasing mode
  56. This selects the way to parse numbers. The default is to parse them
  57. fast, but with possible slight imprecision for floating point numbers
  58. with larger mantissas. Users can also choose to parse numbers slower
  59. but with full precision. Or to not parse them at all, and only validate
  60. numbers. The latter mode is useful for @ref basic_parser instantiations
  61. that wish to treat numbers in a custom way.
  62. @see
  63. @ref basic_parser,
  64. @ref stream_parser.
  65. */
  66. number_precision numbers = number_precision::imprecise;
  67. /** Non-standard extension option
  68. Allow C and C++ style comments to appear
  69. anywhere that whitespace is permissible.
  70. @see
  71. @ref basic_parser,
  72. @ref stream_parser.
  73. */
  74. bool allow_comments = false;
  75. /** Non-standard extension option
  76. Allow a trailing comma to appear after
  77. the last element of any array or object.
  78. @see
  79. @ref basic_parser,
  80. @ref stream_parser.
  81. */
  82. bool allow_trailing_commas = false;
  83. /** Non-standard extension option
  84. Allow invalid UTF-8 sequnces to appear
  85. in keys and strings.
  86. @note This increases parsing performance.
  87. @see
  88. @ref basic_parser,
  89. @ref stream_parser.
  90. */
  91. bool allow_invalid_utf8 = false;
  92. /** Non-standard extension option
  93. Allow invalid UTF-16 surrogate pairs to appear
  94. in strings. When enabled, the parser will not
  95. strictly validate the correctness of UTF-16
  96. encoding, allowing for the presence of illegal
  97. leading or trailing surrogates. In case of
  98. invalid sequences, the parser will replace them
  99. with the Unicode replacement character.
  100. @note Enabling this option may result in the
  101. parsing of invalid UTF-16 sequences without
  102. error, potentially leading to the loss of information.
  103. */
  104. bool allow_invalid_utf16 = false;
  105. /** Non-standard extension option
  106. Allow `Infinity`, `-Infinity`, and `NaN` JSON literals. These values
  107. are produced by some popular JSON implementations for positive
  108. infinity, negative infinity and NaN special numbers respectively.
  109. @see
  110. @ref basic_parser,
  111. @ref stream_parser.
  112. */
  113. bool allow_infinity_and_nan = false;
  114. /** Set JSON parse options on input stream.
  115. The function stores parse options in the private storage of the stream. If
  116. the stream fails to allocate necessary private storage, `badbit` will be
  117. set on it.
  118. @return Reference to `is`.
  119. @par Complexity
  120. Amortized constant (due to potential memory allocation by the stream).
  121. @par Exception Safety
  122. Strong guarantee.
  123. The stream may throw as configured by
  124. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  125. @param is The input stream.
  126. @param opts The options to store.
  127. */
  128. BOOST_JSON_DECL
  129. friend
  130. std::istream&
  131. operator>>( std::istream& is, parse_options const& opts );
  132. };
  133. } // namespace json
  134. } // namespace boost
  135. #endif