xrpld
Loading...
Searching...
No Matches
json_body.h
1#pragma once
2
3#include <xrpl/json/json_value.h>
4#include <xrpl/json/to_string.h>
5
6#include <boost/beast/core/multi_buffer.hpp>
7#include <boost/beast/http/message.hpp>
8
9namespace xrpl {
10
13{
14 explicit JsonBody() = default;
15
17
18 class reader // NOLINT(readability-identifier-naming) -- Boost.Beast body concept name
19 {
20 using dynamic_buffer_type = boost::beast::multi_buffer;
21
23
24 public:
25 using const_buffers_type = dynamic_buffer_type::const_buffers_type;
26
28
29 template <bool IsRequest, class Fields>
30 explicit reader(boost::beast::http::message<IsRequest, JsonBody, Fields> const& m)
31 {
32 stream(m.body, [&](void const* data, std::size_t n) {
33 buffer_.commit(
34 boost::asio::buffer_copy(buffer_.prepare(n), boost::asio::buffer(data, n)));
35 });
36 }
37
38 void
39 init(boost::beast::error_code&) noexcept
40 {
41 }
42
43 // get() must return a boost::optional (not a std::optional) to meet
44 // requirements of a boost::beast::BodyReader.
45 boost::optional<std::pair<const_buffers_type, bool>>
46 get(boost::beast::error_code& ec)
47 {
48 return {{buffer_.data(), false}};
49 }
50
51 void
52 finish(boost::beast::error_code&)
53 {
54 }
55 };
56
57 class writer // NOLINT(readability-identifier-naming) -- Boost.Beast body concept name
58 {
60
61 public:
62 using const_buffers_type = boost::asio::const_buffer;
63
64 template <bool IsRequest, class Fields>
65 explicit writer(
66 boost::beast::http::header<IsRequest, Fields> const& fields,
67 value_type const& value)
68 : bodyString_(to_string(value))
69 {
70 }
71
72 static void
73 init(boost::beast::error_code& ec)
74 {
75 ec.assign(0, ec.category());
76 }
77
78 // get() must return a boost::optional (not a std::optional) to meet
79 // requirements of a boost::beast::BodyWriter.
80 boost::optional<std::pair<const_buffers_type, bool>>
81 get(boost::beast::error_code& ec)
82 {
83 ec.assign(0, ec.category());
84 return {{const_buffers_type{bodyString_.data(), bodyString_.size()}, false}};
85 }
86 };
87};
88
89} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
boost::beast::multi_buffer dynamic_buffer_type
Definition json_body.h:20
dynamic_buffer_type::const_buffers_type const_buffers_type
Definition json_body.h:25
std::false_type is_deferred
Definition json_body.h:27
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:46
void init(boost::beast::error_code &) noexcept
Definition json_body.h:39
dynamic_buffer_type buffer_
Definition json_body.h:22
void finish(boost::beast::error_code &)
Definition json_body.h:52
reader(boost::beast::http::message< IsRequest, JsonBody, Fields > const &m)
Definition json_body.h:30
static void init(boost::beast::error_code &ec)
Definition json_body.h:73
writer(boost::beast::http::header< IsRequest, Fields > const &fields, value_type const &value)
Definition json_body.h:65
boost::asio::const_buffer const_buffers_type
Definition json_body.h:62
std::string bodyString_
Definition json_body.h:59
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:81
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
JsonBody()=default
json::Value value_type
Definition json_body.h:16