parse_into.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/detail/parse_into.hpp>
  16. #include <boost/system/result.hpp>
  17. namespace boost {
  18. namespace json {
  19. /** @ref 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. {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 a string and fills an object provided by the user.
  37. If the buffer does not contain a complete serialized JSON text, an error
  38. occurs. In this case `v` may be partially filled. Overloads
  39. __(1)__--**(3)** consume the entire string `s`. Overloads **(4)**--**(6)**
  40. read characters from the input stream `is`. All overloads consume all
  41. available characters, and produce an error if there are non-whitespace
  42. characters after the initial JSON.
  43. The function supports default constructible types satisfying
  44. {req_SequenceContainer}, arrays, arithmetic types, `bool`, `std::tuple`,
  45. `std::pair`, `std::optional`, `std::variant`, `std::nullptr_t`, and structs
  46. and enums described using Boost.Describe.
  47. @par Complexity
  48. @li **(1)**--**(3)** linear in `sv.size()`.
  49. @li **(4)**--**(6)** linear in the size of consumed input.
  50. @par Exception Safety
  51. Basic guarantee. Calls to `memory_resource::allocate` may throw. Overloads
  52. __(3)__ and **(6)** throw @ref boost::system::system_error on error.
  53. The stream `is` may throw as described by @ref std::ios::exceptions.
  54. @param v The type to parse into.
  55. @param sv The string to parse.
  56. @param ec Set to the error, if any occurred.
  57. @param opt The options for the parser. If this parameter is omitted, the
  58. parser will accept only standard JSON.
  59. @{
  60. */
  61. template<class V>
  62. void
  63. parse_into(
  64. V& v,
  65. string_view sv,
  66. system::error_code& ec,
  67. parse_options const& opt = {} );
  68. template<class V>
  69. void
  70. parse_into(
  71. V& v,
  72. string_view sv,
  73. std::error_code& ec,
  74. parse_options const& opt = {} );
  75. /// Overload
  76. template<class V>
  77. void
  78. parse_into(
  79. V& v,
  80. string_view sv,
  81. parse_options const& opt = {} );
  82. /** Overload
  83. @param is The stream to read from.
  84. @param v
  85. @param ec
  86. @param opt
  87. */
  88. template<class V>
  89. void
  90. parse_into(
  91. V& v,
  92. std::istream& is,
  93. system::error_code& ec,
  94. parse_options const& opt = {} );
  95. /// Overload
  96. template<class V>
  97. void
  98. parse_into(
  99. V& v,
  100. std::istream& is,
  101. std::error_code& ec,
  102. parse_options const& opt = {} );
  103. /// Overload
  104. template<class V>
  105. void
  106. parse_into(
  107. V& v,
  108. std::istream& is,
  109. parse_options const& opt = {} );
  110. /// @}
  111. } // namespace boost
  112. } // namespace json
  113. #include <boost/json/impl/parse_into.hpp>
  114. #endif