rippled
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 json_body() = default;
15
17
18 class reader
19 {
20 using dynamic_buffer_type = boost::beast::multi_buffer;
21
23
24 public:
25 using const_buffers_type = typename dynamic_buffer_type::const_buffers_type;
26
28
29 template <bool isRequest, class Fields>
30 explicit reader(boost::beast::http::message<isRequest, json_body, Fields> const& m)
31 {
32 stream(m.body, [&](void const* data, std::size_t n) {
33 buffer_.commit(boost::asio::buffer_copy(buffer_.prepare(n), boost::asio::buffer(data, n)));
34 });
35 }
36
37 void
38 init(boost::beast::error_code&) noexcept
39 {
40 }
41
42 // get() must return a boost::optional (not a std::optional) to meet
43 // requirements of a boost::beast::BodyReader.
44 boost::optional<std::pair<const_buffers_type, bool>>
45 get(boost::beast::error_code& ec)
46 {
47 return {{buffer_.data(), false}};
48 }
49
50 void
51 finish(boost::beast::error_code&)
52 {
53 }
54 };
55
56 class writer
57 {
59
60 public:
61 using const_buffers_type = boost::asio::const_buffer;
62
63 template <bool isRequest, class Fields>
64 explicit writer(boost::beast::http::header<isRequest, Fields> const& fields, value_type const& value)
65 : body_string_(to_string(value))
66 {
67 }
68
69 void
70 init(boost::beast::error_code& ec)
71 {
72 ec.assign(0, ec.category());
73 }
74
75 // get() must return a boost::optional (not a std::optional) to meet
76 // requirements of a boost::beast::BodyWriter.
77 boost::optional<std::pair<const_buffers_type, bool>>
78 get(boost::beast::error_code& ec)
79 {
80 ec.assign(0, ec.category());
82 }
83 };
84};
85
86} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:45
dynamic_buffer_type buffer_
Definition json_body.h:22
void init(boost::beast::error_code &) noexcept
Definition json_body.h:38
reader(boost::beast::http::message< isRequest, json_body, Fields > const &m)
Definition json_body.h:30
void finish(boost::beast::error_code &)
Definition json_body.h:51
boost::beast::multi_buffer dynamic_buffer_type
Definition json_body.h:20
typename dynamic_buffer_type::const_buffers_type const_buffers_type
Definition json_body.h:25
writer(boost::beast::http::header< isRequest, Fields > const &fields, value_type const &value)
Definition json_body.h:64
void init(boost::beast::error_code &ec)
Definition json_body.h:70
std::string body_string_
Definition json_body.h:58
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:78
boost::asio::const_buffer const_buffers_type
Definition json_body.h:61
T data(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
T size(T... args)
Body that holds JSON.
Definition json_body.h:13
json_body()=default