Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:02

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/json
0008 //
0009 
0010 #ifndef BOOST_JSON_IMPL_SERIALIZE_IPP
0011 #define BOOST_JSON_IMPL_SERIALIZE_IPP
0012 
0013 #include <boost/json/serialize.hpp>
0014 #include <boost/json/serializer.hpp>
0015 #include <ostream>
0016 
0017 namespace boost {
0018 namespace json {
0019 
0020 namespace {
0021 
0022 int serialize_xalloc = std::ios::xalloc();
0023 
0024 enum class serialize_stream_flags : long
0025 {
0026     allow_infinity_and_nan = 1,
0027 };
0028 
0029 std::underlying_type<serialize_stream_flags>::type
0030 to_bitmask( serialize_options const& opts )
0031 {
0032     using E = serialize_stream_flags;
0033     using I = std::underlying_type<E>::type;
0034     return (opts.allow_infinity_and_nan
0035         ? static_cast<I>(E::allow_infinity_and_nan) : 0);
0036 }
0037 
0038 serialize_options
0039 get_stream_flags( std::ostream& os )
0040 {
0041     auto const flags = os.iword(serialize_xalloc);
0042 
0043     serialize_options opts;
0044     using E = serialize_stream_flags;
0045     using I = std::underlying_type<E>::type;
0046     opts.allow_infinity_and_nan =
0047         flags & static_cast<I>(E::allow_infinity_and_nan);
0048     return opts;
0049 }
0050 
0051 } // namespace
0052 
0053 static
0054 void
0055 serialize_impl(
0056     std::string& s,
0057     serializer& sr)
0058 {
0059     // serialize to a small buffer to avoid
0060     // the first few allocations in std::string
0061     char buf[BOOST_JSON_STACK_BUFFER_SIZE];
0062     string_view sv;
0063     sv = sr.read(buf);
0064     if(sr.done())
0065     {
0066         // fast path
0067         s.append(
0068             sv.data(), sv.size());
0069         return;
0070     }
0071     std::size_t len = sv.size();
0072     s.reserve(len * 2);
0073     s.resize(s.capacity());
0074     BOOST_ASSERT(
0075         s.size() >= len * 2);
0076     std::memcpy(&s[0],
0077         sv.data(), sv.size());
0078     auto const lim =
0079         s.max_size() / 2;
0080     for(;;)
0081     {
0082         sv = sr.read(
0083             &s[0] + len,
0084             s.size() - len);
0085         len += sv.size();
0086         if(sr.done())
0087             break;
0088         // growth factor 2x
0089         if(s.size() < lim)
0090             s.resize(s.size() * 2);
0091         else
0092             s.resize(2 * lim);
0093     }
0094     s.resize(len);
0095 }
0096 
0097 std::string
0098 serialize(
0099     value const& jv,
0100     serialize_options const& opts)
0101 {
0102     unsigned char buf[256];
0103     serializer sr(
0104         storage_ptr(),
0105         buf,
0106         sizeof(buf),
0107         opts);
0108     sr.reset(&jv);
0109     std::string s;
0110     serialize_impl(s, sr);
0111     return s;
0112 }
0113 
0114 std::string
0115 serialize(
0116     array const& arr,
0117     serialize_options const& opts)
0118 {
0119     unsigned char buf[256];
0120     serializer sr(
0121         storage_ptr(),
0122         buf,
0123         sizeof(buf),
0124         opts);
0125     std::string s;
0126     sr.reset(&arr);
0127     serialize_impl(s, sr);
0128     return s;
0129 }
0130 
0131 std::string
0132 serialize(
0133     object const& obj,
0134     serialize_options const& opts)
0135 {
0136     unsigned char buf[256];
0137     serializer sr(
0138         storage_ptr(),
0139         buf,
0140         sizeof(buf),
0141         opts);
0142     std::string s;
0143     sr.reset(&obj);
0144     serialize_impl(s, sr);
0145     return s;
0146 }
0147 
0148 std::string
0149 serialize(
0150     string const& str,
0151     serialize_options const& opts)
0152 {
0153     return serialize( str.subview(), opts );
0154 }
0155 
0156 // this is here for key_value_pair::key()
0157 std::string
0158 serialize(
0159     string_view sv,
0160     serialize_options const& opts)
0161 {
0162     unsigned char buf[256];
0163     serializer sr(
0164         storage_ptr(),
0165         buf,
0166         sizeof(buf),
0167         opts);
0168     std::string s;
0169     sr.reset(sv);
0170     serialize_impl(s, sr);
0171     return s;
0172 }
0173 
0174 //----------------------------------------------------------
0175 
0176 //[example_operator_lt__lt_
0177 // Serialize a value into an output stream
0178 
0179 std::ostream&
0180 operator<<( std::ostream& os, value const& jv )
0181 {
0182     // Create a serializer
0183     serializer sr( get_stream_flags(os) );
0184 
0185     // Set the serializer up for our value
0186     sr.reset( &jv );
0187 
0188     // Loop until all output is produced.
0189     while( ! sr.done() )
0190     {
0191         // Use a local buffer to avoid allocation.
0192         char buf[ BOOST_JSON_STACK_BUFFER_SIZE ];
0193 
0194         // Fill our buffer with serialized characters and write it to the output stream.
0195         os << sr.read( buf );
0196     }
0197 
0198     return os;
0199 }
0200 //]
0201 
0202 static
0203 void
0204 to_ostream(
0205     std::ostream& os,
0206     serializer& sr)
0207 {
0208     while(! sr.done())
0209     {
0210         char buf[BOOST_JSON_STACK_BUFFER_SIZE];
0211         auto s = sr.read(buf);
0212         os.write(s.data(), s.size());
0213     }
0214 }
0215 
0216 std::ostream&
0217 operator<<(
0218     std::ostream& os,
0219     array const& arr)
0220 {
0221     serializer sr( get_stream_flags(os) );
0222     sr.reset(&arr);
0223     to_ostream(os, sr);
0224     return os;
0225 }
0226 
0227 std::ostream&
0228 operator<<(
0229     std::ostream& os,
0230     object const& obj)
0231 {
0232     serializer sr( get_stream_flags(os) );
0233     sr.reset(&obj);
0234     to_ostream(os, sr);
0235     return os;
0236 }
0237 
0238 std::ostream&
0239 operator<<(
0240     std::ostream& os,
0241     string const& str)
0242 {
0243     serializer sr( get_stream_flags(os) );
0244     sr.reset(&str);
0245     to_ostream(os, sr);
0246     return os;
0247 }
0248 
0249 std::ostream&
0250 operator<<( std::ostream& os, serialize_options const& opts )
0251 {
0252     os.iword(serialize_xalloc) = to_bitmask(opts);
0253     return os;
0254 }
0255 
0256 } // namespace json
0257 } // namespace boost
0258 
0259 #endif