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);
161 void fromJsonValue(
const rapidjson::Value& val,
unsigned& i);
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: json_object.h:79
Definition: core_rpc_server_commands_defs.h:1197
PARSE_FAIL()
Definition: json_object.h:105
boost::variant< txout_to_script, txout_to_scripthash, txout_to_key > txout_target_v
Definition: cryptonote_basic.h:140
Definition: cryptonote_basic.h:109
JSON_ERROR()
Definition: json_object.h:67
Definition: message_data_structs.h:71
Definition: cryptonote_protocol_defs.h:117
Definition: cryptonote_basic.h:409
MISSING_KEY(const char *key)
Definition: json_object.h:81
Definition: cryptonote_protocol_defs.h:48
Definition: message_data_structs.h:174
Definition: cryptonote_basic.h:77
Definition: cryptonote_basic.h:61
Definition: rctTypes.h:124
Definition: message_data_structs.h:46
Definition: cryptonote_basic.h:87
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
Definition: json_object.h:87
Definition: cryptonote_basic.h:96
Definition: message_data_structs.h:129
Definition: cryptonote_basic.h:72
Definition: rctTypes.h:139
int b
Definition: base.py:1
Definition: cryptonote_basic.h:124
Definition: message_data_structs.h:77
std::string m
Definition: json_object.h:68
Definition: rctTypes.h:436
Definition: json_object.h:64
Definition: message_data_structs.h:116
Definition: rctTypes.h:169
Definition: message_data_structs.h:56
Definition: rctTypes.h:152
Definition: message_data_structs.h:108
constexpr bool is_to_hex()
Definition: json_object.h:112
Definition: rctTypes.h:179
Definition: message_data_structs.h:203
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)
Definition: json_object.cpp:112
void fromJsonValue(const rapidjson::Value &val, std::string &str)
Definition: json_object.cpp:117
Definition: core_rpc_server_commands_defs.h:1319
WRONG_TYPE(const char *type)
Definition: json_object.h:89
boost::variant< txin_gen, txin_to_script, txin_to_scripthash, txin_to_key > txin_v
Definition: cryptonote_basic.h:138
Definition: message_data_structs.h:142
const char * what() const
Definition: json_object.h:73
virtual ~JSON_ERROR()
Definition: json_object.h:71
Definition: cryptonote_basic.h:143
Definition: message_data_structs.h:87
Definition: message_data_structs.h:63
error
Tracks LMDB error codes.
Definition: error.h:44
Definition: message_data_structs.h:122
Definition: cryptonote_basic.h:204
rapidjson::Document json
Definition: transport.cpp:49
Definition: json_object.h:103