liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/json
//
 
#ifndef BOOST_JSON_IMPL_SERIALIZE_IPP
#define BOOST_JSON_IMPL_SERIALIZE_IPP
 
#include <boost/json/serialize.hpp>
#include <boost/json/serializer.hpp>
#include <ostream>
 
BOOST_JSON_NS_BEGIN
 
static
void
serialize_impl(
    std::string& s,
    serializer& sr)
{
    // serialize to a small buffer to avoid
    // the first few allocations in std::string
    char buf[BOOST_JSON_STACK_BUFFER_SIZE];
    string_view sv;
    sv = sr.read(buf);
    if(sr.done())
    {
        // fast path
        s.append(
            sv.data(), sv.size());
        return;
    }
    std::size_t len = sv.size();
    s.reserve(len * 2);
    s.resize(s.capacity());
    BOOST_ASSERT(
        s.size() >= len * 2);
    std::memcpy(&s[0],
        sv.data(), sv.size());
    for(;;)
    {
        sv = sr.read(
            &s[0] + len,
            s.size() - len);
        len += sv.size();
        if(sr.done())
            break;
        s.resize(
            s.capacity() + 1);
    }
    s.resize(len);
}
 
std::string
serialize(
    value const& jv)
{
    std::string s;
    serializer sr;
    sr.reset(&jv);
    serialize_impl(s, sr);
    return s;
}
 
std::string
serialize(
    array const& arr)
{
    std::string s;
    serializer sr;
    sr.reset(&arr);
    serialize_impl(s, sr);
    return s;
}
 
std::string
serialize(
    object const& obj)
{
    std::string s;
    serializer sr;
    sr.reset(&obj);
    serialize_impl(s, sr);
    return s;
}
 
std::string
serialize(
    string const& str)
{
    std::string s;
    serializer sr;
    sr.reset(&str);
    serialize_impl(s, sr);
    return s;
}
 
// this is here for key_value_pair::key()
std::string
serialize(
    string_view sv)
{
    std::string s;
    serializer sr;
    sr.reset(sv);
    serialize_impl(s, sr);
    return s;
}
 
//----------------------------------------------------------
 
//[example_operator_lt__lt_
// Serialize a value into an output stream
 
std::ostream&
operator<<( std::ostream& os, value const& jv )
{
    // Create a serializer
    serializer sr;
 
    // Set the serializer up for our value
    sr.reset( &jv );
 
    // Loop until all output is produced.
    while( ! sr.done() )
    {
        // Use a local buffer to avoid allocation.
        char buf[ BOOST_JSON_STACK_BUFFER_SIZE ];
 
        // Fill our buffer with serialized characters and write it to the output stream.
        os << sr.read( buf );
    }
 
    return os;
}
//]
 
static
void
to_ostream(
    std::ostream& os,
    serializer& sr)
{
    while(! sr.done())
    {
        char buf[BOOST_JSON_STACK_BUFFER_SIZE];
        auto s = sr.read(buf);
        os.write(s.data(), s.size());
    }
}
 
std::ostream&
operator<<(
    std::ostream& os,
    array const& arr)
{
    serializer sr;
    sr.reset(&arr);
    to_ostream(os, sr);
    return os;
}
 
std::ostream&
operator<<(
    std::ostream& os,
    object const& obj)
{
    serializer sr;
    sr.reset(&obj);
    to_ostream(os, sr);
    return os;
}
 
std::ostream&
operator<<(
    std::ostream& os,
    string const& str)
{
    serializer sr;
    sr.reset(&str);
    to_ostream(os, sr);
    return os;
}
 
BOOST_JSON_NS_END
 
#endif