serializer.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  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_SERIALIZER_HPP
  10. #define BOOST_JSON_SERIALIZER_HPP
  11. #include <boost/json/detail/config.hpp>
  12. #include <boost/json/detail/format.hpp>
  13. #include <boost/json/detail/stream.hpp>
  14. #include <boost/json/detail/writer.hpp>
  15. #include <boost/json/serialize_options.hpp>
  16. #include <boost/json/value.hpp>
  17. namespace boost {
  18. namespace json {
  19. /** A serializer for JSON.
  20. This class traverses an instance of a library type and emits serialized
  21. JSON text by filling in one or more caller-provided buffers. To use,
  22. declare a variable and call @ref reset with a pointer to the variable you
  23. want to serialize. Then call @ref read over and over until @ref done
  24. returns `true`.
  25. @par Example
  26. This demonstrates how the serializer may be used to print a JSON value to
  27. an output stream.
  28. @code
  29. void print( std::ostream& os, value const& jv)
  30. {
  31. serializer sr;
  32. sr.reset( &jv );
  33. while( ! sr.done() )
  34. {
  35. char buf[ 4000 ];
  36. os << sr.read( buf );
  37. }
  38. }
  39. @endcode
  40. @par Thread Safety
  41. The same instance may not be accessed concurrently.
  42. @par Non-Standard JSON
  43. The @ref serialize_options structure optionally provided upon construction
  44. is used to enable non-standard JSON extensions. A default-constructed
  45. `serialize_options` doesn't enable any extensions.
  46. @see @ref serialize.
  47. */
  48. class serializer
  49. : detail::writer
  50. {
  51. using fn_t = bool (*)(writer&, detail::stream&);
  52. fn_t fn0_ = nullptr;
  53. fn_t fn1_ = nullptr;
  54. bool done_ = false;
  55. public:
  56. /** Destructor
  57. All temporary storage is deallocated.
  58. @par Complexity
  59. Constant
  60. @par Exception Safety
  61. No-throw guarantee.
  62. */
  63. #ifdef BOOST_JSON_DOCS
  64. BOOST_JSON_DECL
  65. ~serializer() noexcept;
  66. #endif // BOOST_JSON_DOCS
  67. /** Constructors.
  68. The serializer is constructed with no value to serialize The value may
  69. be set later by calling @ref reset. If serialization is attempted with
  70. no value, the output is as if a null value is serialized.
  71. Overload **(3)** is a move constructor. The type is neither copyable
  72. nor movable, so this constructor is deleted.
  73. @par Complexity
  74. Constant.
  75. @par Exception Safety
  76. No-throw guarantee.
  77. @param opts The options for the serializer. If this parameter is
  78. omitted, the serializer will output only standard JSON.
  79. @{
  80. */
  81. BOOST_JSON_DECL
  82. serializer( serialize_options const& opts = {} ) noexcept;
  83. /** Overload
  84. @param sp A pointer to the @ref boost::container::pmr::memory_resource
  85. to use when producing partial output. Shared ownership of the memory
  86. resource is retained until the serializer is destroyed.
  87. @param buf An optional static buffer to use for temporary storage when
  88. producing partial output.
  89. @param size The number of bytes of valid memory pointed to by
  90. `buf`.
  91. @param opts
  92. */
  93. BOOST_JSON_DECL
  94. serializer(
  95. storage_ptr sp,
  96. unsigned char* buf = nullptr,
  97. std::size_t size = 0,
  98. serialize_options const& opts = {}) noexcept;
  99. /// Overload
  100. serializer(serializer&&) = delete;
  101. /// @}
  102. /** Check if the serialization is complete.
  103. This function returns `true` when all of the characters in the
  104. serialized representation of the value have been read.
  105. @par Complexity
  106. Constant.
  107. @par Exception Safety
  108. No-throw guarantee.
  109. */
  110. bool
  111. done() const noexcept
  112. {
  113. return done_;
  114. }
  115. /** Reset the serializer for a new element.
  116. This function prepares the serializer to emit a new serialized JSON
  117. representing its argument: `*p` **(1)**--**(5)**, `sv` **(6)**, or
  118. `np` **(7)**. Ownership is not transferred. The caller is responsible
  119. for ensuring that the lifetime of the object pointed to by the argument
  120. extends until it is no longer needed.
  121. Any memory internally allocated for previous uses of this `serializer`
  122. object is preserved and re-used for the new output.
  123. Overload **(5)** uses \<\<direct_conversion,direct serialization\>\>.
  124. @param p A pointer to the element to serialize.
  125. @{
  126. */
  127. BOOST_JSON_DECL
  128. void
  129. reset(value const* p) noexcept;
  130. BOOST_JSON_DECL
  131. void
  132. reset(array const* p) noexcept;
  133. BOOST_JSON_DECL
  134. void
  135. reset(object const* p) noexcept;
  136. BOOST_JSON_DECL
  137. void
  138. reset(string const* p) noexcept;
  139. template<class T>
  140. void
  141. reset(T const* p) noexcept;
  142. /** Overload
  143. @param sv The characters representing a string.
  144. */
  145. BOOST_JSON_DECL
  146. void
  147. reset(string_view sv) noexcept;
  148. /** Overload
  149. @param np Represents a null value.
  150. */
  151. BOOST_JSON_DECL
  152. void
  153. reset(std::nullptr_t np) noexcept;
  154. /// @}
  155. /** Read the next buffer of serialized JSON.
  156. This function attempts to fill the caller provided buffer starting at
  157. `dest` with up to `size` characters of the serialized JSON that
  158. represents the value. If the buffer is not large enough, multiple calls
  159. may be required.
  160. If serialization completes during this call; that is, that all of the
  161. characters belonging to the serialized value have been written to
  162. caller-provided buffers, the function @ref done will return `true`.
  163. @pre
  164. @code
  165. done() == false
  166. @endcode
  167. @par Complexity
  168. @li **(1)** linear in `size`.
  169. @li **(2)** linear in `N`.
  170. @par Exception Safety
  171. Basic guarantee. Calls to `memory_resource::allocate` may throw.
  172. @return A @ref string_view containing the characters written, which may
  173. be less than `size` or `N`.
  174. @param dest A pointer to storage to write into.
  175. @param size The maximum number of characters to write to the memory
  176. pointed to by `dest`.
  177. @{
  178. */
  179. BOOST_JSON_DECL
  180. string_view
  181. read(char* dest, std::size_t size);
  182. /** Overload
  183. @tparam N The size of the array `dest`.
  184. @param dest
  185. */
  186. template<std::size_t N>
  187. string_view
  188. read(char(&dest)[N])
  189. {
  190. return read(dest, N);
  191. }
  192. /// @}
  193. #ifndef BOOST_JSON_DOCS
  194. // Safety net for accidental buffer overflows
  195. template<std::size_t N>
  196. string_view
  197. read(char(&dest)[N], std::size_t n)
  198. {
  199. // If this goes off, check your parameters
  200. // closely, chances are you passed an array
  201. // thinking it was a pointer.
  202. BOOST_ASSERT(n <= N);
  203. return read(dest, n);
  204. }
  205. #endif
  206. };
  207. } // namespace json
  208. } // namespace boost
  209. #include <boost/json/impl/serializer.hpp>
  210. #endif