Electroneum
json_object.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2019, 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 "string_tools.h"
32 #include "rapidjson/document.h"
36 #include "common/sfinae_helpers.h"
37 
38 #define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
39  do \
40  { \
41  if (!val.HasMember(key)) \
42  { \
43  throw cryptonote::json::MISSING_KEY(key); \
44  } \
45  } while (0);
46 
47 #define INSERT_INTO_JSON_OBJECT(jsonVal, doc, key, source) \
48  rapidjson::Value key##Val; \
49  cryptonote::json::toJsonValue(doc, source, key##Val); \
50  jsonVal.AddMember(#key, key##Val, doc.GetAllocator());
51 
52 #define GET_FROM_JSON_OBJECT(source, dst, key) \
53  OBJECT_HAS_MEMBER_OR_THROW(source, #key) \
54  decltype(dst) dstVal##key; \
55  cryptonote::json::fromJsonValue(source[#key], dstVal##key); \
56  dst = dstVal##key;
57 
58 namespace cryptonote
59 {
60 
61 namespace json
62 {
63 
64 struct JSON_ERROR : public std::exception
65 {
66  protected:
67  JSON_ERROR() { }
69 
70  public:
71  virtual ~JSON_ERROR() { }
72 
73  const char* what() const throw()
74  {
75  return m.c_str();
76  }
77 };
78 
79 struct MISSING_KEY : public JSON_ERROR
80 {
81  MISSING_KEY(const char* key)
82  {
83  m = std::string("Key \"") + key + "\" missing from object.";
84  }
85 };
86 
87 struct WRONG_TYPE : public JSON_ERROR
88 {
89  WRONG_TYPE(const char* type)
90  {
91  m = std::string("Json value has incorrect type, expected: ") + type;
92  }
93 };
94 
95 struct BAD_INPUT : public JSON_ERROR
96 {
98  {
99  m = "An item failed to convert from json object to native object";
100  }
101 };
102 
103 struct PARSE_FAIL : public JSON_ERROR
104 {
106  {
107  m = "Failed to parse the json request";
108  }
109 };
110 
111 template<typename Type>
112 inline constexpr bool is_to_hex()
113 {
114  return std::is_pod<Type>() && !std::is_integral<Type>();
115 }
116 
117 
118 // POD to json value
119 template <class Type>
120 typename std::enable_if<is_to_hex<Type>()>::type toJsonValue(rapidjson::Document& doc, const Type& pod, rapidjson::Value& value)
121 {
122  value = rapidjson::Value(epee::string_tools::pod_to_hex(pod).c_str(), doc.GetAllocator());
123 }
124 
125 template <class Type>
126 typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t)
127 {
128  if (!val.IsString())
129  {
130  throw WRONG_TYPE("string");
131  }
132 
133  //TODO: handle failure to convert hex string to POD type
134  bool success = epee::string_tools::hex_to_pod(val.GetString(), t);
135 
136  if (!success)
137  {
138  throw BAD_INPUT();
139  }
140 }
141 
143 void fromJsonValue(const rapidjson::Value& val, std::string& str);
144 
145 void toJsonValue(rapidjson::Document& doc, bool i, rapidjson::Value& val);
146 void fromJsonValue(const rapidjson::Value& val, bool& b);
147 
148 // integers overloads for toJsonValue are not needed for standard promotions
149 
150 void fromJsonValue(const rapidjson::Value& val, unsigned char& i);
151 
152 void fromJsonValue(const rapidjson::Value& val, signed char& i);
153 
154 void fromJsonValue(const rapidjson::Value& val, char& i);
155 
156 void fromJsonValue(const rapidjson::Value& val, unsigned short& i);
157 
158 void fromJsonValue(const rapidjson::Value& val, short& i);
159 
160 void toJsonValue(rapidjson::Document& doc, const unsigned i, rapidjson::Value& val);
161 void fromJsonValue(const rapidjson::Value& val, unsigned& i);
162 
163 void toJsonValue(rapidjson::Document& doc, const int, rapidjson::Value& val);
164 void fromJsonValue(const rapidjson::Value& val, int& i);
165 
166 
167 void toJsonValue(rapidjson::Document& doc, const unsigned long long i, rapidjson::Value& val);
168 void fromJsonValue(const rapidjson::Value& val, unsigned long long& i);
169 
170 void toJsonValue(rapidjson::Document& doc, const long long i, rapidjson::Value& val);
171 void fromJsonValue(const rapidjson::Value& val, long long& i);
172 
173 inline void toJsonValue(rapidjson::Document& doc, const unsigned long i, rapidjson::Value& val) {
174  toJsonValue(doc, static_cast<unsigned long long>(i), val);
175 }
176 void fromJsonValue(const rapidjson::Value& val, unsigned long& i);
177 
178 inline void toJsonValue(rapidjson::Document& doc, const long i, rapidjson::Value& val) {
179  toJsonValue(doc, static_cast<long long>(i), val);
180 }
181 void fromJsonValue(const rapidjson::Value& val, long& i);
182 
183 // end integers
184 
187 
190 
192 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin);
193 
195 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin);
196 
199 
202 
205 
208 
211 
214 
217 
220 
223 
225 void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout);
226 
229 
232 
235 
238 
241 
244 
247 
250 
253 
256 
259 
262 
265 
268 
270 void fromJsonValue(const rapidjson::Value& i, rct::rctSig& sig);
271 
272 void toJsonValue(rapidjson::Document& doc, const rct::ecdhTuple& tuple, rapidjson::Value& val);
273 void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple);
274 
276 void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig);
277 
279 void fromJsonValue(const rapidjson::Value& val, rct::Bulletproof& p);
280 
281 void toJsonValue(rapidjson::Document& doc, const rct::boroSig& sig, rapidjson::Value& val);
282 void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig);
283 
284 void toJsonValue(rapidjson::Document& doc, const rct::mgSig& sig, rapidjson::Value& val);
285 void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig);
286 
289 
292 
293 template <typename Map>
295 
296 template <typename Map>
297 typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map);
298 
299 template <typename Vec>
301 
302 template <typename Vec>
303 typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec);
304 
305 
306 // ideally would like to have the below functions in the .cpp file, but
307 // unfortunately because of how templates work they have to be here.
308 
309 template <typename Map>
311 {
312  val.SetObject();
313 
314  auto& al = doc.GetAllocator();
315 
316  for (const auto& i : map)
317  {
320  toJsonValue(doc, i.first, k);
321  toJsonValue(doc, i.second, m);
322  val.AddMember(k, m, al);
323  }
324 }
325 
326 template <typename Map>
328 {
329  if (!val.IsObject())
330  {
331  throw WRONG_TYPE("json object");
332  }
333 
334  auto itr = val.MemberBegin();
335 
336  while (itr != val.MemberEnd())
337  {
338  typename Map::key_type k;
339  typename Map::mapped_type m;
340  fromJsonValue(itr->name, k);
341  fromJsonValue(itr->value, m);
342  map.emplace(k, m);
343  ++itr;
344  }
345 }
346 
347 template <typename Vec>
349 {
350  val.SetArray();
351 
352  for (const auto& t : vec)
353  {
355  toJsonValue(doc, t, v);
356  val.PushBack(v, doc.GetAllocator());
357  }
358 }
359 
360 template <typename Vec>
362 {
363  if (!val.IsArray())
364  {
365  throw WRONG_TYPE("json array");
366  }
367 
368  for (rapidjson::SizeType i=0; i < val.Size(); i++)
369  {
370  typename Vec::value_type v;
371  fromJsonValue(val[i], v);
372  vec.push_back(v);
373  }
374 }
375 
376 } // namespace json
377 
378 } // namespace cryptonote
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
::std::string string
Definition: gtest-port.h:1097
epee::misc_utils::struct_init< response_t > response
boost::variant< txin_gen, txin_to_script, txin_to_scripthash, txin_to_key, txin_to_key_public > txin_v
WRONG_TYPE(const char *type)
Definition: json_object.h:89
const char * key
Definition: hmac_keccak.cpp:39
boost::variant< txout_to_script, txout_to_scripthash, txout_to_key, txout_to_key_public > txout_target_v
const char * what() const
Definition: json_object.h:73
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
std::string pod_to_hex(const t_pod_type &s)
Definition: string_tools.h:317
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2116
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2512
bool hex_to_pod(const std::string &hex_str, t_pod_type &s)
Definition: string_tools.h:324
constexpr bool is_to_hex()
Definition: json_object.h:112
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:91
expect< void > success() noexcept
Definition: expect.h:397
void toJsonValue(rapidjson::Document &doc, const std::string &i, rapidjson::Value &val)
void fromJsonValue(const rapidjson::Value &val, std::string &str)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
MISSING_KEY(const char *key)
Definition: json_object.h:81
Type
Type of JSON value.
Definition: rapidjson.h:620
error
Tracks LMDB error codes.
Definition: error.h:44
rapidjson::Document json
Definition: transport.cpp:49