serialize.ipp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  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. static
  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. std::string
  86. serialize(
  87. value const& jv,
  88. serialize_options const& opts)
  89. {
  90. unsigned char buf[256];
  91. serializer sr(
  92. storage_ptr(),
  93. buf,
  94. sizeof(buf),
  95. opts);
  96. sr.reset(&jv);
  97. std::string s;
  98. serialize_impl(s, sr);
  99. return s;
  100. }
  101. std::string
  102. serialize(
  103. array const& arr,
  104. serialize_options const& opts)
  105. {
  106. unsigned char buf[256];
  107. serializer sr(
  108. storage_ptr(),
  109. buf,
  110. sizeof(buf),
  111. opts);
  112. std::string s;
  113. sr.reset(&arr);
  114. serialize_impl(s, sr);
  115. return s;
  116. }
  117. std::string
  118. serialize(
  119. object const& obj,
  120. serialize_options const& opts)
  121. {
  122. unsigned char buf[256];
  123. serializer sr(
  124. storage_ptr(),
  125. buf,
  126. sizeof(buf),
  127. opts);
  128. std::string s;
  129. sr.reset(&obj);
  130. serialize_impl(s, sr);
  131. return s;
  132. }
  133. std::string
  134. serialize(
  135. string const& str,
  136. serialize_options const& opts)
  137. {
  138. return serialize( str.subview(), opts );
  139. }
  140. // this is here for key_value_pair::key()
  141. std::string
  142. serialize(
  143. string_view sv,
  144. serialize_options const& opts)
  145. {
  146. unsigned char buf[256];
  147. serializer sr(
  148. storage_ptr(),
  149. buf,
  150. sizeof(buf),
  151. opts);
  152. std::string s;
  153. sr.reset(sv);
  154. serialize_impl(s, sr);
  155. return s;
  156. }
  157. //----------------------------------------------------------
  158. //[example_operator_lt__lt_
  159. // Serialize a value into an output stream
  160. std::ostream&
  161. operator<<( std::ostream& os, value const& jv )
  162. {
  163. // Create a serializer
  164. serializer sr( get_stream_flags(os) );
  165. // Set the serializer up for our value
  166. sr.reset( &jv );
  167. // Loop until all output is produced.
  168. while( ! sr.done() )
  169. {
  170. // Use a local buffer to avoid allocation.
  171. char buf[ BOOST_JSON_STACK_BUFFER_SIZE ];
  172. // Fill our buffer with serialized characters and write it to the output stream.
  173. os << sr.read( buf );
  174. }
  175. return os;
  176. }
  177. //]
  178. static
  179. void
  180. to_ostream(
  181. std::ostream& os,
  182. serializer& sr)
  183. {
  184. while(! sr.done())
  185. {
  186. char buf[BOOST_JSON_STACK_BUFFER_SIZE];
  187. auto s = sr.read(buf);
  188. os.write(s.data(), s.size());
  189. }
  190. }
  191. std::ostream&
  192. operator<<(
  193. std::ostream& os,
  194. array const& arr)
  195. {
  196. serializer sr( get_stream_flags(os) );
  197. sr.reset(&arr);
  198. to_ostream(os, sr);
  199. return os;
  200. }
  201. std::ostream&
  202. operator<<(
  203. std::ostream& os,
  204. object const& obj)
  205. {
  206. serializer sr( get_stream_flags(os) );
  207. sr.reset(&obj);
  208. to_ostream(os, sr);
  209. return os;
  210. }
  211. std::ostream&
  212. operator<<(
  213. std::ostream& os,
  214. string const& str)
  215. {
  216. serializer sr( get_stream_flags(os) );
  217. sr.reset(&str);
  218. to_ostream(os, sr);
  219. return os;
  220. }
  221. std::ostream&
  222. operator<<( std::ostream& os, serialize_options const& opts )
  223. {
  224. os.iword(serialize_xalloc) = to_bitmask(opts);
  225. return os;
  226. }
  227. } // namespace json
  228. } // namespace boost
  229. #endif