31 #include "string_tools.h"
32 #include "rapidjson/document.h"
38 #define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
41 if (!val.HasMember(key)) \
43 throw cryptonote::json::MISSING_KEY(key); \
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());
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); \
73 const char*
what()
const throw()
83 m = std::string(
"Key \"") + key +
"\" missing from object.";
91 m = std::string(
"Json value has incorrect type, expected: ") + type;
99 m =
"An item failed to convert from json object to native object";
107 m =
"Failed to parse the json request";
111 template<
typename Type>
114 return std::is_pod<Type>() && !std::is_integral<Type>();
119 template <
class Type>
120 typename std::enable_if<is_to_hex<Type>()>::type
toJsonValue(rapidjson::Document& doc,
const Type& pod, rapidjson::Value& value)
122 value = rapidjson::Value(epee::string_tools::pod_to_hex(pod).c_str(), doc.GetAllocator());
125 template <
class Type>
126 typename std::enable_if<is_to_hex<Type>()>::type
fromJsonValue(
const rapidjson::Value& val, Type& t)
134 bool success = epee::string_tools::hex_to_pod(val.GetString(), t);
142 void toJsonValue(rapidjson::Document& doc,
const std::string& i, rapidjson::Value& val);
143 void fromJsonValue(
const rapidjson::Value& val, std::string& str);
145 void toJsonValue(rapidjson::Document& doc,
bool i, rapidjson::Value& val);
150 void fromJsonValue(
const rapidjson::Value& val,
unsigned char& i);
152 void fromJsonValue(
const rapidjson::Value& val,
signed char& i);
156 void fromJsonValue(
const rapidjson::Value& val,
unsigned short& i);
160 void toJsonValue(rapidjson::Document& doc,
const unsigned i, rapidjson::Value& val);
163 void toJsonValue(rapidjson::Document& doc,
const int, rapidjson::Value& val);
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);
170 void toJsonValue(rapidjson::Document& doc,
const long long i, rapidjson::Value& val);
171 void fromJsonValue(
const rapidjson::Value& val,
long long& i);
173 inline void toJsonValue(rapidjson::Document& doc,
const unsigned long i, rapidjson::Value& val) {
174 toJsonValue(doc,
static_cast<unsigned long long>(i), val);
176 void fromJsonValue(
const rapidjson::Value& val,
unsigned long& i);
178 inline void toJsonValue(rapidjson::Document& doc,
const long i, rapidjson::Value& val) {
287 template <
typename Map>
288 typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
toJsonValue(rapidjson::Document& doc,
const Map& map, rapidjson::Value& val);
290 template <
typename Map>
291 typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Map& map);
293 template <
typename Vec>
294 typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
toJsonValue(rapidjson::Document& doc,
const Vec &vec, rapidjson::Value& val);
296 template <
typename Vec>
297 typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Vec& vec);
303 template <
typename Map>
304 typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
toJsonValue(rapidjson::Document& doc,
const Map& map, rapidjson::Value& val)
308 auto& al = doc.GetAllocator();
310 for (
const auto& i : map)
316 val.AddMember(k, m, al);
320 template <
typename Map>
321 typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Map& map)
328 auto itr = val.MemberBegin();
330 while (itr != val.MemberEnd())
332 typename Map::key_type k;
333 typename Map::mapped_type m;
341 template <
typename Vec>
342 typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
toJsonValue(rapidjson::Document& doc,
const Vec &vec, rapidjson::Value& val)
346 for (
const auto& t : vec)
350 val.PushBack(v, doc.GetAllocator());
354 template <
typename Vec>
355 typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Vec& vec)
362 for (rapidjson::SizeType i=0; i < val.Size(); i++)
364 typename Vec::value_type v;
Definition: cryptonote_basic.h:205
expect< void > success() noexcept
Definition: expect.h:397
int b
Definition: base.py:1
void toJsonValue(rapidjson::Document &doc, const std::string &i, rapidjson::Value &val)
Definition: json_object.cpp:112
constexpr bool is_to_hex()
Definition: json_object.h:112
void fromJsonValue(const rapidjson::Value &val, std::string &str)
Definition: json_object.cpp:117
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:226
boost::variant< txout_to_script, txout_to_scripthash, txout_to_key > txout_target_v
Definition: cryptonote_basic.h:140
boost::variant< txin_gen, txin_to_script, txin_to_scripthash, txin_to_key > txin_v
Definition: cryptonote_basic.h:138
error
Tracks LMDB error codes.
Definition: error.h:45
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:91
Definition: cryptonote_protocol_defs.h:118
Definition: cryptonote_basic.h:410
Definition: cryptonote_protocol_defs.h:49
Definition: json_object.h:65
std::string m
Definition: json_object.h:68
const char * what() const
Definition: json_object.h:73
virtual ~JSON_ERROR()
Definition: json_object.h:71
JSON_ERROR()
Definition: json_object.h:67
Definition: json_object.h:80
MISSING_KEY(const char *key)
Definition: json_object.h:81
Definition: json_object.h:104
PARSE_FAIL()
Definition: json_object.h:105
Definition: json_object.h:88
WRONG_TYPE(const char *type)
Definition: json_object.h:89
Definition: core_rpc_server_commands_defs.h:1197
Definition: message_data_structs.h:175
Definition: message_data_structs.h:72
Definition: message_data_structs.h:47
Definition: message_data_structs.h:143
Definition: message_data_structs.h:130
Definition: message_data_structs.h:117
Definition: message_data_structs.h:109
Definition: message_data_structs.h:204
Definition: message_data_structs.h:64
Definition: message_data_structs.h:123
Definition: message_data_structs.h:78
Definition: message_data_structs.h:57
Definition: message_data_structs.h:88
Definition: core_rpc_server_commands_defs.h:1320
Definition: cryptonote_basic.h:144
Definition: cryptonote_basic.h:88
Definition: cryptonote_basic.h:125
Definition: cryptonote_basic.h:97
Definition: cryptonote_basic.h:110
Definition: cryptonote_basic.h:78
Definition: cryptonote_basic.h:62
Definition: cryptonote_basic.h:73
Definition: rctTypes.h:180
Definition: rctTypes.h:139
Definition: rctTypes.h:124
Definition: rctTypes.h:152
Definition: rctTypes.h:169
Definition: rctTypes.h:436
rapidjson::Document json
Definition: transport.cpp:49