parse_options.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Copyright (c) 2020 Krystian Stasiowski ([email protected])
  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 `Infinity`, `-Infinity`, and `NaN` JSON literals. These values
  94. are produced by some popular JSON implementations for positive
  95. infinity, negative infinity and NaN special numbers respectively.
  96. @see
  97. @ref basic_parser,
  98. @ref stream_parser.
  99. */
  100. bool allow_infinity_and_nan = false;
  101. /** Set JSON parse options on input stream.
  102. The function stores parse options in the private storage of the stream. If
  103. the stream fails to allocate necessary private storage, `badbit` will be
  104. set on it.
  105. @return Reference to `is`.
  106. @par Complexity
  107. Amortized constant (due to potential memory allocation by the stream).
  108. @par Exception Safety
  109. Strong guarantee.
  110. The stream may throw as configured by
  111. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  112. @param is The input stream.
  113. @param opts The options to store.
  114. */
  115. BOOST_JSON_DECL
  116. friend
  117. std::istream&
  118. operator>>( std::istream& is, parse_options const& opts );
  119. };
  120. } // namespace json
  121. } // namespace boost
  122. #endif