parse_into.hpp 6.5 KB

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