Monero
json_object.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <boost/utility/string_ref.hpp>
32 #include <cstring>
33 #include <rapidjson/document.h>
34 #include <rapidjson/writer.h>
35 #include <vector>
36 
37 #include "byte_stream.h"
41 #include "common/sfinae_helpers.h"
42 #include "hex.h"
43 #include "span.h"
44 
45 #define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
46  do \
47  { \
48  if (!val.HasMember(key)) \
49  { \
50  throw cryptonote::json::MISSING_KEY(key); \
51  } \
52  } while (0);
53 
54 #define INSERT_INTO_JSON_OBJECT(dest, key, source) \
55  dest.Key(#key, sizeof(#key) - 1); \
56  cryptonote::json::toJsonValue(dest, source);
57 
58 #define GET_FROM_JSON_OBJECT(source, dst, key) \
59  OBJECT_HAS_MEMBER_OR_THROW(source, #key) \
60  decltype(dst) dstVal##key; \
61  cryptonote::json::fromJsonValue(source[#key], dstVal##key); \
62  dst = dstVal##key;
63 
64 namespace cryptonote
65 {
66 
67 namespace json
68 {
69 
70 struct JSON_ERROR : public std::exception
71 {
72  protected:
73  JSON_ERROR() { }
75 
76  public:
77  virtual ~JSON_ERROR() { }
78 
79  const char* what() const throw()
80  {
81  return m.c_str();
82  }
83 };
84 
85 struct MISSING_KEY : public JSON_ERROR
86 {
87  MISSING_KEY(const char* key)
88  {
89  m = std::string("Key \"") + key + "\" missing from object.";
90  }
91 };
92 
93 struct WRONG_TYPE : public JSON_ERROR
94 {
95  WRONG_TYPE(const char* type)
96  {
97  m = std::string("Json value has incorrect type, expected: ") + type;
98  }
99 };
100 
101 struct BAD_INPUT : public JSON_ERROR
102 {
104  {
105  m = "An item failed to convert from json object to native object";
106  }
107 };
108 
109 struct PARSE_FAIL : public JSON_ERROR
110 {
112  {
113  m = "Failed to parse the json request";
114  }
115 };
116 
117 template<typename Type>
118 inline constexpr bool is_to_hex()
119 {
120  return std::is_pod<Type>() && !std::is_integral<Type>();
121 }
122 
124 
125 // POD to json key
126 template <class Type>
127 inline typename std::enable_if<is_to_hex<Type>()>::type toJsonKey(rapidjson::Writer<epee::byte_stream>& dest, const Type& pod)
128 {
129  const auto hex = epee::to_hex::array(pod);
130  dest.Key(hex.data(), hex.size());
131 }
132 
133 // POD to json value
134 template <class Type>
135 inline typename std::enable_if<is_to_hex<Type>()>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Type& pod)
136 {
137  const auto hex = epee::to_hex::array(pod);
138  dest.String(hex.data(), hex.size());
139 }
140 
141 template <class Type>
142 inline typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t)
143 {
144  static_assert(std::is_standard_layout<Type>(), "expected standard layout type");
146 }
147 
148 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rapidjson::Value& src);
149 
150 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, boost::string_ref i);
151 inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::string& i)
152 {
153  toJsonValue(dest, boost::string_ref{i});
154 }
155 void fromJsonValue(const rapidjson::Value& val, std::string& str);
156 
157 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::vector<std::uint8_t>&);
158 void fromJsonValue(const rapidjson::Value& src, std::vector<std::uint8_t>& i);
159 
160 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, bool i);
161 void fromJsonValue(const rapidjson::Value& val, bool& b);
162 
163 // integers overloads for toJsonValue are not needed for standard promotions
164 
165 void fromJsonValue(const rapidjson::Value& val, unsigned char& i);
166 
167 void fromJsonValue(const rapidjson::Value& val, signed char& i);
168 
169 void fromJsonValue(const rapidjson::Value& val, char& i);
170 
171 void fromJsonValue(const rapidjson::Value& val, unsigned short& i);
172 
173 void fromJsonValue(const rapidjson::Value& val, short& i);
174 
175 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned i);
176 void fromJsonValue(const rapidjson::Value& val, unsigned& i);
177 
178 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const int);
179 void fromJsonValue(const rapidjson::Value& val, int& i);
180 
181 
182 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned long long i);
183 void fromJsonValue(const rapidjson::Value& val, unsigned long long& i);
184 
185 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const long long i);
186 void fromJsonValue(const rapidjson::Value& val, long long& i);
187 
188 inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned long i) {
189  toJsonValue(dest, static_cast<unsigned long long>(i));
190 }
191 void fromJsonValue(const rapidjson::Value& val, unsigned long& i);
192 
193 inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const long i) {
194  toJsonValue(dest, static_cast<long long>(i));
195 }
196 void fromJsonValue(const rapidjson::Value& val, long& i);
197 
198 // end integers
199 
200 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::transaction& tx);
202 
203 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::block& b);
205 
206 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_v& txin);
207 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin);
208 
209 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_gen& txin);
210 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin);
211 
212 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_script& txin);
214 
215 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_scripthash& txin);
217 
218 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_key& txin);
220 
221 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_target_v& txout);
223 
224 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_script& txout);
226 
227 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_scripthash& txout);
229 
230 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_key& txout);
232 
233 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_tagged_key& txout);
235 
236 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_out& txout);
237 void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout);
238 
239 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::connection_info& info);
241 
242 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_blob_entry& tx);
244 
245 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::block_complete_entry& blk);
247 
248 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::block_with_transactions& blk);
250 
251 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::transaction_info& tx_info);
253 
254 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_key_and_amount_index& out);
256 
257 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::amount_with_random_outputs& out);
259 
260 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::peer& peer);
262 
263 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::tx_in_pool& tx);
265 
266 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::hard_fork_info& info);
268 
269 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_amount_count& out);
271 
272 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_amount_and_index& out);
274 
275 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_key_mask_unlocked& out);
277 
278 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::error& err);
280 
281 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::BlockHeaderResponse& response);
283 
284 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::rctSig& sig, bool prune);
285 void fromJsonValue(const rapidjson::Value& val, rct::rctSig& sig);
286 
287 void fromJsonValue(const rapidjson::Value& val, rct::ctkey& key);
288 
289 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::ecdhTuple& tuple);
290 void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple);
291 
292 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::rangeSig& sig);
293 void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig);
294 
295 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::Bulletproof& p);
297 
298 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::BulletproofPlus& p);
300 
301 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::boroSig& sig);
302 void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig);
303 
304 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::mgSig& sig);
305 void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig);
306 
307 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::clsag& sig);
308 void fromJsonValue(const rapidjson::Value& val, rct::clsag& sig);
309 
310 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::DaemonInfo& info);
312 
313 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_distribution& dist);
315 
316 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_block_template_backlog_entry& entry);
318 
319 template <typename Map>
320 typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Map& map);
321 
322 template <typename Map>
324 
325 template <typename Vec>
326 typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Vec &vec);
327 
328 template <typename Vec>
330 
331 
332 // ideally would like to have the below functions in the .cpp file, but
333 // unfortunately because of how templates work they have to be here.
334 
335 template <typename Map>
336 inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Map& map)
337 {
338  using key_type = typename Map::key_type;
339  static_assert(std::is_same<std::string, key_type>() || is_to_hex<key_type>(), "invalid map key type");
340 
341  dest.StartObject();
342  for (const auto& i : map)
343  {
344  toJsonKey(dest, i.first);
345  toJsonValue(dest, i.second);
346  }
347  dest.EndObject();
348 }
349 
350 template <typename Map>
352 {
353  if (!val.IsObject())
354  {
355  throw WRONG_TYPE("json object");
356  }
357 
358  auto itr = val.MemberBegin();
359 
360  map.clear();
361  while (itr != val.MemberEnd())
362  {
363  typename Map::key_type k;
364  typename Map::mapped_type m;
365  fromJsonValue(itr->name, k);
366  fromJsonValue(itr->value, m);
367  map.emplace(k, m);
368  ++itr;
369  }
370 }
371 
372 template <typename Vec>
373 inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Vec &vec)
374 {
375  using value_type = typename Vec::value_type;
376  static_assert(!std::is_same<value_type, char>::value, "encoding an array of chars is faster as hex");
377  static_assert(!std::is_same<value_type, unsigned char>::value, "encoding an array of unsigned char is faster as hex");
378 
379  dest.StartArray();
380  for (auto t : vec)
381  toJsonValue(dest, t);
382  dest.EndArray();
383 }
384 
385 namespace traits
386 {
387  template<typename T>
388  void reserve(const T&, std::size_t)
389  {}
390 
391  template<typename T>
392  void reserve(std::vector<T>& vec, const std::size_t count)
393  {
394  vec.reserve(count);
395  }
396 }
397 
398 template <typename Vec>
400 {
401  using value_type = typename Vec::value_type;
402  static_assert(!std::is_same<value_type, char>::value, "encoding a vector of chars is faster as hex");
403  static_assert(!std::is_same<value_type, unsigned char>::value, "encoding a vector of unsigned char is faster as hex");
404 
405  if (!val.IsArray())
406  {
407  throw WRONG_TYPE("json array");
408  }
409 
410  vec.clear();
411  traits::reserve(vec, val.Size());
412  for (rapidjson::SizeType i=0; i < val.Size(); i++)
413  {
414  vec.emplace_back();
415  fromJsonValue(val[i], vec.back());
416  }
417 }
418 
419 } // namespace json
420 
421 } // namespace cryptonote
Definition: cryptonote_basic.h:86
Definition: json_object.h:85
Definition: core_rpc_server_commands_defs.h:1338
PARSE_FAIL()
Definition: json_object.h:111
const uint32_t T[512]
Definition: groestl_tables.h:36
Definition: cryptonote_basic.h:123
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
int * count
Definition: gmock_stress_test.cc:176
void reserve(const T &, std::size_t)
Definition: json_object.h:388
int i
Definition: pymoduletest.py:23
JSON_ERROR()
Definition: json_object.h:73
Definition: message_data_structs.h:71
Definition: cryptonote_protocol_defs.h:132
::std::string string
Definition: gtest-port.h:1097
CXA_THROW_INFO_T void(* dest)(void *))
Definition: stack_trace.cpp:91
Definition: cryptonote_basic.h:474
Definition: rctTypes.h:97
MISSING_KEY(const char *key)
Definition: json_object.h:87
boost::variant< txout_to_script, txout_to_scripthash, txout_to_key, txout_to_tagged_key > txout_target_v
Definition: cryptonote_basic.h:154
t
Definition: console.py:33
const char * key
Definition: hmac_keccak.cpp:40
int type
Definition: superscalar.cpp:50
static std::array< char, N *2 > array(const std::array< std::uint8_t, N > &src) noexcept
Definition: hex.h:52
Definition: cryptonote_protocol_defs.h:47
void toJsonValue(rapidjson::Writer< epee::byte_stream > &dest, const rapidjson::Value &src)
Definition: json_object.cpp:132
span< std::uint8_t > as_mut_byte_span(T &src) noexcept
Definition: span.h:172
Definition: message_data_structs.h:175
Definition: cryptonote_basic.h:78
Non-owning sequence of data. Does not deep copy.
Definition: span.h:54
void read_hex(const rapidjson::Value &val, epee::span< std::uint8_t > dest)
Definition: json_object.cpp:119
Definition: cryptonote_basic.h:61
Definition: rctTypes.h:140
Definition: message_data_structs.h:46
Definition: cryptonote_basic.h:101
Definition: message_data_structs.h:160
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:44
Definition: json_object.h:93
Definition: cryptonote_basic.h:110
Definition: message_data_structs.h:130
Definition: cryptonote_basic.h:72
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2116
Definition: rctTypes.h:155
Definition: rctTypes.h:249
Definition: rctTypes.h:181
std::enable_if< is_to_hex< Type >)>::type toJsonKey(rapidjson::Writer< epee::byte_stream > &dest, const Type &pod)
Definition: json_object.h:127
Definition: cryptonote_basic.h:138
Definition: message_data_structs.h:77
std::string m
Definition: json_object.h:74
Definition: rctTypes.h:613
Definition: json_object.h:70
Definition: cryptonote_protocol_defs.h:121
Definition: message_data_structs.h:117
static MDB_envinfo info
Definition: mdb_load.c:37
Definition: rctTypes.h:202
Definition: message_data_structs.h:56
Definition: rctTypes.h:168
Definition: message_data_structs.h:109
BAD_INPUT()
Definition: json_object.h:103
constexpr bool is_to_hex()
Definition: json_object.h:118
Definition: rctTypes.h:212
Definition: message_data_structs.h:206
const char *const str
Definition: portlistingparse.c:23
void fromJsonValue(const rapidjson::Value &val, std::string &str)
Definition: json_object.cpp:142
Definition: core_rpc_server_commands_defs.h:1522
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
WRONG_TYPE(const char *type)
Definition: json_object.h:95
static void prune(MDB_env *env0, MDB_env *env1)
Definition: blockchain_prune.cpp:249
cryptonote::transaction tx
Definition: transaction.cpp:40
p
Definition: pymoduletest.py:75
boost::variant< txin_gen, txin_to_script, txin_to_scripthash, txin_to_key > txin_v
Definition: cryptonote_basic.h:152
std::string hex(difficulty_type v)
Definition: difficulty.cpp:242
Definition: message_data_structs.h:143
const char * what() const
Definition: json_object.h:79
virtual ~JSON_ERROR()
Definition: json_object.h:77
Definition: cryptonote_basic.h:157
Definition: message_data_structs.h:88
Definition: cryptonote_tx_utils.h:112
Type
Type of JSON value.
Definition: rapidjson.h:623
Definition: message_data_structs.h:63
error
Tracks LMDB error codes.
Definition: error.h:44
Definition: message_data_structs.h:123
Definition: cryptonote_basic.h:204
rapidjson::Document json
Definition: transport.cpp:49
cryptonote::block b
Definition: block.cpp:40
Definition: json_object.h:109
Definition: json_object.h:101