serialize.ipp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_IMPL_SERIALIZE_IPP
  10. #define BOOST_JSON_IMPL_SERIALIZE_IPP
  11. #include <boost/json/serialize.hpp>
  12. #include <boost/json/serializer.hpp>
  13. #include <ostream>
  14. namespace boost {
  15. namespace json {
  16. namespace {
  17. int serialize_xalloc = std::ios::xalloc();
  18. enum class serialize_stream_flags : long
  19. {
  20. allow_infinity_and_nan = 1,
  21. };
  22. std::underlying_type<serialize_stream_flags>::type
  23. to_bitmask( serialize_options const& opts )
  24. {
  25. using E = serialize_stream_flags;
  26. using I = std::underlying_type<E>::type;
  27. return (opts.allow_infinity_and_nan
  28. ? static_cast<I>(E::allow_infinity_and_nan) : 0);
  29. }
  30. serialize_options
  31. get_stream_flags( std::ostream& os )
  32. {
  33. auto const flags = os.iword(serialize_xalloc);
  34. serialize_options opts;
  35. using E = serialize_stream_flags;
  36. using I = std::underlying_type<E>::type;
  37. opts.allow_infinity_and_nan =
  38. flags & static_cast<I>(E::allow_infinity_and_nan);
  39. return opts;
  40. }
  41. } // namespace
  42. namespace detail {
  43. void
  44. serialize_impl(
  45. std::string& s,
  46. serializer& sr)
  47. {
  48. // serialize to a small buffer to avoid
  49. // the first few allocations in std::string
  50. char buf[BOOST_JSON_STACK_BUFFER_SIZE];
  51. string_view sv;
  52. sv = sr.read(buf);
  53. if(sr.done())
  54. {
  55. // fast path
  56. s.append(
  57. sv.data(), sv.size());
  58. return;
  59. }
  60. std::size_t len = sv.size();
  61. s.reserve(len * 2);
  62. s.resize(s.capacity());
  63. BOOST_ASSERT(
  64. s.size() >= len * 2);
  65. std::memcpy(&s[0],
  66. sv.data(), sv.size());
  67. auto const lim =
  68. s.max_size() / 2;
  69. for(;;)
  70. {
  71. sv = sr.read(
  72. &s[0] + len,
  73. s.size() - len);
  74. len += sv.size();
  75. if(sr.done())
  76. break;
  77. // growth factor 2x
  78. if(s.size() < lim)
  79. s.resize(s.size() * 2);
  80. else
  81. s.resize(2 * lim);
  82. }
  83. s.resize(len);
  84. }
  85. } // namespace detail
  86. std::string
  87. serialize(
  88. value const& jv,
  89. serialize_options const& opts)
  90. {
  91. unsigned char buf[256];
  92. serializer sr(
  93. storage_ptr(),
  94. buf,
  95. sizeof(buf),
  96. opts);
  97. sr.reset(&jv);
  98. std::string s;
  99. serialize_impl(s, sr);
  100. return s;
  101. }
  102. std::string
  103. serialize(
  104. array const& arr,
  105. serialize_options const& opts)
  106. {
  107. unsigned char buf[256];
  108. serializer sr(
  109. storage_ptr(),
  110. buf,
  111. sizeof(buf),
  112. opts);
  113. std::string s;
  114. sr.reset(&arr);
  115. serialize_impl(s, sr);
  116. return s;
  117. }
  118. std::string
  119. serialize(
  120. object const& obj,
  121. serialize_options const& opts)
  122. {
  123. unsigned char buf[256];
  124. serializer sr(
  125. storage_ptr(),
  126. buf,
  127. sizeof(buf),
  128. opts);
  129. std::string s;
  130. sr.reset(&obj);
  131. serialize_impl(s, sr);
  132. return s;
  133. }
  134. std::string
  135. serialize(
  136. string const& str,
  137. serialize_options const& opts)
  138. {
  139. return serialize( str.subview(), opts );
  140. }
  141. // this is here for key_value_pair::key()
  142. std::string
  143. serialize(
  144. string_view sv,
  145. serialize_options const& opts)
  146. {
  147. unsigned char buf[256];
  148. serializer sr(
  149. storage_ptr(),
  150. buf,
  151. sizeof(buf),
  152. opts);
  153. std::string s;
  154. sr.reset(sv);
  155. serialize_impl(s, sr);
  156. return s;
  157. }
  158. //----------------------------------------------------------
  159. // tag::example_operator_lt_lt[]
  160. // Serialize a value into an output stream
  161. std::ostream&
  162. operator<<( std::ostream& os, value const& jv )
  163. {
  164. // Create a serializer
  165. serializer sr( get_stream_flags(os) );
  166. // Set the serializer up for our value
  167. sr.reset( &jv );
  168. // Loop until all output is produced.
  169. while( ! sr.done() )
  170. {
  171. // Use a local buffer to avoid allocation.
  172. char buf[ BOOST_JSON_STACK_BUFFER_SIZE ];
  173. // Fill our buffer with serialized characters and write it to the output stream.
  174. os << sr.read( buf );
  175. }
  176. return os;
  177. }
  178. // end::example_operator_lt_lt[]
  179. static
  180. void
  181. to_ostream(
  182. std::ostream& os,
  183. serializer& sr)
  184. {
  185. while(! sr.done())
  186. {
  187. char buf[BOOST_JSON_STACK_BUFFER_SIZE];
  188. auto s = sr.read(buf);
  189. os.write(s.data(), s.size());
  190. }
  191. }
  192. std::ostream&
  193. operator<<(
  194. std::ostream& os,
  195. array const& arr)
  196. {
  197. serializer sr( get_stream_flags(os) );
  198. sr.reset(&arr);
  199. to_ostream(os, sr);
  200. return os;
  201. }
  202. std::ostream&
  203. operator<<(
  204. std::ostream& os,
  205. object const& obj)
  206. {
  207. serializer sr( get_stream_flags(os) );
  208. sr.reset(&obj);
  209. to_ostream(os, sr);
  210. return os;
  211. }
  212. std::ostream&
  213. operator<<(
  214. std::ostream& os,
  215. string const& str)
  216. {
  217. serializer sr( get_stream_flags(os) );
  218. sr.reset(&str);
  219. to_ostream(os, sr);
  220. return os;
  221. }
  222. std::ostream&
  223. operator<<( std::ostream& os, serialize_options const& opts )
  224. {
  225. os.iword(serialize_xalloc) = to_bitmask(opts);
  226. return os;
  227. }
  228. } // namespace json
  229. } // namespace boost
  230. #endif