parse.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. // Copyright (c) 2020 Krystian Stasiowski ([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_HPP
  11. #define BOOST_JSON_PARSE_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/error.hpp>
  14. #include <boost/json/parse_options.hpp>
  15. #include <boost/json/storage_ptr.hpp>
  16. #include <boost/json/string_view.hpp>
  17. #include <boost/json/value.hpp>
  18. namespace boost {
  19. namespace json {
  20. /** Return parsed JSON as a @ref value.
  21. This function parses an entire string in one
  22. step to produce a complete JSON object, returned
  23. as a @ref value. If the buffer does not contain a
  24. complete serialized JSON, an error occurs. In this
  25. case the returned value will be null, using the
  26. [default memory resource].
  27. @par Complexity
  28. Linear in `s.size()`.
  29. @par Exception Safety
  30. Strong guarantee.
  31. Calls to `memory_resource::allocate` may throw.
  32. @return A value representing the parsed JSON,
  33. or a null if any error occurred.
  34. @param s The string to parse.
  35. @param ec Set to the error, if any occurred.
  36. @param sp The memory resource that the new value and all
  37. of its elements will use. If this parameter is omitted,
  38. the [default memory resource] is used.
  39. @param opt The options for the parser. If this parameter
  40. is omitted, the parser will accept only standard JSON.
  41. @see
  42. @ref parse_options,
  43. @ref stream_parser.
  44. [default memory resource]: json/allocators/storage_ptr.html#json.allocators.storage_ptr.default_memory_resource
  45. */
  46. /** @{ */
  47. BOOST_JSON_DECL
  48. value
  49. parse(
  50. string_view s,
  51. system::error_code& ec,
  52. storage_ptr sp = {},
  53. parse_options const& opt = {});
  54. BOOST_JSON_DECL
  55. value
  56. parse(
  57. string_view s,
  58. std::error_code& ec,
  59. storage_ptr sp = {},
  60. parse_options const& opt = {});
  61. /** @} */
  62. /** Return parsed JSON as a @ref value.
  63. This function parses an entire string in one
  64. step to produce a complete JSON object, returned
  65. as a @ref value. If the buffer does not contain a
  66. complete serialized JSON, an exception is thrown.
  67. @par Complexity
  68. Linear in `s.size()`.
  69. @par Exception Safety
  70. Strong guarantee.
  71. Calls to `memory_resource::allocate` may throw.
  72. @return A value representing the parsed
  73. JSON upon success.
  74. @param s The string to parse.
  75. @param sp The memory resource that the new value and all
  76. of its elements will use. If this parameter is omitted,
  77. the [default memory resource] is used.
  78. @param opt The options for the parser. If this parameter
  79. is omitted, the parser will accept only standard JSON.
  80. @throw boost::system::system_error Thrown on failure.
  81. @see
  82. @ref parse_options,
  83. @ref stream_parser.
  84. [default memory resource]: json/allocators/storage_ptr.html#json.allocators.storage_ptr.default_memory_resource
  85. */
  86. BOOST_JSON_DECL
  87. value
  88. parse(
  89. string_view s,
  90. storage_ptr sp = {},
  91. parse_options const& opt = {});
  92. /** Return parsed JSON as a @ref value.
  93. This function reads data from an input stream and parses it to produce a
  94. complete JSON entity, returned as a @ref value. If the stream does not
  95. contain a complete serialized JSON, or contains extra non-whitespace data,
  96. an error occurs. In this case the returned value will be `null`, using the
  97. [default memory resource].
  98. @par Complexity
  99. Linear in the size of consumed input.
  100. @par Exception Safety
  101. Basic guarantee.
  102. Calls to `memory_resource::allocate` may throw.
  103. The stream may throw as described by
  104. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  105. @return A value representing the parsed JSON,
  106. or a `null` if any error occurred.
  107. @param is The stream to read from.
  108. @param ec Set to the error, if any occurred.
  109. @param sp The memory resource that the new value and all of its elements
  110. will use. If this parameter is omitted, the [default memory resource]
  111. is used.
  112. @param opt The options for the parser. If this parameter is omitted, the
  113. parser will accept only standard JSON.
  114. @see @ref parse_options, @ref stream_parser, @ref value::operator>>.
  115. [default memory resource]: json/allocators/storage_ptr.html#json.allocators.storage_ptr.default_memory_resource
  116. */
  117. /** @{ */
  118. BOOST_JSON_DECL
  119. value
  120. parse(
  121. std::istream& is,
  122. system::error_code& ec,
  123. storage_ptr sp = {},
  124. parse_options const& opt = {});
  125. BOOST_JSON_DECL
  126. value
  127. parse(
  128. std::istream& is,
  129. std::error_code& ec,
  130. storage_ptr sp = {},
  131. parse_options const& opt = {});
  132. /** @} */
  133. /** Return parsed JSON as a @ref value.
  134. This function reads data from an input stream and parses it to produce a
  135. complete JSON entity, returned as a @ref value. If the stream does not
  136. contain a complete serialized JSON, or contains extra non-whitespace data,
  137. an exception is thrown.
  138. @par Complexity
  139. Linear in the size of consumed input.
  140. @par Exception Safety
  141. Basic guarantee.
  142. Throws `boost::system::system_error` on failed parse.
  143. Calls to `memory_resource::allocate` may throw.
  144. The stream may throw as described by
  145. [`std::ios::exceptions`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions).
  146. @return A value representing the parsed JSON upon success.
  147. @param is The stream to read from.
  148. @param sp The memory resource that the new value and all of its elements
  149. will use. If this parameter is omitted, the [default memory resource]
  150. is used.
  151. @param opt The options for the parser. If this parameter is omitted, the
  152. parser will accept only standard JSON.
  153. @see @ref parse_options, @ref stream_parser, @ref value::operator>>.
  154. [default memory resource]: json/allocators/storage_ptr.html#json.allocators.storage_ptr.default_memory_resource
  155. */
  156. BOOST_JSON_DECL
  157. value
  158. parse(
  159. std::istream& is,
  160. storage_ptr sp = {},
  161. parse_options const& opt = {});
  162. } // namespace json
  163. } // namespace boost
  164. #endif