-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjson.cpp
More file actions
177 lines (153 loc) · 3.98 KB
/
Copy pathjson.cpp
File metadata and controls
177 lines (153 loc) · 3.98 KB
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
//
// Copyright (c) 2026 Mohammad Nejati
//
// 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/cppalliance/burl
//
#include <boost/burl/json.hpp>
#include <boost/capy/buffers.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/stream_parser.hpp>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <system_error>
#include <utility>
namespace boost
{
namespace burl
{
namespace
{
class json_body
{
json::value value_;
public:
explicit json_body(json::value value)
: value_(std::move(value))
{
}
std::optional<std::string>
content_type() const
{
return "application/json";
}
std::optional<std::uint64_t>
content_length() const noexcept
{
// TODO: determine the length for small JSON values that fit in place.
// The serialized size is not known without serializing.
return std::nullopt;
}
capy::io_task<>
write(capy::any_buffer_sink& sink) const
{
json::serializer sr;
sr.reset(&value_);
while(!sr.done())
{
capy::mutable_buffer arr[2];
auto dst = sink.prepare(arr);
std::size_t n = 0;
for(capy::mutable_buffer b : dst)
{
if(sr.done())
break;
n += sr.read(static_cast<char*>(b.data()), b.size()).size();
}
if(auto [ec] = co_await sink.commit(n); ec)
co_return { ec };
}
co_return {};
}
};
} // namespace
any_request_body
tag_invoke(body_from_tag<json::value>, json::value value)
{
return json_body{ std::move(value) };
}
capy::io_task<json::value>
tag_invoke(body_to_tag<json::value>, response& resp)
{
auto source = resp.as_buffer_source();
json::stream_parser parser;
for(;;)
{
capy::const_buffer arr[2];
auto [ec, bufs] = co_await source.pull(arr);
if(ec)
{
if(ec == capy::error::eof)
{
parser.finish(ec);
if(ec)
co_return { ec, {} };
co_return { {}, parser.release() };
}
co_return { ec, {} };
}
std::size_t n = 0;
for(const auto& buf : bufs)
{
n += parser.write_some(
{ static_cast<const char*>(buf.data()), buf.size() }, ec);
if(ec)
co_return { ec, {} };
}
source.consume(n);
}
}
any_request_body
tag_invoke(body_from_tag<json::object>, json::object value)
{
return json_body{ std::move(value) };
}
capy::io_task<json::object>
tag_invoke(body_to_tag<json::object>, response& resp)
{
auto [ec, jv] = co_await tag_invoke(body_to_tag<json::value>{}, resp);
if(ec)
co_return { ec, {} };
auto r = jv.try_as_object();
if(r.has_error())
co_return { r.error(), {} };
co_return { {}, std::move(*r) };
}
any_request_body
tag_invoke(body_from_tag<json::array>, json::array value)
{
return json_body{ std::move(value) };
}
capy::io_task<json::array>
tag_invoke(body_to_tag<json::array>, response& resp)
{
auto [ec, jv] = co_await tag_invoke(body_to_tag<json::value>{}, resp);
if(ec)
co_return { ec, {} };
auto r = jv.try_as_array();
if(r.has_error())
co_return { r.error(), {} };
co_return { {}, std::move(*r) };
}
any_request_body
tag_invoke(body_from_tag<json::string>, json::string value)
{
return json_body{ std::move(value) };
}
capy::io_task<json::string>
tag_invoke(body_to_tag<json::string>, response& resp)
{
auto [ec, jv] = co_await tag_invoke(body_to_tag<json::value>{}, resp);
if(ec)
co_return { ec, {} };
auto r = jv.try_as_string();
if(r.has_error())
co_return { r.error(), {} };
co_return { {}, std::move(*r) };
}
} // namespace burl
} // namespace boost