31 #include <boost/utility/string_ref.hpp>
33 #include <rapidjson/document.h>
34 #include <rapidjson/writer.h>
37 #include "byte_stream.h"
45 #define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
48 if (!val.HasMember(key)) \
50 throw cryptonote::json::MISSING_KEY(key); \
54 #define INSERT_INTO_JSON_OBJECT(dest, key, source) \
55 dest.Key(#key, sizeof(#key) - 1); \
56 cryptonote::json::toJsonValue(dest, source);
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); \
79 const char*
what()
const throw()
89 m = std::string(
"Key \"") + key +
"\" missing from object.";
97 m = std::string(
"Json value has incorrect type, expected: ") + type;
105 m =
"An item failed to convert from json object to native object";
113 m =
"Failed to parse the json request";
117 template<
typename Type>
120 return std::is_pod<Type>() && !std::is_integral<Type>();
123 void read_hex(
const rapidjson::Value& val, epee::span<std::uint8_t>
dest);
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)
129 const auto hex = epee::to_hex::array(pod);
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)
137 const auto hex = epee::to_hex::array(pod);
141 template <
class Type>
142 inline typename std::enable_if<is_to_hex<Type>()>::type
fromJsonValue(
const rapidjson::Value& val, Type& t)
144 static_assert(std::is_standard_layout<Type>(),
"expected standard layout type");
148 void toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const rapidjson::Value& src);
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)
155 void fromJsonValue(
const rapidjson::Value& val, std::string& str);
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);
165 void fromJsonValue(
const rapidjson::Value& val,
unsigned char& i);
167 void fromJsonValue(
const rapidjson::Value& val,
signed char& i);
171 void fromJsonValue(
const rapidjson::Value& val,
unsigned short& i);
178 void toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const int);
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);
185 void toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const long long i);
186 void fromJsonValue(
const rapidjson::Value& val,
long long& i);
188 inline void toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const unsigned long i) {
191 void fromJsonValue(
const rapidjson::Value& val,
unsigned long& i);
307 template <
typename Map>
308 typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const Map& map);
310 template <
typename Map>
311 typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Map& map);
313 template <
typename Vec>
314 typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const Vec &vec);
316 template <
typename Vec>
317 typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Vec& vec);
323 template <
typename Map>
324 inline typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const Map& map)
326 using key_type =
typename Map::key_type;
327 static_assert(std::is_same<std::string, key_type>() || is_to_hex<key_type>(),
"invalid map key type");
330 for (
const auto& i : map)
338 template <
typename Map>
339 inline typename std::enable_if<sfinae::is_map_like<Map>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Map& map)
346 auto itr = val.MemberBegin();
349 while (itr != val.MemberEnd())
351 typename Map::key_type k;
352 typename Map::mapped_type m;
360 template <
typename Vec>
361 inline typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
toJsonValue(rapidjson::Writer<epee::byte_stream>&
dest,
const Vec &vec)
363 using value_type =
typename Vec::value_type;
364 static_assert(!std::is_same<value_type, char>::value,
"encoding an array of chars is faster as hex");
365 static_assert(!std::is_same<value_type, unsigned char>::value,
"encoding an array of unsigned char is faster as hex");
368 for (
const auto& t : vec)
386 template <
typename Vec>
387 inline typename std::enable_if<sfinae::is_vector_like<Vec>::value,
void>::type
fromJsonValue(
const rapidjson::Value& val, Vec& vec)
389 using value_type =
typename Vec::value_type;
390 static_assert(!std::is_same<value_type, char>::value,
"encoding a vector of chars is faster as hex");
391 static_assert(!std::is_same<value_type, unsigned char>::value,
"encoding a vector of unsigned char is faster as hex");
400 for (rapidjson::SizeType i=0; i < val.Size(); i++)
Definition: cryptonote_basic.h:194
const uint32_t T[512]
Definition: groestl_tables.h:36
int b
Definition: base.py:1
void reserve(const T &, std::size_t)
Definition: json_object.h:376
constexpr bool is_to_hex()
Definition: json_object.h:118
void read_hex(const rapidjson::Value &val, epee::span< std::uint8_t > dest)
Definition: json_object.cpp:118
void fromJsonValue(const rapidjson::Value &val, std::string &str)
Definition: json_object.cpp:141
std::enable_if< is_to_hex< Type >)>::type toJsonKey(rapidjson::Writer< epee::byte_stream > &dest, const Type &pod)
Definition: json_object.h:127
void toJsonValue(rapidjson::Writer< epee::byte_stream > &dest, const rapidjson::Value &src)
Definition: json_object.cpp:131
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:45
boost::variant< txout_to_script, txout_to_scripthash, txout_to_key > txout_target_v
Definition: cryptonote_basic.h:139
boost::variant< txin_gen, txin_to_script, txin_to_scripthash, txin_to_key > txin_v
Definition: cryptonote_basic.h:137
std::string hex(difficulty_type v)
Definition: difficulty.cpp:242
mdb_size_t count(MDB_cursor *cur)
Definition: value_stream.cpp:39
error
Tracks LMDB error codes.
Definition: error.h:45
CXA_THROW_INFO_T void(* dest)(void *))
Definition: stack_trace.cpp:90
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:90
Definition: cryptonote_protocol_defs.h:133
Definition: cryptonote_basic.h:464
Definition: cryptonote_protocol_defs.h:48
Definition: json_object.h:71
std::string m
Definition: json_object.h:74
const char * what() const
Definition: json_object.h:79
virtual ~JSON_ERROR()
Definition: json_object.h:77
JSON_ERROR()
Definition: json_object.h:73
Definition: json_object.h:86
MISSING_KEY(const char *key)
Definition: json_object.h:87
Definition: json_object.h:110
PARSE_FAIL()
Definition: json_object.h:111
Definition: json_object.h:94
WRONG_TYPE(const char *type)
Definition: json_object.h:95
Definition: core_rpc_server_commands_defs.h:1159
Definition: message_data_structs.h:176
Definition: message_data_structs.h:72
Definition: message_data_structs.h:47
Definition: message_data_structs.h:144
Definition: message_data_structs.h:131
Definition: message_data_structs.h:118
Definition: message_data_structs.h:110
Definition: message_data_structs.h:206
Definition: message_data_structs.h:64
Definition: message_data_structs.h:124
Definition: message_data_structs.h:78
Definition: message_data_structs.h:57
Definition: message_data_structs.h:89
Definition: cryptonote_protocol_defs.h:122
Definition: core_rpc_server_commands_defs.h:1344
Definition: cryptonote_basic.h:143
Definition: cryptonote_basic.h:87
Definition: cryptonote_basic.h:124
Definition: cryptonote_basic.h:96
Definition: cryptonote_basic.h:109
Definition: cryptonote_basic.h:77
Definition: cryptonote_basic.h:61
Definition: cryptonote_basic.h:72
Definition: rctTypes.h:205
Definition: rctTypes.h:147
Definition: rctTypes.h:97
Definition: rctTypes.h:132
Definition: rctTypes.h:160
Definition: rctTypes.h:194
Definition: rctTypes.h:536
rapidjson::Document json
Definition: transport.cpp:49