rippled
Loading...
Searching...
No Matches
json_body.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_RPC_JSON_BODY_H
21#define RIPPLE_RPC_JSON_BODY_H
22
23#include <xrpl/json/json_value.h>
24#include <xrpl/json/to_string.h>
25
26#include <boost/beast/core/multi_buffer.hpp>
27#include <boost/beast/http/message.hpp>
28
29namespace ripple {
30
33{
34 explicit json_body() = default;
35
37
38 class reader
39 {
40 using dynamic_buffer_type = boost::beast::multi_buffer;
41
43
44 public:
46 typename dynamic_buffer_type::const_buffers_type;
47
49
50 template <bool isRequest, class Fields>
51 explicit reader(
52 boost::beast::http::message<isRequest, json_body, Fields> const& m)
53 {
54 stream(m.body, [&](void const* data, std::size_t n) {
55 buffer_.commit(boost::asio::buffer_copy(
56 buffer_.prepare(n), boost::asio::buffer(data, n)));
57 });
58 }
59
60 void
61 init(boost::beast::error_code&) noexcept
62 {
63 }
64
65 // get() must return a boost::optional (not a std::optional) to meet
66 // requirements of a boost::beast::BodyReader.
67 boost::optional<std::pair<const_buffers_type, bool>>
68 get(boost::beast::error_code& ec)
69 {
70 return {{buffer_.data(), false}};
71 }
72
73 void
74 finish(boost::beast::error_code&)
75 {
76 }
77 };
78
79 class writer
80 {
82
83 public:
84 using const_buffers_type = boost::asio::const_buffer;
85
86 template <bool isRequest, class Fields>
87 explicit writer(
88 boost::beast::http::header<isRequest, Fields> const& fields,
89 value_type const& value)
90 : body_string_(to_string(value))
91 {
92 }
93
94 void
95 init(boost::beast::error_code& ec)
96 {
97 ec.assign(0, ec.category());
98 }
99
100 // get() must return a boost::optional (not a std::optional) to meet
101 // requirements of a boost::beast::BodyWriter.
102 boost::optional<std::pair<const_buffers_type, bool>>
103 get(boost::beast::error_code& ec)
104 {
105 ec.assign(0, ec.category());
106 return {
108 false}};
109 }
110 };
111};
112
113} // namespace ripple
114
115#endif
Represents a JSON value.
Definition json_value.h:149
reader(boost::beast::http::message< isRequest, json_body, Fields > const &m)
Definition json_body.h:51
dynamic_buffer_type buffer_
Definition json_body.h:42
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:68
void finish(boost::beast::error_code &)
Definition json_body.h:74
boost::beast::multi_buffer dynamic_buffer_type
Definition json_body.h:40
typename dynamic_buffer_type::const_buffers_type const_buffers_type
Definition json_body.h:46
void init(boost::beast::error_code &) noexcept
Definition json_body.h:61
void init(boost::beast::error_code &ec)
Definition json_body.h:95
writer(boost::beast::http::header< isRequest, Fields > const &fields, value_type const &value)
Definition json_body.h:87
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition json_body.h:103
boost::asio::const_buffer const_buffers_type
Definition json_body.h:84
T data(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
T size(T... args)
Body that holds JSON.
Definition json_body.h:33
json_body()=default