serialize.ipp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. BOOST_JSON_NS_BEGIN
  15. static
  16. void
  17. serialize_impl(
  18. std::string& s,
  19. serializer& sr)
  20. {
  21. // serialize to a small buffer to avoid
  22. // the first few allocations in std::string
  23. char buf[BOOST_JSON_STACK_BUFFER_SIZE];
  24. string_view sv;
  25. sv = sr.read(buf);
  26. if(sr.done())
  27. {
  28. // fast path
  29. s.append(
  30. sv.data(), sv.size());
  31. return;
  32. }
  33. std::size_t len = sv.size();
  34. s.reserve(len * 2);
  35. s.resize(s.capacity());
  36. BOOST_ASSERT(
  37. s.size() >= len * 2);
  38. std::memcpy(&s[0],
  39. sv.data(), sv.size());
  40. for(;;)
  41. {
  42. sv = sr.read(
  43. &s[0] + len,
  44. s.size() - len);
  45. len += sv.size();
  46. if(sr.done())
  47. break;
  48. s.resize(
  49. s.capacity() + 1);
  50. }
  51. s.resize(len);
  52. }
  53. std::string
  54. serialize(
  55. value const& jv)
  56. {
  57. std::string s;
  58. serializer sr;
  59. sr.reset(&jv);
  60. serialize_impl(s, sr);
  61. return s;
  62. }
  63. std::string
  64. serialize(
  65. array const& arr)
  66. {
  67. std::string s;
  68. serializer sr;
  69. sr.reset(&arr);
  70. serialize_impl(s, sr);
  71. return s;
  72. }
  73. std::string
  74. serialize(
  75. object const& obj)
  76. {
  77. std::string s;
  78. serializer sr;
  79. sr.reset(&obj);
  80. serialize_impl(s, sr);
  81. return s;
  82. }
  83. std::string
  84. serialize(
  85. string const& str)
  86. {
  87. std::string s;
  88. serializer sr;
  89. sr.reset(&str);
  90. serialize_impl(s, sr);
  91. return s;
  92. }
  93. // this is here for key_value_pair::key()
  94. std::string
  95. serialize(
  96. string_view sv)
  97. {
  98. std::string s;
  99. serializer sr;
  100. sr.reset(sv);
  101. serialize_impl(s, sr);
  102. return s;
  103. }
  104. //----------------------------------------------------------
  105. //[example_operator_lt__lt_
  106. // Serialize a value into an output stream
  107. std::ostream&
  108. operator<<( std::ostream& os, value const& jv )
  109. {
  110. // Create a serializer
  111. serializer sr;
  112. // Set the serializer up for our value
  113. sr.reset( &jv );
  114. // Loop until all output is produced.
  115. while( ! sr.done() )
  116. {
  117. // Use a local buffer to avoid allocation.
  118. char buf[ BOOST_JSON_STACK_BUFFER_SIZE ];
  119. // Fill our buffer with serialized characters and write it to the output stream.
  120. os << sr.read( buf );
  121. }
  122. return os;
  123. }
  124. //]
  125. static
  126. void
  127. to_ostream(
  128. std::ostream& os,
  129. serializer& sr)
  130. {
  131. while(! sr.done())
  132. {
  133. char buf[BOOST_JSON_STACK_BUFFER_SIZE];
  134. auto s = sr.read(buf);
  135. os.write(s.data(), s.size());
  136. }
  137. }
  138. std::ostream&
  139. operator<<(
  140. std::ostream& os,
  141. array const& arr)
  142. {
  143. serializer sr;
  144. sr.reset(&arr);
  145. to_ostream(os, sr);
  146. return os;
  147. }
  148. std::ostream&
  149. operator<<(
  150. std::ostream& os,
  151. object const& obj)
  152. {
  153. serializer sr;
  154. sr.reset(&obj);
  155. to_ostream(os, sr);
  156. return os;
  157. }
  158. std::ostream&
  159. operator<<(
  160. std::ostream& os,
  161. string const& str)
  162. {
  163. serializer sr;
  164. sr.reset(&str);
  165. to_ostream(os, sr);
  166. return os;
  167. }
  168. BOOST_JSON_NS_END
  169. #endif