2019-11-17 04:30:19 +00:00
|
|
|
// Copyright (c) 2016-2020, The Monero Project
|
2017-09-05 16:20:27 +00:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-11-07 05:45:06 +00:00
|
|
|
#include <boost/utility/string_ref.hpp>
|
|
|
|
#include <cstring>
|
|
|
|
#include <rapidjson/document.h>
|
|
|
|
#include <rapidjson/writer.h>
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
#include "byte_stream.h"
|
2017-09-05 16:20:27 +00:00
|
|
|
#include "cryptonote_basic/cryptonote_basic.h"
|
|
|
|
#include "rpc/message_data_structs.h"
|
|
|
|
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
|
|
|
|
#include "common/sfinae_helpers.h"
|
2019-11-17 04:30:19 +00:00
|
|
|
#include "hex.h"
|
|
|
|
#include "span.h"
|
2017-09-05 16:20:27 +00:00
|
|
|
|
|
|
|
#define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (!val.HasMember(key)) \
|
|
|
|
{ \
|
|
|
|
throw cryptonote::json::MISSING_KEY(key); \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
2019-11-07 05:45:06 +00:00
|
|
|
#define INSERT_INTO_JSON_OBJECT(dest, key, source) \
|
|
|
|
dest.Key(#key, sizeof(#key) - 1); \
|
|
|
|
cryptonote::json::toJsonValue(dest, source);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
|
|
|
#define GET_FROM_JSON_OBJECT(source, dst, key) \
|
|
|
|
OBJECT_HAS_MEMBER_OR_THROW(source, #key) \
|
|
|
|
decltype(dst) dstVal##key; \
|
|
|
|
cryptonote::json::fromJsonValue(source[#key], dstVal##key); \
|
|
|
|
dst = dstVal##key;
|
|
|
|
|
|
|
|
namespace cryptonote
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace json
|
|
|
|
{
|
|
|
|
|
|
|
|
struct JSON_ERROR : public std::exception
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
JSON_ERROR() { }
|
|
|
|
std::string m;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~JSON_ERROR() { }
|
|
|
|
|
|
|
|
const char* what() const throw()
|
|
|
|
{
|
|
|
|
return m.c_str();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MISSING_KEY : public JSON_ERROR
|
|
|
|
{
|
|
|
|
MISSING_KEY(const char* key)
|
|
|
|
{
|
|
|
|
m = std::string("Key \"") + key + "\" missing from object.";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct WRONG_TYPE : public JSON_ERROR
|
|
|
|
{
|
|
|
|
WRONG_TYPE(const char* type)
|
|
|
|
{
|
|
|
|
m = std::string("Json value has incorrect type, expected: ") + type;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BAD_INPUT : public JSON_ERROR
|
|
|
|
{
|
|
|
|
BAD_INPUT()
|
|
|
|
{
|
|
|
|
m = "An item failed to convert from json object to native object";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PARSE_FAIL : public JSON_ERROR
|
|
|
|
{
|
|
|
|
PARSE_FAIL()
|
|
|
|
{
|
|
|
|
m = "Failed to parse the json request";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-05 16:20:40 +00:00
|
|
|
template<typename Type>
|
|
|
|
inline constexpr bool is_to_hex()
|
|
|
|
{
|
|
|
|
return std::is_pod<Type>() && !std::is_integral<Type>();
|
|
|
|
}
|
|
|
|
|
2019-11-17 04:30:19 +00:00
|
|
|
void read_hex(const rapidjson::Value& val, epee::span<std::uint8_t> dest);
|
|
|
|
|
2019-11-07 05:45:06 +00:00
|
|
|
// POD to json key
|
|
|
|
template <class Type>
|
2020-03-18 23:22:39 +00:00
|
|
|
inline typename std::enable_if<is_to_hex<Type>()>::type toJsonKey(rapidjson::Writer<epee::byte_stream>& dest, const Type& pod)
|
2019-11-07 05:45:06 +00:00
|
|
|
{
|
|
|
|
const auto hex = epee::to_hex::array(pod);
|
|
|
|
dest.Key(hex.data(), hex.size());
|
|
|
|
}
|
2017-09-05 16:20:27 +00:00
|
|
|
|
|
|
|
// POD to json value
|
|
|
|
template <class Type>
|
2020-03-18 23:22:39 +00:00
|
|
|
inline typename std::enable_if<is_to_hex<Type>()>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Type& pod)
|
2017-09-05 16:20:27 +00:00
|
|
|
{
|
2019-11-07 05:45:06 +00:00
|
|
|
const auto hex = epee::to_hex::array(pod);
|
|
|
|
dest.String(hex.data(), hex.size());
|
2017-09-05 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Type>
|
2019-11-07 05:45:06 +00:00
|
|
|
inline typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t)
|
2017-09-05 16:20:27 +00:00
|
|
|
{
|
2019-11-17 04:30:19 +00:00
|
|
|
static_assert(std::is_standard_layout<Type>(), "expected standard layout type");
|
|
|
|
json::read_hex(val, epee::as_mut_byte_span(t));
|
2017-09-05 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rapidjson::Value& src);
|
2019-11-07 05:45:06 +00:00
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, boost::string_ref i);
|
|
|
|
inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::string& i)
|
2019-11-07 05:45:06 +00:00
|
|
|
{
|
|
|
|
toJsonValue(dest, boost::string_ref{i});
|
|
|
|
}
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, std::string& str);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, bool i);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, bool& b);
|
|
|
|
|
2017-09-05 16:20:40 +00:00
|
|
|
// integers overloads for toJsonValue are not needed for standard promotions
|
|
|
|
|
|
|
|
void fromJsonValue(const rapidjson::Value& val, unsigned char& i);
|
|
|
|
|
|
|
|
void fromJsonValue(const rapidjson::Value& val, signed char& i);
|
|
|
|
|
|
|
|
void fromJsonValue(const rapidjson::Value& val, char& i);
|
|
|
|
|
|
|
|
void fromJsonValue(const rapidjson::Value& val, unsigned short& i);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
2017-09-05 16:20:40 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, short& i);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned i);
|
2017-09-05 16:20:40 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, unsigned& i);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const int);
|
2017-09-05 16:20:40 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, int& i);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned long long i);
|
2017-09-05 16:20:40 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, unsigned long long& i);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const long long i);
|
2017-09-05 16:20:40 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, long long& i);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned long i) {
|
2019-11-07 05:45:06 +00:00
|
|
|
toJsonValue(dest, static_cast<unsigned long long>(i));
|
2017-09-05 16:20:40 +00:00
|
|
|
}
|
|
|
|
void fromJsonValue(const rapidjson::Value& val, unsigned long& i);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const long i) {
|
2019-11-07 05:45:06 +00:00
|
|
|
toJsonValue(dest, static_cast<long long>(i));
|
2017-09-05 16:20:40 +00:00
|
|
|
}
|
|
|
|
void fromJsonValue(const rapidjson::Value& val, long& i);
|
|
|
|
|
|
|
|
// end integers
|
2017-09-05 16:20:27 +00:00
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::transaction& tx);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::transaction& tx);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::block& b);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::block& b);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_v& txin);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_gen& txin);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_script& txin);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_script& txin);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_scripthash& txin);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_scripthash& txin);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_key& txin);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_key& txin);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_target_v& txout);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_target_v& txout);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_script& txout);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_script& txout);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_scripthash& txout);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_scripthash& txout);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_key& txout);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_key& txout);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_out& txout);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::connection_info& info);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& info);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_blob_entry& tx);
|
2019-09-16 12:18:34 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_blob_entry& tx);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::block_complete_entry& blk);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::block_complete_entry& blk);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::block_with_transactions& blk);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::block_with_transactions& blk);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::transaction_info& tx_info);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::transaction_info& tx_info);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_key_and_amount_index& out);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_and_amount_index& out);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::amount_with_random_outputs& out);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::amount_with_random_outputs& out);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::peer& peer);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::peer& peer);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::tx_in_pool& tx);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::tx_in_pool& tx);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::hard_fork_info& info);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::hard_fork_info& info);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_amount_count& out);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_count& out);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_amount_and_index& out);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_and_index& out);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_key_mask_unlocked& out);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_mask_unlocked& out);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::error& err);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::error& error);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::BlockHeaderResponse& response);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::BlockHeaderResponse& response);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::rctSig& i);
|
2019-11-07 05:45:06 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, rct::rctSig& sig);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
2020-05-30 01:59:58 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, rct::ctkey& key);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::ecdhTuple& tuple);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::rangeSig& sig);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::Bulletproof& p);
|
2017-12-02 21:17:42 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, rct::Bulletproof& p);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::boroSig& sig);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::mgSig& sig);
|
2017-09-05 16:20:27 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig);
|
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::DaemonInfo& info);
|
2017-09-05 16:20:40 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& info);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
2020-03-18 23:22:39 +00:00
|
|
|
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_distribution& dist);
|
2018-10-20 02:06:03 +00:00
|
|
|
void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_distribution& dist);
|
|
|
|
|
2017-09-05 16:20:27 +00:00
|
|
|
template <typename Map>
|
2020-03-18 23:22:39 +00:00
|
|
|
typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Map& map);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
|
|
|
template <typename Map>
|
|
|
|
typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map);
|
|
|
|
|
|
|
|
template <typename Vec>
|
2020-03-18 23:22:39 +00:00
|
|
|
typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Vec &vec);
|
2017-09-05 16:20:27 +00:00
|
|
|
|
|
|
|
template <typename Vec>
|
|
|
|
typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec);
|
|
|
|
|
|
|
|
|
|
|
|
// ideally would like to have the below functions in the .cpp file, but
|
|
|
|
// unfortunately because of how templates work they have to be here.
|
|
|
|
|
|
|
|
template <typename Map>
|
2020-03-18 23:22:39 +00:00
|
|
|
inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Map& map)
|
2017-09-05 16:20:27 +00:00
|
|
|
{
|
2019-11-07 05:45:06 +00:00
|
|
|
using key_type = typename Map::key_type;
|
|
|
|
static_assert(std::is_same<std::string, key_type>() || is_to_hex<key_type>(), "invalid map key type");
|
2017-09-05 16:20:27 +00:00
|
|
|
|
2019-11-07 05:45:06 +00:00
|
|
|
dest.StartObject();
|
2017-09-05 16:20:27 +00:00
|
|
|
for (const auto& i : map)
|
|
|
|
{
|
2019-11-07 05:45:06 +00:00
|
|
|
toJsonKey(dest, i.first);
|
|
|
|
toJsonValue(dest, i.second);
|
2017-09-05 16:20:27 +00:00
|
|
|
}
|
2019-11-07 05:45:06 +00:00
|
|
|
dest.EndObject();
|
2017-09-05 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Map>
|
2019-11-07 05:45:06 +00:00
|
|
|
inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map)
|
2017-09-05 16:20:27 +00:00
|
|
|
{
|
|
|
|
if (!val.IsObject())
|
|
|
|
{
|
|
|
|
throw WRONG_TYPE("json object");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto itr = val.MemberBegin();
|
|
|
|
|
|
|
|
while (itr != val.MemberEnd())
|
|
|
|
{
|
|
|
|
typename Map::key_type k;
|
|
|
|
typename Map::mapped_type m;
|
|
|
|
fromJsonValue(itr->name, k);
|
|
|
|
fromJsonValue(itr->value, m);
|
|
|
|
map.emplace(k, m);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Vec>
|
2020-03-18 23:22:39 +00:00
|
|
|
inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Vec &vec)
|
2017-09-05 16:20:27 +00:00
|
|
|
{
|
2019-11-07 05:45:06 +00:00
|
|
|
dest.StartArray();
|
2017-09-05 16:20:27 +00:00
|
|
|
for (const auto& t : vec)
|
2019-11-07 05:45:06 +00:00
|
|
|
toJsonValue(dest, t);
|
2020-03-16 23:59:26 +00:00
|
|
|
dest.EndArray();
|
2017-09-05 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Vec>
|
2019-11-07 05:45:06 +00:00
|
|
|
inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec)
|
2017-09-05 16:20:27 +00:00
|
|
|
{
|
|
|
|
if (!val.IsArray())
|
|
|
|
{
|
|
|
|
throw WRONG_TYPE("json array");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (rapidjson::SizeType i=0; i < val.Size(); i++)
|
|
|
|
{
|
|
|
|
typename Vec::value_type v;
|
|
|
|
fromJsonValue(val[i], v);
|
|
|
|
vec.push_back(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace json
|
|
|
|
|
|
|
|
} // namespace cryptonote
|