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++)