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
9#include <cstddef>
10#include <string>
11#include <type_traits>
12#include <utility>
13
14namespace xrpl {
15
20{
21 explicit JsonBody() = default;
22
24
25 class reader // NOLINT(readability-identifier-naming) -- Boost.Beast body concept name
26 {
27 using dynamic_buffer_type = boost::beast::multi_buffer;
28
30
31 public:
32 using const_buffers_type = dynamic_buffer_type::const_buffers_type;
33
35
36 template <bool IsRequest, class Fields>
37 explicit reader(boost::beast::http::message<IsRequest, JsonBody, Fields> const& m)
38 {
39 stream(m.body, [&](void const* data, std::size_t n) {
40 buffer_.commit(
41 boost::asio::buffer_copy(buffer_.prepare(n), boost::asio::buffer(data, n)));
42 });
43 }
44
45 void
46 init(boost::beast::error_code&) noexcept
47 {
48 }
49
50 // get() must return a boost::optional (not a std::optional) to meet
51 // requirements of a boost::beast::BodyReader.
52 boost::optional<std::pair<const_buffers_type, bool>>
53 get(boost::beast::error_code& ec)
54 {
55 return {{buffer_.data(), false}};
56 }
57
58 void
59 finish(boost::beast::error_code&)
60 {
61 }
62 };
63
64 class writer // NOLINT(readability-identifier-naming) -- Boost.Beast body concept name
65 {
67
68 public:
69 using const_buffers_type = boost::asio::const_buffer;
70
71 template <bool IsRequest, class Fields>
72 explicit writer(
73 boost::beast::http::header<IsRequest, Fields> const& fields,
74 value_type const& value)
75 : bodyString_(to_string(value))
76 {
77 }
78
79 static void
80 init(boost::beast::error_code& ec)
81 {
82 ec.assign(0, ec.category());
83 }
84
85 // get() must return a boost::optional (not a std::optional) to meet
86 // requirements of a boost::beast::BodyWriter.
87 boost::optional<std::pair<const_buffers_type, bool>>
88 get(boost::beast::error_code& ec)
89 {
90 ec.assign(0, ec.category());
91 return {{const_buffers_type{bodyString_.data(), bodyString_.size()}, false}};
92 }
93 };
94};
95
96} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
boost::beast::multi_buffer dynamic_buffer_type
Definition json_body.h:27
dynamic_buffer_type::const_buffers_type const_buffers_type
Definition json_body.h:32
std::false_type is_deferred
Definition json_body.h:34
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:53
void init(boost::beast::error_code &) noexcept
Definition json_body.h:46
dynamic_buffer_type buffer_
Definition json_body.h:29
void finish(boost::beast::error_code &)
Definition json_body.h:59
reader(boost::beast::http::message< IsRequest, JsonBody, Fields > const &m)
Definition json_body.h:37
static void init(boost::beast::error_code &ec)
Definition json_body.h:80
writer(boost::beast::http::header< IsRequest, Fields > const &fields, value_type const &value)
Definition json_body.h:72
boost::asio::const_buffer const_buffers_type
Definition json_body.h:69
std::string bodyString_
Definition json_body.h:66
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:88
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:651
JsonBody()=default
json::Value value_type
Definition json_body.h:23