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