rippled
Loading...
Searching...
No Matches
json_body.h
1#ifndef XRPL_RPC_JSON_BODY_H
2#define XRPL_RPC_JSON_BODY_H
3
4#include <xrpl/json/json_value.h>
5#include <xrpl/json/to_string.h>
6
7#include <boost/beast/core/multi_buffer.hpp>
8#include <boost/beast/http/message.hpp>
9
10namespace ripple {
11
14{
15 explicit json_body() = default;
16
18
19 class reader
20 {
21 using dynamic_buffer_type = boost::beast::multi_buffer;
22
24
25 public:
27 typename dynamic_buffer_type::const_buffers_type;
28
30
31 template <bool isRequest, class Fields>
32 explicit reader(
33 boost::beast::http::message<isRequest, json_body, Fields> const& m)
34 {
35 stream(m.body, [&](void const* data, std::size_t n) {
36 buffer_.commit(boost::asio::buffer_copy(
37 buffer_.prepare(n), boost::asio::buffer(data, n)));
38 });
39 }
40
41 void
42 init(boost::beast::error_code&) noexcept
43 {
44 }
45
46 // get() must return a boost::optional (not a std::optional) to meet
47 // requirements of a boost::beast::BodyReader.
48 boost::optional<std::pair<const_buffers_type, bool>>
49 get(boost::beast::error_code& ec)
50 {
51 return {{buffer_.data(), false}};
52 }
53
54 void
55 finish(boost::beast::error_code&)
56 {
57 }
58 };
59
60 class writer
61 {
63
64 public:
65 using const_buffers_type = boost::asio::const_buffer;
66
67 template <bool isRequest, class Fields>
68 explicit writer(
69 boost::beast::http::header<isRequest, Fields> const& fields,
70 value_type const& value)
71 : body_string_(to_string(value))
72 {
73 }
74
75 void
76 init(boost::beast::error_code& ec)
77 {
78 ec.assign(0, ec.category());
79 }
80
81 // get() must return a boost::optional (not a std::optional) to meet
82 // requirements of a boost::beast::BodyWriter.
83 boost::optional<std::pair<const_buffers_type, bool>>
84 get(boost::beast::error_code& ec)
85 {
86 ec.assign(0, ec.category());
87 return {
89 false}};
90 }
91 };
92};
93
94} // namespace ripple
95
96#endif
Represents a JSON value.
Definition json_value.h:130
reader(boost::beast::http::message< isRequest, json_body, Fields > const &m)
Definition json_body.h:32
dynamic_buffer_type buffer_
Definition json_body.h:23
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:49
void finish(boost::beast::error_code &)
Definition json_body.h:55
boost::beast::multi_buffer dynamic_buffer_type
Definition json_body.h:21
typename dynamic_buffer_type::const_buffers_type const_buffers_type
Definition json_body.h:27
void init(boost::beast::error_code &) noexcept
Definition json_body.h:42
void init(boost::beast::error_code &ec)
Definition json_body.h:76
writer(boost::beast::http::header< isRequest, Fields > const &fields, value_type const &value)
Definition json_body.h:68
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:84
boost::asio::const_buffer const_buffers_type
Definition json_body.h:65
T data(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
T size(T... args)
Body that holds JSON.
Definition json_body.h:14
json_body()=default