parse_into.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Copyright (c) 2021 Peter Dimov
  3. // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/json
  9. //
  10. #ifndef BOOST_JSON_PARSE_INTO_HPP
  11. #define BOOST_JSON_PARSE_INTO_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/basic_parser.hpp>
  14. #include <boost/json/string_view.hpp>
  15. #include <boost/json/system_error.hpp>
  16. #include <boost/json/detail/parse_into.hpp>
  17. namespace boost {
  18. namespace json {
  19. /** `basic_parser` that parses into a given type
  20. This is an alias template for @ref basic_parser instantiations that use
  21. a dedicated handler that parses directly into an object provided by the
  22. user instead of creating a @ref value.
  23. Objects of type `parser_for<T>` have constructor signature equivalent to
  24. `parser_for( parse_options const&, T& )`.
  25. @tparam T the type to parse into. This type must be
  26. [*DefaultConstructible*](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
  27. */
  28. template< class T >
  29. using parser_for =
  30. #ifndef BOOST_JSON_DOCS
  31. basic_parser<detail::into_handler<T>>;
  32. #else
  33. __see_below__;
  34. #endif
  35. /** Parse a JSON text into a user-defined object.
  36. This function parses an entire string in one step and fills an object
  37. provided by the user. If the buffer does not contain a complete serialized
  38. JSON text, an error occurs. In this case `v` may be partially filled.
  39. The function supports default constructible types satisfying
  40. <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
  41. arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
  42. `std::optional`, `std::nullptr_t`, and structs and enums described using
  43. Boost.Describe.
  44. @par Complexity
  45. Linear in `sv.size()`.
  46. @par Exception Safety
  47. Basic guarantee.
  48. Calls to `memory_resource::allocate` may throw.
  49. @param v The type to parse into.
  50. @param sv The string to parse.
  51. @param ec Set to the error, if any occurred.
  52. @param opt The options for the parser. If this parameter
  53. is omitted, the parser will accept only standard JSON.
  54. */
  55. /** @{ */
  56. template<class V>
  57. void
  58. parse_into(
  59. V& v,
  60. string_view sv,
  61. error_code& ec,
  62. parse_options const& opt = {} );
  63. template<class V>
  64. void
  65. parse_into(
  66. V& v,
  67. string_view sv,
  68. std::error_code& ec,
  69. parse_options const& opt = {} );
  70. /** @} */
  71. /** Parse a JSON text into a user-defined object.
  72. This function parses an entire string in one step and fills an object
  73. provided by the user. If the buffer does not contain a complete serialized
  74. JSON text, an exception is thrown. In this case `v` may be
  75. partially filled.
  76. The function supports default constructible types satisfying
  77. <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
  78. arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
  79. `std::optional`, `std::nullptr_t`, and structs and enums described using
  80. Boost.Describe.
  81. @par Complexity
  82. Linear in `sv.size()`.
  83. @par Exception Safety
  84. Basic guarantee.
  85. Throws @ref system_error on failed parse.
  86. Calls to `memory_resource::allocate` may throw.
  87. @param v The type to parse into.
  88. @param sv The string to parse.
  89. @param opt The options for the parser. If this parameter
  90. is omitted, the parser will accept only standard JSON.
  91. */
  92. template<class V>
  93. void
  94. parse_into(
  95. V& v,
  96. string_view sv,
  97. parse_options const& opt = {} );
  98. /** Parse a JSON text into a user-defined object.
  99. This function reads data from an input stream and fills an object provided
  100. by the user. If the buffer does not contain a complete serialized JSON
  101. text, or contains extra non-whitespace data, an error occurs. In this case
  102. `v` may be partially filled.
  103. The function supports default constructible types satisfying
  104. <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
  105. arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
  106. `std::optional`, `std::nullptr_t`, and structs and enums described using
  107. Boost.Describe.
  108. @par Complexity
  109. Linear in the size of consumed input.
  110. @par Exception Safety
  111. Basic guarantee.
  112. Calls to `memory_resource::allocate` may throw.
  113. The stream may throw as described by
  114. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  115. @param v The type to parse into.
  116. @param is The stream to read from.
  117. @param ec Set to the error, if any occurred.
  118. @param opt The options for the parser. If this parameter
  119. is omitted, the parser will accept only standard JSON.
  120. */
  121. /** @{ */
  122. template<class V>
  123. void
  124. parse_into(
  125. V& v,
  126. std::istream& is,
  127. error_code& ec,
  128. parse_options const& opt = {} );
  129. template<class V>
  130. void
  131. parse_into(
  132. V& v,
  133. std::istream& is,
  134. std::error_code& ec,
  135. parse_options const& opt = {} );
  136. /** @} */
  137. /** Parse a JSON text into a user-defined object.
  138. This function reads data from an input stream and fills an object provided
  139. by the user. If the buffer does not contain a complete serialized JSON
  140. text, or contains extra non-whitespace data, an exception is thrown. In
  141. this case `v` may be partially filled.
  142. The function supports default constructible types satisfying
  143. <a href="https://en.cppreference.com/w/cpp/named_req/SequenceContainer"><em>SequenceContainer</em></a>,
  144. arrays, arithmetic types, `bool`, `std::tuple`, `std::pair`,
  145. `std::optional`, `std::nullptr_t`, and structs and enums described using
  146. Boost.Describe.
  147. @par Complexity
  148. Linear in the size of consumed input.
  149. @par Exception Safety
  150. Basic guarantee.
  151. Throws @ref system_error on failed parse.
  152. Calls to `memory_resource::allocate` may throw.
  153. The stream may throw as described by
  154. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  155. @param v The type to parse into.
  156. @param is The stream to read from.
  157. @param opt The options for the parser. If this parameter
  158. is omitted, the parser will accept only standard JSON.
  159. */
  160. template<class V>
  161. void
  162. parse_into(
  163. V& v,
  164. std::istream& is,
  165. parse_options const& opt = {} );
  166. } // namespace boost
  167. } // namespace json
  168. #include <boost/json/impl/parse_into.hpp>
  169. #endif