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(
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
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 : body_string_(to_string(value))
69 {
70 }
71
72 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());
85 }
86 };
87};
88
89} // 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:46
dynamic_buffer_type buffer_
Definition json_body.h:22
void init(boost::beast::error_code &) noexcept
Definition json_body.h:39
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:52
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:65
void init(boost::beast::error_code &ec)
Definition json_body.h:73
std::string body_string_
Definition json_body.h:59
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:81
boost::asio::const_buffer const_buffers_type
Definition json_body.h:62
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:602
T size(T... args)
Body that holds JSON.
Definition json_body.h:13
json_body()=default