From 0f78b06e8c578819f831a513490278a5f70b01af Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Thu, 7 Nov 2019 05:45:06 +0000 Subject: [PATCH] Various improvements to the ZMQ JSON-RPC handling: - Finding handling function in ZMQ JSON-RPC now uses binary search - Temporary `std::vector`s in JSON output now use `epee::span` to prevent allocations. - Binary -> hex in JSON output no longer allocates temporary buffer - C++ structs -> JSON skips intermediate DOM creation, and instead write directly to an output stream. --- contrib/epee/include/hex.h | 12 +- src/rpc/daemon_handler.cpp | 125 +++-- src/rpc/daemon_handler.h | 2 +- src/rpc/daemon_messages.cpp | 618 ++++++++--------------- src/rpc/daemon_messages.h | 17 +- src/rpc/message.cpp | 198 +++----- src/rpc/message.h | 49 +- src/serialization/CMakeLists.txt | 1 + src/serialization/json_object.cpp | 619 +++++++++++++----------- src/serialization/json_object.h | 162 ++++--- tests/unit_tests/epee_utils.cpp | 11 + tests/unit_tests/json_serialization.cpp | 47 +- 12 files changed, 832 insertions(+), 1029 deletions(-) diff --git a/contrib/epee/include/hex.h b/contrib/epee/include/hex.h index 6e720f128..4e8f90786 100644 --- a/contrib/epee/include/hex.h +++ b/contrib/epee/include/hex.h @@ -51,12 +51,22 @@ namespace epee template static std::array array(const std::array& src) noexcept { - std::array out{{}}; + std::array out; static_assert(N <= 128, "keep the stack size down"); buffer_unchecked(out.data(), {src.data(), src.size()}); return out; } + //! \return An array containing hex of `src`. + template + static std::array array(const T& src) noexcept + { + std::array out; + static_assert(sizeof(T) <= 128, "keep the stack size down"); + buffer_unchecked(out.data(), as_byte_span(src)); + return out; + } + //! Append `src` as hex to `out`. static void buffer(std::ostream& out, const span src); diff --git a/src/rpc/daemon_handler.cpp b/src/rpc/daemon_handler.cpp index 0b6cd6c7a..7c2bc4562 100644 --- a/src/rpc/daemon_handler.cpp +++ b/src/rpc/daemon_handler.cpp @@ -28,6 +28,10 @@ #include "daemon_handler.h" +#include +#include +#include + #include // likely included by daemon_handler.h's includes, // but including here for clarity @@ -42,6 +46,74 @@ namespace cryptonote namespace rpc { + namespace + { + using handler_function = std::string(DaemonHandler& handler, const rapidjson::Value& id, const rapidjson::Value& msg); + struct handler_map + { + const char* method_name; + handler_function* call; + }; + + bool operator<(const handler_map& lhs, const handler_map& rhs) noexcept + { + return std::strcmp(lhs.method_name, rhs.method_name) < 0; + } + + bool operator<(const handler_map& lhs, const std::string& rhs) noexcept + { + return std::strcmp(lhs.method_name, rhs.c_str()) < 0; + } + + template + std::string handle_message(DaemonHandler& handler, const rapidjson::Value& id, const rapidjson::Value& parameters) + { + typename Message::Request request{}; + request.fromJson(parameters); + + typename Message::Response response{}; + handler.handle(request, response); + return FullMessage::getResponse(response, id); + } + + constexpr const handler_map handlers[] = + { + {u8"get_block_hash", handle_message}, + {u8"get_block_header_by_hash", handle_message}, + {u8"get_block_header_by_height", handle_message}, + {u8"get_block_headers_by_height", handle_message}, + {u8"get_blocks_fast", handle_message}, + {u8"get_dynamic_fee_estimate", handle_message}, + {u8"get_hashes_fast", handle_message}, + {u8"get_height", handle_message}, + {u8"get_info", handle_message}, + {u8"get_last_block_header", handle_message}, + {u8"get_output_distribution", handle_message}, + {u8"get_output_histogram", handle_message}, + {u8"get_output_keys", handle_message}, + {u8"get_peer_list", handle_message}, + {u8"get_rpc_version", handle_message}, + {u8"get_transaction_pool", handle_message}, + {u8"get_transactions", handle_message}, + {u8"get_tx_global_output_indices", handle_message}, + {u8"hard_fork_info", handle_message}, + {u8"key_images_spent", handle_message}, + {u8"mining_status", handle_message}, + {u8"save_bc", handle_message}, + {u8"send_raw_tx", handle_message}, + {u8"set_log_level", handle_message}, + {u8"start_mining", handle_message}, + {u8"stop_mining", handle_message} + }; + } // anonymous + + DaemonHandler::DaemonHandler(cryptonote::core& c, t_p2p& p2p) + : m_core(c), m_p2p(p2p) + { + const auto last_sorted = std::is_sorted_until(std::begin(handlers), std::end(handlers)); + if (last_sorted != std::end(handlers)) + throw std::logic_error{std::string{"ZMQ JSON-RPC handlers map is not properly sorted, see "} + last_sorted->method_name}; + } void DaemonHandler::handle(const GetHeight::Request& req, GetHeight::Response& res) { @@ -840,68 +912,21 @@ namespace rpc { MDEBUG("Handling RPC request: " << request); - Message* resp_message = NULL; - try { FullMessage req_full(request, true); - rapidjson::Value& req_json = req_full.getMessage(); - const std::string request_type = req_full.getRequestType(); - - // create correct Message subclass and call handle() on it - REQ_RESP_TYPES_MACRO(request_type, GetHeight, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetBlocksFast, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetHashesFast, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetTransactions, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, KeyImagesSpent, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetTxGlobalOutputIndices, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, SendRawTx, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, SendRawTxHex, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetInfo, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, StartMining, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, StopMining, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, MiningStatus, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, SaveBC, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetBlockHash, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetLastBlockHeader, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetBlockHeaderByHash, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetBlockHeaderByHeight, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetBlockHeadersByHeight, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetPeerList, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, SetLogLevel, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetTransactionPool, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, HardForkInfo, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetOutputHistogram, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetOutputKeys, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetRPCVersion, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetFeeEstimate, req_json, resp_message, handle); - REQ_RESP_TYPES_MACRO(request_type, GetOutputDistribution, req_json, resp_message, handle); - - // if none of the request types matches - if (resp_message == NULL) - { + const auto matched_handler = std::lower_bound(std::begin(handlers), std::end(handlers), request_type); + if (matched_handler == std::end(handlers) || matched_handler->method_name != request_type) return BAD_REQUEST(request_type, req_full.getID()); - } - - FullMessage resp_full = FullMessage::responseMessage(resp_message, req_full.getID()); - - const std::string response = resp_full.getJson(); - delete resp_message; - resp_message = NULL; + std::string response = matched_handler->call(*this, req_full.getID(), req_full.getMessage()); MDEBUG("Returning RPC response: " << response); - return response; } catch (const std::exception& e) { - if (resp_message) - { - delete resp_message; - } - return BAD_JSON(e.what()); } } diff --git a/src/rpc/daemon_handler.h b/src/rpc/daemon_handler.h index 34723f676..c33f608ab 100644 --- a/src/rpc/daemon_handler.h +++ b/src/rpc/daemon_handler.h @@ -50,7 +50,7 @@ class DaemonHandler : public RpcHandler { public: - DaemonHandler(cryptonote::core& c, t_p2p& p2p) : m_core(c), m_p2p(p2p) { } + DaemonHandler(cryptonote::core& c, t_p2p& p2p); ~DaemonHandler() { } diff --git a/src/rpc/daemon_messages.cpp b/src/rpc/daemon_messages.cpp index cf0f5ece1..5c179408e 100644 --- a/src/rpc/daemon_messages.cpp +++ b/src/rpc/daemon_messages.cpp @@ -34,99 +34,47 @@ namespace cryptonote namespace rpc { +void GetHeight::Request::doToJson(rapidjson::Writer& dest) const +{} -const char* const GetHeight::name = "get_height"; -const char* const GetBlocksFast::name = "get_blocks_fast"; -const char* const GetHashesFast::name = "get_hashes_fast"; -const char* const GetTransactions::name = "get_transactions"; -const char* const KeyImagesSpent::name = "key_images_spent"; -const char* const GetTxGlobalOutputIndices::name = "get_tx_global_output_indices"; -const char* const SendRawTx::name = "send_raw_tx"; -const char* const SendRawTxHex::name = "send_raw_tx_hex"; -const char* const StartMining::name = "start_mining"; -const char* const StopMining::name = "stop_mining"; -const char* const MiningStatus::name = "mining_status"; -const char* const GetInfo::name = "get_info"; -const char* const SaveBC::name = "save_bc"; -const char* const GetBlockHash::name = "get_block_hash"; -const char* const GetLastBlockHeader::name = "get_last_block_header"; -const char* const GetBlockHeaderByHash::name = "get_block_header_by_hash"; -const char* const GetBlockHeaderByHeight::name = "get_block_header_by_height"; -const char* const GetBlockHeadersByHeight::name = "get_block_headers_by_height"; -const char* const GetPeerList::name = "get_peer_list"; -const char* const SetLogLevel::name = "set_log_level"; -const char* const GetTransactionPool::name = "get_transaction_pool"; -const char* const HardForkInfo::name = "hard_fork_info"; -const char* const GetOutputHistogram::name = "get_output_histogram"; -const char* const GetOutputKeys::name = "get_output_keys"; -const char* const GetRPCVersion::name = "get_rpc_version"; -const char* const GetFeeEstimate::name = "get_dynamic_fee_estimate"; -const char* const GetOutputDistribution::name = "get_output_distribution"; - - - - -rapidjson::Value GetHeight::Request::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} - -void GetHeight::Request::fromJson(rapidjson::Value& val) +void GetHeight::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetHeight::Response::toJson(rapidjson::Document& doc) const +void GetHeight::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - auto& al = doc.GetAllocator(); - - val.AddMember("height", height, al); - - return val; + INSERT_INTO_JSON_OBJECT(dest, height, height); } -void GetHeight::Response::fromJson(rapidjson::Value& val) +void GetHeight::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, height, height); } -rapidjson::Value GetBlocksFast::Request::toJson(rapidjson::Document& doc) const +void GetBlocksFast::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - auto& al = doc.GetAllocator(); - - INSERT_INTO_JSON_OBJECT(val, doc, block_ids, block_ids); - val.AddMember("start_height", start_height, al); - val.AddMember("prune", prune, al); - - return val; + INSERT_INTO_JSON_OBJECT(dest, block_ids, block_ids); + INSERT_INTO_JSON_OBJECT(dest, start_height, start_height); + INSERT_INTO_JSON_OBJECT(dest, prune, prune); } -void GetBlocksFast::Request::fromJson(rapidjson::Value& val) +void GetBlocksFast::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, block_ids, block_ids); GET_FROM_JSON_OBJECT(val, start_height, start_height); GET_FROM_JSON_OBJECT(val, prune, prune); } -rapidjson::Value GetBlocksFast::Response::toJson(rapidjson::Document& doc) const +void GetBlocksFast::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - auto& al = doc.GetAllocator(); - - INSERT_INTO_JSON_OBJECT(val, doc, blocks, blocks); - val.AddMember("start_height", start_height, al); - val.AddMember("current_height", current_height, al); - INSERT_INTO_JSON_OBJECT(val, doc, output_indices, output_indices); - - return val; + INSERT_INTO_JSON_OBJECT(dest, blocks, blocks); + INSERT_INTO_JSON_OBJECT(dest, start_height, start_height); + INSERT_INTO_JSON_OBJECT(dest, current_height, current_height); + INSERT_INTO_JSON_OBJECT(dest, output_indices, output_indices); } -void GetBlocksFast::Response::fromJson(rapidjson::Value& val) +void GetBlocksFast::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, blocks, blocks); GET_FROM_JSON_OBJECT(val, start_height, start_height); @@ -135,38 +83,26 @@ void GetBlocksFast::Response::fromJson(rapidjson::Value& val) } -rapidjson::Value GetHashesFast::Request::toJson(rapidjson::Document& doc) const +void GetHashesFast::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - auto& al = doc.GetAllocator(); - - INSERT_INTO_JSON_OBJECT(val, doc, known_hashes, known_hashes); - val.AddMember("start_height", start_height, al); - - return val; + INSERT_INTO_JSON_OBJECT(dest, known_hashes, known_hashes); + INSERT_INTO_JSON_OBJECT(dest, start_height, start_height); } -void GetHashesFast::Request::fromJson(rapidjson::Value& val) +void GetHashesFast::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, known_hashes, known_hashes); GET_FROM_JSON_OBJECT(val, start_height, start_height); } -rapidjson::Value GetHashesFast::Response::toJson(rapidjson::Document& doc) const +void GetHashesFast::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - auto& al = doc.GetAllocator(); - - INSERT_INTO_JSON_OBJECT(val, doc, hashes, hashes); - val.AddMember("start_height", start_height, al); - val.AddMember("current_height", current_height, al); - - return val; + INSERT_INTO_JSON_OBJECT(dest, hashes, hashes); + INSERT_INTO_JSON_OBJECT(dest, start_height, start_height); + INSERT_INTO_JSON_OBJECT(dest, current_height, current_height); } -void GetHashesFast::Response::fromJson(rapidjson::Value& val) +void GetHashesFast::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, hashes, hashes); GET_FROM_JSON_OBJECT(val, start_height, start_height); @@ -174,154 +110,114 @@ void GetHashesFast::Response::fromJson(rapidjson::Value& val) } -rapidjson::Value GetTransactions::Request::toJson(rapidjson::Document& doc) const +void GetTransactions::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, tx_hashes, tx_hashes); - - return val; + INSERT_INTO_JSON_OBJECT(dest, tx_hashes, tx_hashes); } -void GetTransactions::Request::fromJson(rapidjson::Value& val) +void GetTransactions::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, tx_hashes, tx_hashes); } -rapidjson::Value GetTransactions::Response::toJson(rapidjson::Document& doc) const +void GetTransactions::Response::doToJson(rapidjson::Writer& dest) const { - rapidjson::Value val(rapidjson::kObjectType); - - INSERT_INTO_JSON_OBJECT(val, doc, txs, txs); - INSERT_INTO_JSON_OBJECT(val, doc, missed_hashes, missed_hashes); - - return val; + INSERT_INTO_JSON_OBJECT(dest, txs, txs); + INSERT_INTO_JSON_OBJECT(dest, missed_hashes, missed_hashes); } -void GetTransactions::Response::fromJson(rapidjson::Value& val) +void GetTransactions::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, txs, txs); GET_FROM_JSON_OBJECT(val, missed_hashes, missed_hashes); } -rapidjson::Value KeyImagesSpent::Request::toJson(rapidjson::Document& doc) const +void KeyImagesSpent::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, key_images, key_images); - - return val; + INSERT_INTO_JSON_OBJECT(dest, key_images, key_images); } -void KeyImagesSpent::Request::fromJson(rapidjson::Value& val) +void KeyImagesSpent::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, key_images, key_images); } -rapidjson::Value KeyImagesSpent::Response::toJson(rapidjson::Document& doc) const +void KeyImagesSpent::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, spent_status, spent_status); - - return val; + INSERT_INTO_JSON_OBJECT(dest, spent_status, spent_status); } -void KeyImagesSpent::Response::fromJson(rapidjson::Value& val) +void KeyImagesSpent::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, spent_status, spent_status); } -rapidjson::Value GetTxGlobalOutputIndices::Request::toJson(rapidjson::Document& doc) const +void GetTxGlobalOutputIndices::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, tx_hash, tx_hash); - - return val; + INSERT_INTO_JSON_OBJECT(dest, tx_hash, tx_hash); } -void GetTxGlobalOutputIndices::Request::fromJson(rapidjson::Value& val) +void GetTxGlobalOutputIndices::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, tx_hash, tx_hash); } -rapidjson::Value GetTxGlobalOutputIndices::Response::toJson(rapidjson::Document& doc) const +void GetTxGlobalOutputIndices::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, output_indices, output_indices); - - return val; + INSERT_INTO_JSON_OBJECT(dest, output_indices, output_indices); } -void GetTxGlobalOutputIndices::Response::fromJson(rapidjson::Value& val) +void GetTxGlobalOutputIndices::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, output_indices, output_indices); } -rapidjson::Value SendRawTx::Request::toJson(rapidjson::Document& doc) const +void SendRawTx::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, tx, tx); - INSERT_INTO_JSON_OBJECT(val, doc, relay, relay); - - return val; + INSERT_INTO_JSON_OBJECT(dest, tx, tx); + INSERT_INTO_JSON_OBJECT(dest, relay, relay); } -void SendRawTx::Request::fromJson(rapidjson::Value& val) +void SendRawTx::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, tx, tx); GET_FROM_JSON_OBJECT(val, relay, relay); } -rapidjson::Value SendRawTx::Response::toJson(rapidjson::Document& doc) const +void SendRawTx::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, relayed, relayed); - - return val; + INSERT_INTO_JSON_OBJECT(dest, relayed, relayed); } -void SendRawTx::Response::fromJson(rapidjson::Value& val) +void SendRawTx::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, relayed, relayed); } -rapidjson::Value SendRawTxHex::Request::toJson(rapidjson::Document& doc) const +void SendRawTxHex::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, tx_as_hex, tx_as_hex); - INSERT_INTO_JSON_OBJECT(val, doc, relay, relay); - - return val; + INSERT_INTO_JSON_OBJECT(dest, tx_as_hex, tx_as_hex); + INSERT_INTO_JSON_OBJECT(dest, relay, relay); } -void SendRawTxHex::Request::fromJson(rapidjson::Value& val) +void SendRawTxHex::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, tx_as_hex, tx_as_hex); GET_FROM_JSON_OBJECT(val, relay, relay); } -rapidjson::Value StartMining::Request::toJson(rapidjson::Document& doc) const +void StartMining::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, miner_address, miner_address); - INSERT_INTO_JSON_OBJECT(val, doc, threads_count, threads_count); - INSERT_INTO_JSON_OBJECT(val, doc, do_background_mining, do_background_mining); - INSERT_INTO_JSON_OBJECT(val, doc, ignore_battery, ignore_battery); - - return val; + INSERT_INTO_JSON_OBJECT(dest, miner_address, miner_address); + INSERT_INTO_JSON_OBJECT(dest, threads_count, threads_count); + INSERT_INTO_JSON_OBJECT(dest, do_background_mining, do_background_mining); + INSERT_INTO_JSON_OBJECT(dest, ignore_battery, ignore_battery); } -void StartMining::Request::fromJson(rapidjson::Value& val) +void StartMining::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, miner_address, miner_address); GET_FROM_JSON_OBJECT(val, threads_count, threads_count); @@ -329,58 +225,46 @@ void StartMining::Request::fromJson(rapidjson::Value& val) GET_FROM_JSON_OBJECT(val, ignore_battery, ignore_battery); } -rapidjson::Value StartMining::Response::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void StartMining::Response::doToJson(rapidjson::Writer& dest) const +{} -void StartMining::Response::fromJson(rapidjson::Value& val) +void StartMining::Response::fromJson(const rapidjson::Value& val) { } -rapidjson::Value StopMining::Request::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void StopMining::Request::doToJson(rapidjson::Writer& dest) const +{} -void StopMining::Request::fromJson(rapidjson::Value& val) +void StopMining::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value StopMining::Response::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void StopMining::Response::doToJson(rapidjson::Writer& dest) const +{} -void StopMining::Response::fromJson(rapidjson::Value& val) +void StopMining::Response::fromJson(const rapidjson::Value& val) { } -rapidjson::Value MiningStatus::Request::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void MiningStatus::Request::doToJson(rapidjson::Writer& dest) const +{} -void MiningStatus::Request::fromJson(rapidjson::Value& val) +void MiningStatus::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value MiningStatus::Response::toJson(rapidjson::Document& doc) const +void MiningStatus::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, active, active); - INSERT_INTO_JSON_OBJECT(val, doc, speed, speed); - INSERT_INTO_JSON_OBJECT(val, doc, threads_count, threads_count); - INSERT_INTO_JSON_OBJECT(val, doc, address, address); - INSERT_INTO_JSON_OBJECT(val, doc, is_background_mining_enabled, is_background_mining_enabled); - - return val; + INSERT_INTO_JSON_OBJECT(dest, active, active); + INSERT_INTO_JSON_OBJECT(dest, speed, speed); + INSERT_INTO_JSON_OBJECT(dest, threads_count, threads_count); + INSERT_INTO_JSON_OBJECT(dest, address, address); + INSERT_INTO_JSON_OBJECT(dest, is_background_mining_enabled, is_background_mining_enabled); } -void MiningStatus::Response::fromJson(rapidjson::Value& val) +void MiningStatus::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, active, active); GET_FROM_JSON_OBJECT(val, speed, speed); @@ -390,318 +274,230 @@ void MiningStatus::Response::fromJson(rapidjson::Value& val) } -rapidjson::Value GetInfo::Request::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void GetInfo::Request::doToJson(rapidjson::Writer& dest) const +{} -void GetInfo::Request::fromJson(rapidjson::Value& val) +void GetInfo::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetInfo::Response::toJson(rapidjson::Document& doc) const +void GetInfo::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, info, info); - - return val; + INSERT_INTO_JSON_OBJECT(dest, info, info); } -void GetInfo::Response::fromJson(rapidjson::Value& val) +void GetInfo::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, info, info); } -rapidjson::Value SaveBC::Request::toJson(rapidjson::Document& doc) const -{ - auto val = Message::toJson(doc); +void SaveBC::Request::doToJson(rapidjson::Writer& dest) const +{} - return val; -} - -void SaveBC::Request::fromJson(rapidjson::Value& val) +void SaveBC::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value SaveBC::Response::toJson(rapidjson::Document& doc) const -{ - auto val = Message::toJson(doc); +void SaveBC::Response::doToJson(rapidjson::Writer& dest) const +{} - return val; -} - -void SaveBC::Response::fromJson(rapidjson::Value& val) +void SaveBC::Response::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetBlockHash::Request::toJson(rapidjson::Document& doc) const +void GetBlockHash::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, height, height); - - return val; + INSERT_INTO_JSON_OBJECT(dest, height, height); } -void GetBlockHash::Request::fromJson(rapidjson::Value& val) +void GetBlockHash::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, height, height); } -rapidjson::Value GetBlockHash::Response::toJson(rapidjson::Document& doc) const +void GetBlockHash::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, hash, hash); - - return val; + INSERT_INTO_JSON_OBJECT(dest, hash, hash); } -void GetBlockHash::Response::fromJson(rapidjson::Value& val) +void GetBlockHash::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, hash, hash); } -rapidjson::Value GetLastBlockHeader::Request::toJson(rapidjson::Document& doc) const -{ - auto val = Message::toJson(doc); +void GetLastBlockHeader::Request::doToJson(rapidjson::Writer& dest) const +{} - return val; -} - -void GetLastBlockHeader::Request::fromJson(rapidjson::Value& val) +void GetLastBlockHeader::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetLastBlockHeader::Response::toJson(rapidjson::Document& doc) const +void GetLastBlockHeader::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, header, header); - - return val; + INSERT_INTO_JSON_OBJECT(dest, header, header); } -void GetLastBlockHeader::Response::fromJson(rapidjson::Value& val) +void GetLastBlockHeader::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, header, header); } -rapidjson::Value GetBlockHeaderByHash::Request::toJson(rapidjson::Document& doc) const +void GetBlockHeaderByHash::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, hash, hash); - - return val; + INSERT_INTO_JSON_OBJECT(dest, hash, hash); } -void GetBlockHeaderByHash::Request::fromJson(rapidjson::Value& val) +void GetBlockHeaderByHash::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, hash, hash); } -rapidjson::Value GetBlockHeaderByHash::Response::toJson(rapidjson::Document& doc) const +void GetBlockHeaderByHash::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, header, header); - - return val; + INSERT_INTO_JSON_OBJECT(dest, header, header); } -void GetBlockHeaderByHash::Response::fromJson(rapidjson::Value& val) +void GetBlockHeaderByHash::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, header, header); } -rapidjson::Value GetBlockHeaderByHeight::Request::toJson(rapidjson::Document& doc) const +void GetBlockHeaderByHeight::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, height, height); - - return val; + INSERT_INTO_JSON_OBJECT(dest, height, height); } -void GetBlockHeaderByHeight::Request::fromJson(rapidjson::Value& val) +void GetBlockHeaderByHeight::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, height, height); } -rapidjson::Value GetBlockHeaderByHeight::Response::toJson(rapidjson::Document& doc) const +void GetBlockHeaderByHeight::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, header, header); - - return val; + INSERT_INTO_JSON_OBJECT(dest, header, header); } -void GetBlockHeaderByHeight::Response::fromJson(rapidjson::Value& val) +void GetBlockHeaderByHeight::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, header, header); } -rapidjson::Value GetBlockHeadersByHeight::Request::toJson(rapidjson::Document& doc) const +void GetBlockHeadersByHeight::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, heights, heights); - - return val; + INSERT_INTO_JSON_OBJECT(dest, heights, heights); } -void GetBlockHeadersByHeight::Request::fromJson(rapidjson::Value& val) +void GetBlockHeadersByHeight::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, heights, heights); } -rapidjson::Value GetBlockHeadersByHeight::Response::toJson(rapidjson::Document& doc) const +void GetBlockHeadersByHeight::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, headers, headers); - - return val; + INSERT_INTO_JSON_OBJECT(dest, headers, headers); } -void GetBlockHeadersByHeight::Response::fromJson(rapidjson::Value& val) +void GetBlockHeadersByHeight::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, headers, headers); } -rapidjson::Value GetPeerList::Request::toJson(rapidjson::Document& doc) const -{ - auto val = Message::toJson(doc); +void GetPeerList::Request::doToJson(rapidjson::Writer& dest) const +{} - return val; -} - -void GetPeerList::Request::fromJson(rapidjson::Value& val) +void GetPeerList::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetPeerList::Response::toJson(rapidjson::Document& doc) const +void GetPeerList::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, white_list, white_list); - INSERT_INTO_JSON_OBJECT(val, doc, gray_list, gray_list); - - return val; + INSERT_INTO_JSON_OBJECT(dest, white_list, white_list); + INSERT_INTO_JSON_OBJECT(dest, gray_list, gray_list); } -void GetPeerList::Response::fromJson(rapidjson::Value& val) +void GetPeerList::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, white_list, white_list); GET_FROM_JSON_OBJECT(val, gray_list, gray_list); } -rapidjson::Value SetLogLevel::Request::toJson(rapidjson::Document& doc) const +void SetLogLevel::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - auto& al = doc.GetAllocator(); - - val.AddMember("level", level, al); - - return val; + INSERT_INTO_JSON_OBJECT(dest, level, level); } -void SetLogLevel::Request::fromJson(rapidjson::Value& val) +void SetLogLevel::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, level, level); } -rapidjson::Value SetLogLevel::Response::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void SetLogLevel::Response::doToJson(rapidjson::Writer& dest) const +{} -void SetLogLevel::Response::fromJson(rapidjson::Value& val) +void SetLogLevel::Response::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetTransactionPool::Request::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void GetTransactionPool::Request::doToJson(rapidjson::Writer& dest) const +{} -void GetTransactionPool::Request::fromJson(rapidjson::Value& val) +void GetTransactionPool::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetTransactionPool::Response::toJson(rapidjson::Document& doc) const +void GetTransactionPool::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, transactions, transactions); - INSERT_INTO_JSON_OBJECT(val, doc, key_images, key_images); - - return val; + INSERT_INTO_JSON_OBJECT(dest, transactions, transactions); + INSERT_INTO_JSON_OBJECT(dest, key_images, key_images); } -void GetTransactionPool::Response::fromJson(rapidjson::Value& val) +void GetTransactionPool::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, transactions, transactions); GET_FROM_JSON_OBJECT(val, key_images, key_images); } -rapidjson::Value HardForkInfo::Request::toJson(rapidjson::Document& doc) const +void HardForkInfo::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, version, version); - - return val; + INSERT_INTO_JSON_OBJECT(dest, version, version); } -void HardForkInfo::Request::fromJson(rapidjson::Value& val) +void HardForkInfo::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, version, version); } -rapidjson::Value HardForkInfo::Response::toJson(rapidjson::Document& doc) const +void HardForkInfo::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, info, info); - - return val; + INSERT_INTO_JSON_OBJECT(dest, info, info); } -void HardForkInfo::Response::fromJson(rapidjson::Value& val) +void HardForkInfo::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, info, info); } -rapidjson::Value GetOutputHistogram::Request::toJson(rapidjson::Document& doc) const +void GetOutputHistogram::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, amounts, amounts); - INSERT_INTO_JSON_OBJECT(val, doc, min_count, min_count); - INSERT_INTO_JSON_OBJECT(val, doc, max_count, max_count); - INSERT_INTO_JSON_OBJECT(val, doc, unlocked, unlocked); - INSERT_INTO_JSON_OBJECT(val, doc, recent_cutoff, recent_cutoff); - - return val; + INSERT_INTO_JSON_OBJECT(dest, amounts, amounts); + INSERT_INTO_JSON_OBJECT(dest, min_count, min_count); + INSERT_INTO_JSON_OBJECT(dest, max_count, max_count); + INSERT_INTO_JSON_OBJECT(dest, unlocked, unlocked); + INSERT_INTO_JSON_OBJECT(dest, recent_cutoff, recent_cutoff); } -void GetOutputHistogram::Request::fromJson(rapidjson::Value& val) +void GetOutputHistogram::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, amounts, amounts); GET_FROM_JSON_OBJECT(val, min_count, min_count); @@ -710,100 +506,74 @@ void GetOutputHistogram::Request::fromJson(rapidjson::Value& val) GET_FROM_JSON_OBJECT(val, recent_cutoff, recent_cutoff); } -rapidjson::Value GetOutputHistogram::Response::toJson(rapidjson::Document& doc) const +void GetOutputHistogram::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, histogram, histogram); - - return val; + INSERT_INTO_JSON_OBJECT(dest, histogram, histogram); } -void GetOutputHistogram::Response::fromJson(rapidjson::Value& val) +void GetOutputHistogram::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, histogram, histogram); } -rapidjson::Value GetOutputKeys::Request::toJson(rapidjson::Document& doc) const +void GetOutputKeys::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, outputs, outputs); - - return val; + INSERT_INTO_JSON_OBJECT(dest, outputs, outputs); } -void GetOutputKeys::Request::fromJson(rapidjson::Value& val) +void GetOutputKeys::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, outputs, outputs); } -rapidjson::Value GetOutputKeys::Response::toJson(rapidjson::Document& doc) const +void GetOutputKeys::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, keys, keys); - - return val; + INSERT_INTO_JSON_OBJECT(dest, keys, keys); } -void GetOutputKeys::Response::fromJson(rapidjson::Value& val) +void GetOutputKeys::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, keys, keys); } -rapidjson::Value GetRPCVersion::Request::toJson(rapidjson::Document& doc) const -{ - return Message::toJson(doc); -} +void GetRPCVersion::Request::doToJson(rapidjson::Writer& dest) const +{} -void GetRPCVersion::Request::fromJson(rapidjson::Value& val) +void GetRPCVersion::Request::fromJson(const rapidjson::Value& val) { } -rapidjson::Value GetRPCVersion::Response::toJson(rapidjson::Document& doc) const +void GetRPCVersion::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, version, version); - - return val; + INSERT_INTO_JSON_OBJECT(dest, version, version); } -void GetRPCVersion::Response::fromJson(rapidjson::Value& val) +void GetRPCVersion::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, version, version); } -rapidjson::Value GetFeeEstimate::Request::toJson(rapidjson::Document& doc) const +void GetFeeEstimate::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, num_grace_blocks, num_grace_blocks); - - return val; + INSERT_INTO_JSON_OBJECT(dest, num_grace_blocks, num_grace_blocks); } -void GetFeeEstimate::Request::fromJson(rapidjson::Value& val) +void GetFeeEstimate::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, num_grace_blocks, num_grace_blocks); } -rapidjson::Value GetFeeEstimate::Response::toJson(rapidjson::Document& doc) const +void GetFeeEstimate::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, estimated_base_fee, estimated_base_fee); - INSERT_INTO_JSON_OBJECT(val, doc, fee_mask, fee_mask); - INSERT_INTO_JSON_OBJECT(val, doc, size_scale, size_scale); - INSERT_INTO_JSON_OBJECT(val, doc, hard_fork_version, hard_fork_version); - - return val; + INSERT_INTO_JSON_OBJECT(dest, estimated_base_fee, estimated_base_fee); + INSERT_INTO_JSON_OBJECT(dest, fee_mask, fee_mask); + INSERT_INTO_JSON_OBJECT(dest, size_scale, size_scale); + INSERT_INTO_JSON_OBJECT(dest, hard_fork_version, hard_fork_version); } -void GetFeeEstimate::Response::fromJson(rapidjson::Value& val) +void GetFeeEstimate::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, estimated_base_fee, estimated_base_fee); GET_FROM_JSON_OBJECT(val, fee_mask, fee_mask); @@ -811,19 +581,15 @@ void GetFeeEstimate::Response::fromJson(rapidjson::Value& val) GET_FROM_JSON_OBJECT(val, hard_fork_version, hard_fork_version); } -rapidjson::Value GetOutputDistribution::Request::toJson(rapidjson::Document& doc) const +void GetOutputDistribution::Request::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, amounts, amounts); - INSERT_INTO_JSON_OBJECT(val, doc, from_height, from_height); - INSERT_INTO_JSON_OBJECT(val, doc, to_height, to_height); - INSERT_INTO_JSON_OBJECT(val, doc, cumulative, cumulative); - - return val; + INSERT_INTO_JSON_OBJECT(dest, amounts, amounts); + INSERT_INTO_JSON_OBJECT(dest, from_height, from_height); + INSERT_INTO_JSON_OBJECT(dest, to_height, to_height); + INSERT_INTO_JSON_OBJECT(dest, cumulative, cumulative); } -void GetOutputDistribution::Request::fromJson(rapidjson::Value& val) +void GetOutputDistribution::Request::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, amounts, amounts); GET_FROM_JSON_OBJECT(val, from_height, from_height); @@ -831,17 +597,13 @@ void GetOutputDistribution::Request::fromJson(rapidjson::Value& val) GET_FROM_JSON_OBJECT(val, cumulative, cumulative); } -rapidjson::Value GetOutputDistribution::Response::toJson(rapidjson::Document& doc) const +void GetOutputDistribution::Response::doToJson(rapidjson::Writer& dest) const { - auto val = Message::toJson(doc); - - INSERT_INTO_JSON_OBJECT(val, doc, status, status); - INSERT_INTO_JSON_OBJECT(val, doc, distributions, distributions); - - return val; + INSERT_INTO_JSON_OBJECT(dest, status, status); + INSERT_INTO_JSON_OBJECT(dest, distributions, distributions); } -void GetOutputDistribution::Response::fromJson(rapidjson::Value& val) +void GetOutputDistribution::Response::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, status, status); GET_FROM_JSON_OBJECT(val, distributions, distributions); diff --git a/src/rpc/daemon_messages.h b/src/rpc/daemon_messages.h index c0d9aed0a..bb5059cdc 100644 --- a/src/rpc/daemon_messages.h +++ b/src/rpc/daemon_messages.h @@ -28,6 +28,8 @@ #pragma once +#include +#include #include #include @@ -40,26 +42,25 @@ #define BEGIN_RPC_MESSAGE_CLASS(classname) \ class classname \ { \ - public: \ - static const char* const name; + public: #define BEGIN_RPC_MESSAGE_REQUEST \ - class Request : public Message \ + class Request final : public Message \ { \ public: \ Request() { } \ ~Request() { } \ - rapidjson::Value toJson(rapidjson::Document& doc) const; \ - void fromJson(rapidjson::Value& val); + void doToJson(rapidjson::Writer& dest) const override final; \ + void fromJson(const rapidjson::Value& val) override final; #define BEGIN_RPC_MESSAGE_RESPONSE \ - class Response : public Message \ + class Response final : public Message \ { \ public: \ Response() { } \ ~Response() { } \ - rapidjson::Value toJson(rapidjson::Document& doc) const; \ - void fromJson(rapidjson::Value& val); + void doToJson(rapidjson::Writer& dest) const override final; \ + void fromJson(const rapidjson::Value& val) override final; #define END_RPC_MESSAGE_REQUEST }; #define END_RPC_MESSAGE_RESPONSE }; diff --git a/src/rpc/message.cpp b/src/rpc/message.cpp index 158b58005..a3df7fb56 100644 --- a/src/rpc/message.cpp +++ b/src/rpc/message.cpp @@ -27,12 +27,10 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "message.h" + #include "daemon_rpc_version.h" #include "serialization/json_object.h" -#include "rapidjson/writer.h" -#include "rapidjson/stringbuffer.h" - namespace cryptonote { @@ -54,60 +52,23 @@ constexpr const char params_field[] = "params"; constexpr const char result_field[] = "result"; } -rapidjson::Value Message::toJson(rapidjson::Document& doc) const +void Message::toJson(rapidjson::Writer& dest) const { - rapidjson::Value val(rapidjson::kObjectType); - - auto& al = doc.GetAllocator(); - - val.AddMember("status", rapidjson::StringRef(status.c_str()), al); - val.AddMember("error_details", rapidjson::StringRef(error_details.c_str()), al); - INSERT_INTO_JSON_OBJECT(val, doc, rpc_version, DAEMON_RPC_VERSION_ZMQ); - - return val; + dest.StartObject(); + INSERT_INTO_JSON_OBJECT(dest, status, status); + INSERT_INTO_JSON_OBJECT(dest, error_details, error_details); + INSERT_INTO_JSON_OBJECT(dest, rpc_version, DAEMON_RPC_VERSION_ZMQ); + doToJson(dest); + dest.EndObject(); } -void Message::fromJson(rapidjson::Value& val) +void Message::fromJson(const rapidjson::Value& val) { GET_FROM_JSON_OBJECT(val, status, status); GET_FROM_JSON_OBJECT(val, error_details, error_details); GET_FROM_JSON_OBJECT(val, rpc_version, rpc_version); } - -FullMessage::FullMessage(const std::string& request, Message* message) -{ - doc.SetObject(); - - doc.AddMember(method_field, rapidjson::StringRef(request.c_str()), doc.GetAllocator()); - doc.AddMember(params_field, message->toJson(doc), doc.GetAllocator()); - - // required by JSON-RPC 2.0 spec - doc.AddMember("jsonrpc", rapidjson::Value("2.0"), doc.GetAllocator()); -} - -FullMessage::FullMessage(Message* message) -{ - doc.SetObject(); - - // required by JSON-RPC 2.0 spec - doc.AddMember("jsonrpc", "2.0", doc.GetAllocator()); - - if (message->status == Message::STATUS_OK) - { - doc.AddMember(result_field, message->toJson(doc), doc.GetAllocator()); - } - else - { - cryptonote::rpc::error err; - - err.error_str = message->status; - err.message = message->error_details; - - INSERT_INTO_JSON_OBJECT(doc, doc, error, err); - } -} - FullMessage::FullMessage(const std::string& json_string, bool request) { doc.Parse(json_string.c_str()); @@ -132,30 +93,13 @@ FullMessage::FullMessage(const std::string& json_string, bool request) } } -std::string FullMessage::getJson() -{ - - if (!doc.HasMember(id_field)) - { - doc.AddMember(id_field, rapidjson::Value("unused"), doc.GetAllocator()); - } - - rapidjson::StringBuffer buf; - - rapidjson::Writer writer(buf); - - doc.Accept(writer); - - return std::string(buf.GetString(), buf.GetSize()); -} - std::string FullMessage::getRequestType() const { OBJECT_HAS_MEMBER_OR_THROW(doc, method_field) return doc[method_field].GetString(); } -rapidjson::Value& FullMessage::getMessage() +const rapidjson::Value& FullMessage::getMessage() const { if (doc.HasMember(params_field)) { @@ -174,30 +118,15 @@ rapidjson::Value& FullMessage::getMessage() rapidjson::Value FullMessage::getMessageCopy() { - rapidjson::Value& val = getMessage(); - - return rapidjson::Value(val, doc.GetAllocator()); + return rapidjson::Value(getMessage(), doc.GetAllocator()); } -rapidjson::Value& FullMessage::getID() +const rapidjson::Value& FullMessage::getID() const { OBJECT_HAS_MEMBER_OR_THROW(doc, id_field) return doc[id_field]; } -void FullMessage::setID(rapidjson::Value& id) -{ - auto itr = doc.FindMember(id_field); - if (itr != doc.MemberEnd()) - { - itr->value = id; - } - else - { - doc.AddMember(id_field, id, doc.GetAllocator()); - } -} - cryptonote::rpc::error FullMessage::getError() { cryptonote::rpc::error err; @@ -211,82 +140,89 @@ cryptonote::rpc::error FullMessage::getError() return err; } -FullMessage FullMessage::requestMessage(const std::string& request, Message* message) +std::string FullMessage::getRequest(const std::string& request, const Message& message, const unsigned id) { - return FullMessage(request, message); + rapidjson::StringBuffer buffer; + { + rapidjson::Writer dest{buffer}; + + dest.StartObject(); + INSERT_INTO_JSON_OBJECT(dest, jsonrpc, (boost::string_ref{"2.0", 3})); + + dest.Key(id_field); + json::toJsonValue(dest, id); + + dest.Key(method_field); + json::toJsonValue(dest, request); + + dest.Key(params_field); + message.toJson(dest); + + dest.EndObject(); + + if (!dest.IsComplete()) + throw std::logic_error{"Invalid JSON tree generated"}; + } + return std::string{buffer.GetString(), buffer.GetSize()}; } -FullMessage FullMessage::requestMessage(const std::string& request, Message* message, rapidjson::Value& id) + +std::string FullMessage::getResponse(const Message& message, const rapidjson::Value& id) { - auto mes = requestMessage(request, message); - mes.setID(id); - return mes; -} + rapidjson::StringBuffer buffer; + { + rapidjson::Writer dest{buffer}; -FullMessage FullMessage::responseMessage(Message* message) -{ - return FullMessage(message); -} + dest.StartObject(); + INSERT_INTO_JSON_OBJECT(dest, jsonrpc, (boost::string_ref{"2.0", 3})); -FullMessage FullMessage::responseMessage(Message* message, rapidjson::Value& id) -{ - auto mes = responseMessage(message); - mes.setID(id); - return mes; -} + dest.Key(id_field); + json::toJsonValue(dest, id); -FullMessage* FullMessage::timeoutMessage() -{ - auto *full_message = new FullMessage(); + if (message.status == Message::STATUS_OK) + { + dest.Key(result_field); + message.toJson(dest); + } + else + { + cryptonote::rpc::error err; - auto& doc = full_message->doc; - auto& al = full_message->doc.GetAllocator(); + err.error_str = message.status; + err.message = message.error_details; - doc.SetObject(); + INSERT_INTO_JSON_OBJECT(dest, error, err); + } + dest.EndObject(); - // required by JSON-RPC 2.0 spec - doc.AddMember("jsonrpc", "2.0", al); - - cryptonote::rpc::error err; - - err.error_str = "RPC request timed out."; - INSERT_INTO_JSON_OBJECT(doc, doc, err, err); - - return full_message; + if (!dest.IsComplete()) + throw std::logic_error{"Invalid JSON tree generated"}; + } + return std::string{buffer.GetString(), buffer.GetSize()}; } // convenience functions for bad input std::string BAD_REQUEST(const std::string& request) { - Message fail; - fail.status = Message::STATUS_BAD_REQUEST; - fail.error_details = std::string("\"") + request + "\" is not a valid request."; - - FullMessage fail_response = FullMessage::responseMessage(&fail); - - return fail_response.getJson(); + rapidjson::Value invalid; + return BAD_REQUEST(request, invalid); } -std::string BAD_REQUEST(const std::string& request, rapidjson::Value& id) +std::string BAD_REQUEST(const std::string& request, const rapidjson::Value& id) { Message fail; fail.status = Message::STATUS_BAD_REQUEST; fail.error_details = std::string("\"") + request + "\" is not a valid request."; - - FullMessage fail_response = FullMessage::responseMessage(&fail, id); - - return fail_response.getJson(); + return FullMessage::getResponse(fail, id); } std::string BAD_JSON(const std::string& error_details) { + rapidjson::Value invalid; Message fail; fail.status = Message::STATUS_BAD_JSON; fail.error_details = error_details; - - FullMessage fail_response = FullMessage::responseMessage(&fail); - - return fail_response.getJson(); + return FullMessage::getResponse(fail, invalid); } diff --git a/src/rpc/message.h b/src/rpc/message.h index 2b7b61ab3..4cbc84fe4 100644 --- a/src/rpc/message.h +++ b/src/rpc/message.h @@ -28,27 +28,12 @@ #pragma once -#include "rapidjson/document.h" -#include "rpc/message_data_structs.h" +#include +#include +#include #include -/* I normally hate using macros, but in this case it would be untenably - * verbose to not use a macro. This macro saves the trouble of explicitly - * writing the below if block for every single RPC call. - */ -#define REQ_RESP_TYPES_MACRO( runtime_str, type, reqjson, resp_message_ptr, handler) \ - \ - if (runtime_str == type::name) \ - { \ - type::Request reqvar; \ - type::Response *respvar = new type::Response(); \ - \ - reqvar.fromJson(reqjson); \ - \ - handler(reqvar, *respvar); \ - \ - resp_message_ptr = respvar; \ - } +#include "rpc/message_data_structs.h" namespace cryptonote { @@ -58,6 +43,9 @@ namespace rpc class Message { + virtual void doToJson(rapidjson::Writer& dest) const + {} + public: static const char* STATUS_OK; static const char* STATUS_RETRY; @@ -69,9 +57,9 @@ namespace rpc virtual ~Message() { } - virtual rapidjson::Value toJson(rapidjson::Document& doc) const; + void toJson(rapidjson::Writer& dest) const; - virtual void fromJson(rapidjson::Value& val); + virtual void fromJson(const rapidjson::Value& val); std::string status; std::string error_details; @@ -87,27 +75,18 @@ namespace rpc FullMessage(const std::string& json_string, bool request=false); - std::string getJson(); - std::string getRequestType() const; - rapidjson::Value& getMessage(); + const rapidjson::Value& getMessage() const; rapidjson::Value getMessageCopy(); - rapidjson::Value& getID(); - - void setID(rapidjson::Value& id); + const rapidjson::Value& getID() const; cryptonote::rpc::error getError(); - static FullMessage requestMessage(const std::string& request, Message* message); - static FullMessage requestMessage(const std::string& request, Message* message, rapidjson::Value& id); - - static FullMessage responseMessage(Message* message); - static FullMessage responseMessage(Message* message, rapidjson::Value& id); - - static FullMessage* timeoutMessage(); + static std::string getRequest(const std::string& request, const Message& message, unsigned id); + static std::string getResponse(const Message& message, const rapidjson::Value& id); private: FullMessage() = default; @@ -121,7 +100,7 @@ namespace rpc // convenience functions for bad input std::string BAD_REQUEST(const std::string& request); - std::string BAD_REQUEST(const std::string& request, rapidjson::Value& id); + std::string BAD_REQUEST(const std::string& request, const rapidjson::Value& id); std::string BAD_JSON(const std::string& error_details); diff --git a/src/serialization/CMakeLists.txt b/src/serialization/CMakeLists.txt index 28b775a37..0b231f156 100644 --- a/src/serialization/CMakeLists.txt +++ b/src/serialization/CMakeLists.txt @@ -44,6 +44,7 @@ target_link_libraries(serialization LINK_PRIVATE cryptonote_core cryptonote_protocol + epee ${Boost_CHRONO_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_SYSTEM_LIBRARY} diff --git a/src/serialization/json_object.cpp b/src/serialization/json_object.cpp index ea67209dc..e98ba0483 100644 --- a/src/serialization/json_object.cpp +++ b/src/serialization/json_object.cpp @@ -109,9 +109,14 @@ namespace } } -void toJsonValue(rapidjson::Document& doc, const std::string& i, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const rapidjson::Value& src) { - val = rapidjson::Value(i.c_str(), doc.GetAllocator()); + src.Accept(dest); +} + +void toJsonValue(rapidjson::Writer& dest, const boost::string_ref i) +{ + dest.String(i.data(), i.size()); } void fromJsonValue(const rapidjson::Value& val, std::string& str) @@ -124,9 +129,9 @@ void fromJsonValue(const rapidjson::Value& val, std::string& str) str = val.GetString(); } -void toJsonValue(rapidjson::Document& doc, bool i, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, bool i) { - val.SetBool(i); + dest.Bool(i); } void fromJsonValue(const rapidjson::Value& val, bool& b) @@ -163,9 +168,9 @@ void fromJsonValue(const rapidjson::Value& val, short& i) to_int(val, i); } -void toJsonValue(rapidjson::Document& doc, const unsigned int i, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const unsigned int i) { - val = rapidjson::Value(i); + dest.Uint(i); } void fromJsonValue(const rapidjson::Value& val, unsigned int& i) @@ -173,9 +178,9 @@ void fromJsonValue(const rapidjson::Value& val, unsigned int& i) to_uint(val, i); } -void toJsonValue(rapidjson::Document& doc, const int i, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const int i) { - val = rapidjson::Value(i); + dest.Int(i); } void fromJsonValue(const rapidjson::Value& val, int& i) @@ -183,10 +188,10 @@ void fromJsonValue(const rapidjson::Value& val, int& i) to_int(val, i); } -void toJsonValue(rapidjson::Document& doc, const unsigned long long i, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const unsigned long long i) { - static_assert(!precision_loss(), "precision loss"); - val = rapidjson::Value(std::uint64_t(i)); + static_assert(std::numeric_limits::max() <= std::numeric_limits::max(), "bad uint64 conversion"); + dest.Uint64(i); } void fromJsonValue(const rapidjson::Value& val, unsigned long long& i) @@ -194,10 +199,11 @@ void fromJsonValue(const rapidjson::Value& val, unsigned long long& i) to_uint64(val, i); } -void toJsonValue(rapidjson::Document& doc, const long long i, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const long long i) { - static_assert(!precision_loss(), "precision loss"); - val = rapidjson::Value(std::int64_t(i)); + static_assert(std::numeric_limits::min() <= std::numeric_limits::min(), "bad int64 conversion"); + static_assert(std::numeric_limits::max() <= std::numeric_limits::max(), "bad int64 conversion"); + dest.Int64(i); } void fromJsonValue(const rapidjson::Value& val, long long& i) @@ -215,17 +221,19 @@ void fromJsonValue(const rapidjson::Value& val, long& i) to_int64(val, i); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::transaction& tx, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::transaction& tx) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, version, tx.version); - INSERT_INTO_JSON_OBJECT(val, doc, unlock_time, tx.unlock_time); - INSERT_INTO_JSON_OBJECT(val, doc, inputs, tx.vin); - INSERT_INTO_JSON_OBJECT(val, doc, outputs, tx.vout); - INSERT_INTO_JSON_OBJECT(val, doc, extra, tx.extra); - INSERT_INTO_JSON_OBJECT(val, doc, signatures, tx.signatures); - INSERT_INTO_JSON_OBJECT(val, doc, ringct, tx.rct_signatures); + INSERT_INTO_JSON_OBJECT(dest, version, tx.version); + INSERT_INTO_JSON_OBJECT(dest, unlock_time, tx.unlock_time); + INSERT_INTO_JSON_OBJECT(dest, inputs, tx.vin); + INSERT_INTO_JSON_OBJECT(dest, outputs, tx.vout); + INSERT_INTO_JSON_OBJECT(dest, extra, tx.extra); + INSERT_INTO_JSON_OBJECT(dest, signatures, tx.signatures); + INSERT_INTO_JSON_OBJECT(dest, ringct, tx.rct_signatures); + + dest.EndObject(); } @@ -245,17 +253,19 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::transaction& tx) GET_FROM_JSON_OBJECT(val, tx.rct_signatures, ringct); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::block& b, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::block& b) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, major_version, b.major_version); - INSERT_INTO_JSON_OBJECT(val, doc, minor_version, b.minor_version); - INSERT_INTO_JSON_OBJECT(val, doc, timestamp, b.timestamp); - INSERT_INTO_JSON_OBJECT(val, doc, prev_id, b.prev_id); - INSERT_INTO_JSON_OBJECT(val, doc, nonce, b.nonce); - INSERT_INTO_JSON_OBJECT(val, doc, miner_tx, b.miner_tx); - INSERT_INTO_JSON_OBJECT(val, doc, tx_hashes, b.tx_hashes); + INSERT_INTO_JSON_OBJECT(dest, major_version, b.major_version); + INSERT_INTO_JSON_OBJECT(dest, minor_version, b.minor_version); + INSERT_INTO_JSON_OBJECT(dest, timestamp, b.timestamp); + INSERT_INTO_JSON_OBJECT(dest, prev_id, b.prev_id); + INSERT_INTO_JSON_OBJECT(dest, nonce, b.nonce); + INSERT_INTO_JSON_OBJECT(dest, miner_tx, b.miner_tx); + INSERT_INTO_JSON_OBJECT(dest, tx_hashes, b.tx_hashes); + + dest.EndObject(); } @@ -275,35 +285,34 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::block& b) GET_FROM_JSON_OBJECT(val, b.tx_hashes, tx_hashes); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_v& txin, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_v& txin) { - val.SetObject(); - + dest.StartObject(); struct add_input { using result_type = void; - rapidjson::Document& doc; - rapidjson::Value& val; + rapidjson::Writer& dest; void operator()(cryptonote::txin_to_key const& input) const { - INSERT_INTO_JSON_OBJECT(val, doc, to_key, input); + INSERT_INTO_JSON_OBJECT(dest, to_key, input); } void operator()(cryptonote::txin_gen const& input) const { - INSERT_INTO_JSON_OBJECT(val, doc, gen, input); + INSERT_INTO_JSON_OBJECT(dest, gen, input); } void operator()(cryptonote::txin_to_script const& input) const { - INSERT_INTO_JSON_OBJECT(val, doc, to_script, input); + INSERT_INTO_JSON_OBJECT(dest, to_script, input); } void operator()(cryptonote::txin_to_scripthash const& input) const { - INSERT_INTO_JSON_OBJECT(val, doc, to_scripthash, input); + INSERT_INTO_JSON_OBJECT(dest, to_scripthash, input); } }; - boost::apply_visitor(add_input{doc, val}, txin); + boost::apply_visitor(add_input{dest}, txin); + dest.EndObject(); } @@ -348,14 +357,15 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin) } } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_gen& txin, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_gen& txin) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, height, txin.height); + INSERT_INTO_JSON_OBJECT(dest, height, txin.height); + + dest.EndObject(); } - void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin) { if (!val.IsObject()) @@ -366,13 +376,15 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin) GET_FROM_JSON_OBJECT(val, txin.height, height); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_script& txin, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_to_script& txin) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, prev, txin.prev); - INSERT_INTO_JSON_OBJECT(val, doc, prevout, txin.prevout); - INSERT_INTO_JSON_OBJECT(val, doc, sigset, txin.sigset); + INSERT_INTO_JSON_OBJECT(dest, prev, txin.prev); + INSERT_INTO_JSON_OBJECT(dest, prevout, txin.prevout); + INSERT_INTO_JSON_OBJECT(dest, sigset, txin.sigset); + + dest.EndObject(); } @@ -388,14 +400,17 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_script& txin GET_FROM_JSON_OBJECT(val, txin.sigset, sigset); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_scripthash& txin, rapidjson::Value& val) -{ - val.SetObject(); - INSERT_INTO_JSON_OBJECT(val, doc, prev, txin.prev); - INSERT_INTO_JSON_OBJECT(val, doc, prevout, txin.prevout); - INSERT_INTO_JSON_OBJECT(val, doc, script, txin.script); - INSERT_INTO_JSON_OBJECT(val, doc, sigset, txin.sigset); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_to_scripthash& txin) +{ + dest.StartObject(); + + INSERT_INTO_JSON_OBJECT(dest, prev, txin.prev); + INSERT_INTO_JSON_OBJECT(dest, prevout, txin.prevout); + INSERT_INTO_JSON_OBJECT(dest, script, txin.script); + INSERT_INTO_JSON_OBJECT(dest, sigset, txin.sigset); + + dest.EndObject(); } @@ -412,16 +427,17 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_scripthash& GET_FROM_JSON_OBJECT(val, txin.sigset, sigset); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_key& txin, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_to_key& txin) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, amount, txin.amount); - INSERT_INTO_JSON_OBJECT(val, doc, key_offsets, txin.key_offsets); - INSERT_INTO_JSON_OBJECT(val, doc, key_image, txin.k_image); + INSERT_INTO_JSON_OBJECT(dest, amount, txin.amount); + INSERT_INTO_JSON_OBJECT(dest, key_offsets, txin.key_offsets); + INSERT_INTO_JSON_OBJECT(dest, key_image, txin.k_image); + + dest.EndObject(); } - void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_key& txin) { if (!val.IsObject()) @@ -434,15 +450,17 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_key& txin) GET_FROM_JSON_OBJECT(val, txin.k_image, key_image); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_script& txout, rapidjson::Value& val) + +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txout_to_script& txout) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, keys, txout.keys); - INSERT_INTO_JSON_OBJECT(val, doc, script, txout.script); + INSERT_INTO_JSON_OBJECT(dest, keys, txout.keys); + INSERT_INTO_JSON_OBJECT(dest, script, txout.script); + + dest.EndObject(); } - void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_script& txout) { if (!val.IsObject()) @@ -454,14 +472,16 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_script& txo GET_FROM_JSON_OBJECT(val, txout.script, script); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_scripthash& txout, rapidjson::Value& val) + +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txout_to_scripthash& txout) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, hash, txout.hash); + INSERT_INTO_JSON_OBJECT(dest, hash, txout.hash); + + dest.EndObject(); } - void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_scripthash& txout) { if (!val.IsObject()) @@ -472,14 +492,16 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_scripthash& GET_FROM_JSON_OBJECT(val, txout.hash, hash); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_key& txout, rapidjson::Value& val) + +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txout_to_key& txout) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, key, txout.key); + INSERT_INTO_JSON_OBJECT(dest, key, txout.key); + + dest.EndObject(); } - void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_key& txout) { if (!val.IsObject()) @@ -490,33 +512,32 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_key& txout) GET_FROM_JSON_OBJECT(val, txout.key, key); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_out& txout, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::tx_out& txout) { - val.SetObject(); - - INSERT_INTO_JSON_OBJECT(val, doc, amount, txout.amount); + dest.StartObject(); + INSERT_INTO_JSON_OBJECT(dest, amount, txout.amount); struct add_output { using result_type = void; - rapidjson::Document& doc; - rapidjson::Value& val; + rapidjson::Writer& dest; void operator()(cryptonote::txout_to_key const& output) const { - INSERT_INTO_JSON_OBJECT(val, doc, to_key, output); + INSERT_INTO_JSON_OBJECT(dest, to_key, output); } void operator()(cryptonote::txout_to_script const& output) const { - INSERT_INTO_JSON_OBJECT(val, doc, to_script, output); + INSERT_INTO_JSON_OBJECT(dest, to_script, output); } void operator()(cryptonote::txout_to_scripthash const& output) const { - INSERT_INTO_JSON_OBJECT(val, doc, to_scripthash, output); + INSERT_INTO_JSON_OBJECT(dest, to_scripthash, output); } }; - boost::apply_visitor(add_output{doc, val}, txout.target); + boost::apply_visitor(add_output{dest}, txout.target); + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout) @@ -559,37 +580,39 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout) } } -void toJsonValue(rapidjson::Document& doc, const cryptonote::connection_info& info, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::connection_info& info) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, incoming, info.incoming); - INSERT_INTO_JSON_OBJECT(val, doc, localhost, info.localhost); - INSERT_INTO_JSON_OBJECT(val, doc, local_ip, info.local_ip); - INSERT_INTO_JSON_OBJECT(val, doc, address_type, info.address_type); + INSERT_INTO_JSON_OBJECT(dest, incoming, info.incoming); + INSERT_INTO_JSON_OBJECT(dest, localhost, info.localhost); + INSERT_INTO_JSON_OBJECT(dest, local_ip, info.local_ip); + INSERT_INTO_JSON_OBJECT(dest, address_type, info.address_type); - INSERT_INTO_JSON_OBJECT(val, doc, ip, info.ip); - INSERT_INTO_JSON_OBJECT(val, doc, port, info.port); - INSERT_INTO_JSON_OBJECT(val, doc, rpc_port, info.rpc_port); - INSERT_INTO_JSON_OBJECT(val, doc, rpc_credits_per_hash, info.rpc_credits_per_hash); + INSERT_INTO_JSON_OBJECT(dest, ip, info.ip); + INSERT_INTO_JSON_OBJECT(dest, port, info.port); + INSERT_INTO_JSON_OBJECT(dest, rpc_port, info.rpc_port); + INSERT_INTO_JSON_OBJECT(dest, rpc_credits_per_hash, info.rpc_credits_per_hash); - INSERT_INTO_JSON_OBJECT(val, doc, peer_id, info.peer_id); + INSERT_INTO_JSON_OBJECT(dest, peer_id, info.peer_id); - INSERT_INTO_JSON_OBJECT(val, doc, recv_count, info.recv_count); - INSERT_INTO_JSON_OBJECT(val, doc, recv_idle_time, info.recv_idle_time); + INSERT_INTO_JSON_OBJECT(dest, recv_count, info.recv_count); + INSERT_INTO_JSON_OBJECT(dest, recv_idle_time, info.recv_idle_time); - INSERT_INTO_JSON_OBJECT(val, doc, send_count, info.send_count); - INSERT_INTO_JSON_OBJECT(val, doc, send_idle_time, info.send_idle_time); + INSERT_INTO_JSON_OBJECT(dest, send_count, info.send_count); + INSERT_INTO_JSON_OBJECT(dest, send_idle_time, info.send_idle_time); - INSERT_INTO_JSON_OBJECT(val, doc, state, info.state); + INSERT_INTO_JSON_OBJECT(dest, state, info.state); - INSERT_INTO_JSON_OBJECT(val, doc, live_time, info.live_time); + INSERT_INTO_JSON_OBJECT(dest, live_time, info.live_time); - INSERT_INTO_JSON_OBJECT(val, doc, avg_download, info.avg_download); - INSERT_INTO_JSON_OBJECT(val, doc, current_download, info.current_download); + INSERT_INTO_JSON_OBJECT(dest, avg_download, info.avg_download); + INSERT_INTO_JSON_OBJECT(dest, current_download, info.current_download); - INSERT_INTO_JSON_OBJECT(val, doc, avg_upload, info.avg_upload); - INSERT_INTO_JSON_OBJECT(val, doc, current_upload, info.current_upload); + INSERT_INTO_JSON_OBJECT(dest, avg_upload, info.avg_upload); + INSERT_INTO_JSON_OBJECT(dest, current_upload, info.current_upload); + + dest.EndObject(); } @@ -629,12 +652,14 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& inf GET_FROM_JSON_OBJECT(val, info.current_upload, current_upload); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_blob_entry& tx, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::tx_blob_entry& tx) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, blob, tx.blob); - INSERT_INTO_JSON_OBJECT(val, doc, prunable_hash, tx.prunable_hash); + INSERT_INTO_JSON_OBJECT(dest, blob, tx.blob); + INSERT_INTO_JSON_OBJECT(dest, prunable_hash, tx.prunable_hash); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_blob_entry& tx) @@ -648,12 +673,14 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_blob_entry& tx) GET_FROM_JSON_OBJECT(val, tx.prunable_hash, prunable_hash); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::block_complete_entry& blk, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::block_complete_entry& blk) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, block, blk.block); - INSERT_INTO_JSON_OBJECT(val, doc, transactions, blk.txs); + INSERT_INTO_JSON_OBJECT(dest, block, blk.block); + INSERT_INTO_JSON_OBJECT(dest, transactions, blk.txs); + + dest.EndObject(); } @@ -668,12 +695,14 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::block_complete_entry GET_FROM_JSON_OBJECT(val, blk.txs, transactions); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::block_with_transactions& blk, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::block_with_transactions& blk) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, block, blk.block); - INSERT_INTO_JSON_OBJECT(val, doc, transactions, blk.transactions); + INSERT_INTO_JSON_OBJECT(dest, block, blk.block); + INSERT_INTO_JSON_OBJECT(dest, transactions, blk.transactions); + + dest.EndObject(); } @@ -688,13 +717,15 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::block_with_tran GET_FROM_JSON_OBJECT(val, blk.transactions, transactions); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::transaction_info& tx_info, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::transaction_info& tx_info) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, height, tx_info.height); - INSERT_INTO_JSON_OBJECT(val, doc, in_pool, tx_info.in_pool); - INSERT_INTO_JSON_OBJECT(val, doc, transaction, tx_info.transaction); + INSERT_INTO_JSON_OBJECT(dest, height, tx_info.height); + INSERT_INTO_JSON_OBJECT(dest, in_pool, tx_info.in_pool); + INSERT_INTO_JSON_OBJECT(dest, transaction, tx_info.transaction); + + dest.EndObject(); } @@ -710,12 +741,14 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::transaction_inf GET_FROM_JSON_OBJECT(val, tx_info.transaction, transaction); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_and_amount_index& out, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_key_and_amount_index& out) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, amount_index, out.amount_index); - INSERT_INTO_JSON_OBJECT(val, doc, key, out.key); + INSERT_INTO_JSON_OBJECT(dest, amount_index, out.amount_index); + INSERT_INTO_JSON_OBJECT(dest, key, out.key); + + dest.EndObject(); } @@ -730,12 +763,14 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_and_ GET_FROM_JSON_OBJECT(val, out.key, key); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::amount_with_random_outputs& out, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::amount_with_random_outputs& out) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount); - INSERT_INTO_JSON_OBJECT(val, doc, outputs, out.outputs); + INSERT_INTO_JSON_OBJECT(dest, amount, out.amount); + INSERT_INTO_JSON_OBJECT(dest, outputs, out.outputs); + + dest.EndObject(); } @@ -750,17 +785,19 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::amount_with_ran GET_FROM_JSON_OBJECT(val, out.outputs, outputs); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::peer& peer, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::peer& peer) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, id, peer.id); - INSERT_INTO_JSON_OBJECT(val, doc, ip, peer.ip); - INSERT_INTO_JSON_OBJECT(val, doc, port, peer.port); - INSERT_INTO_JSON_OBJECT(val, doc, rpc_port, peer.rpc_port); - INSERT_INTO_JSON_OBJECT(val, doc, rpc_credits_per_hash, peer.rpc_credits_per_hash); - INSERT_INTO_JSON_OBJECT(val, doc, last_seen, peer.last_seen); - INSERT_INTO_JSON_OBJECT(val, doc, pruning_seed, peer.pruning_seed); + INSERT_INTO_JSON_OBJECT(dest, id, peer.id); + INSERT_INTO_JSON_OBJECT(dest, ip, peer.ip); + INSERT_INTO_JSON_OBJECT(dest, port, peer.port); + INSERT_INTO_JSON_OBJECT(dest, rpc_port, peer.rpc_port); + INSERT_INTO_JSON_OBJECT(dest, rpc_credits_per_hash, peer.rpc_credits_per_hash); + INSERT_INTO_JSON_OBJECT(dest, last_seen, peer.last_seen); + INSERT_INTO_JSON_OBJECT(dest, pruning_seed, peer.pruning_seed); + + dest.EndObject(); } @@ -780,25 +817,27 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::peer& peer) GET_FROM_JSON_OBJECT(val, peer.pruning_seed, pruning_seed); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::tx_in_pool& tx, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::tx_in_pool& tx) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, tx, tx.tx); - INSERT_INTO_JSON_OBJECT(val, doc, tx_hash, tx.tx_hash); - INSERT_INTO_JSON_OBJECT(val, doc, blob_size, tx.blob_size); - INSERT_INTO_JSON_OBJECT(val, doc, weight, tx.weight); - INSERT_INTO_JSON_OBJECT(val, doc, fee, tx.fee); - INSERT_INTO_JSON_OBJECT(val, doc, max_used_block_hash, tx.max_used_block_hash); - INSERT_INTO_JSON_OBJECT(val, doc, max_used_block_height, tx.max_used_block_height); - INSERT_INTO_JSON_OBJECT(val, doc, kept_by_block, tx.kept_by_block); - INSERT_INTO_JSON_OBJECT(val, doc, last_failed_block_hash, tx.last_failed_block_hash); - INSERT_INTO_JSON_OBJECT(val, doc, last_failed_block_height, tx.last_failed_block_height); - INSERT_INTO_JSON_OBJECT(val, doc, receive_time, tx.receive_time); - INSERT_INTO_JSON_OBJECT(val, doc, last_relayed_time, tx.last_relayed_time); - INSERT_INTO_JSON_OBJECT(val, doc, relayed, tx.relayed); - INSERT_INTO_JSON_OBJECT(val, doc, do_not_relay, tx.do_not_relay); - INSERT_INTO_JSON_OBJECT(val, doc, double_spend_seen, tx.double_spend_seen); + INSERT_INTO_JSON_OBJECT(dest, tx, tx.tx); + INSERT_INTO_JSON_OBJECT(dest, tx_hash, tx.tx_hash); + INSERT_INTO_JSON_OBJECT(dest, blob_size, tx.blob_size); + INSERT_INTO_JSON_OBJECT(dest, weight, tx.weight); + INSERT_INTO_JSON_OBJECT(dest, fee, tx.fee); + INSERT_INTO_JSON_OBJECT(dest, max_used_block_hash, tx.max_used_block_hash); + INSERT_INTO_JSON_OBJECT(dest, max_used_block_height, tx.max_used_block_height); + INSERT_INTO_JSON_OBJECT(dest, kept_by_block, tx.kept_by_block); + INSERT_INTO_JSON_OBJECT(dest, last_failed_block_hash, tx.last_failed_block_hash); + INSERT_INTO_JSON_OBJECT(dest, last_failed_block_height, tx.last_failed_block_height); + INSERT_INTO_JSON_OBJECT(dest, receive_time, tx.receive_time); + INSERT_INTO_JSON_OBJECT(dest, last_relayed_time, tx.last_relayed_time); + INSERT_INTO_JSON_OBJECT(dest, relayed, tx.relayed); + INSERT_INTO_JSON_OBJECT(dest, do_not_relay, tx.do_not_relay); + INSERT_INTO_JSON_OBJECT(dest, double_spend_seen, tx.double_spend_seen); + + dest.EndObject(); } @@ -825,18 +864,20 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::tx_in_pool& tx) GET_FROM_JSON_OBJECT(val, tx.double_spend_seen, double_spend_seen); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::hard_fork_info& info, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::hard_fork_info& info) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, version, info.version); - INSERT_INTO_JSON_OBJECT(val, doc, enabled, info.enabled); - INSERT_INTO_JSON_OBJECT(val, doc, window, info.window); - INSERT_INTO_JSON_OBJECT(val, doc, votes, info.votes); - INSERT_INTO_JSON_OBJECT(val, doc, threshold, info.threshold); - INSERT_INTO_JSON_OBJECT(val, doc, voting, info.voting); - INSERT_INTO_JSON_OBJECT(val, doc, state, info.state); - INSERT_INTO_JSON_OBJECT(val, doc, earliest_height, info.earliest_height); + INSERT_INTO_JSON_OBJECT(dest, version, info.version); + INSERT_INTO_JSON_OBJECT(dest, enabled, info.enabled); + INSERT_INTO_JSON_OBJECT(dest, window, info.window); + INSERT_INTO_JSON_OBJECT(dest, votes, info.votes); + INSERT_INTO_JSON_OBJECT(dest, threshold, info.threshold); + INSERT_INTO_JSON_OBJECT(dest, voting, info.voting); + INSERT_INTO_JSON_OBJECT(dest, state, info.state); + INSERT_INTO_JSON_OBJECT(dest, earliest_height, info.earliest_height); + + dest.EndObject(); } @@ -857,14 +898,16 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::hard_fork_info& GET_FROM_JSON_OBJECT(val, info.earliest_height, earliest_height); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_count& out, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_amount_count& out) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount); - INSERT_INTO_JSON_OBJECT(val, doc, total_count, out.total_count); - INSERT_INTO_JSON_OBJECT(val, doc, unlocked_count, out.unlocked_count); - INSERT_INTO_JSON_OBJECT(val, doc, recent_count, out.recent_count); + INSERT_INTO_JSON_OBJECT(dest, amount, out.amount); + INSERT_INTO_JSON_OBJECT(dest, total_count, out.total_count); + INSERT_INTO_JSON_OBJECT(dest, unlocked_count, out.unlocked_count); + INSERT_INTO_JSON_OBJECT(dest, recent_count, out.recent_count); + + dest.EndObject(); } @@ -881,12 +924,14 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_c GET_FROM_JSON_OBJECT(val, out.recent_count, recent_count); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_and_index& out, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_amount_and_index& out) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount); - INSERT_INTO_JSON_OBJECT(val, doc, index, out.index); + INSERT_INTO_JSON_OBJECT(dest, amount, out.amount); + INSERT_INTO_JSON_OBJECT(dest, index, out.index); + + dest.EndObject(); } @@ -901,13 +946,15 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_a GET_FROM_JSON_OBJECT(val, out.index, index); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_mask_unlocked& out, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_key_mask_unlocked& out) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, key, out.key); - INSERT_INTO_JSON_OBJECT(val, doc, mask, out.mask); - INSERT_INTO_JSON_OBJECT(val, doc, unlocked, out.unlocked); + INSERT_INTO_JSON_OBJECT(dest, key, out.key); + INSERT_INTO_JSON_OBJECT(dest, mask, out.mask); + INSERT_INTO_JSON_OBJECT(dest, unlocked, out.unlocked); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_mask_unlocked& out) @@ -922,13 +969,15 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_mask GET_FROM_JSON_OBJECT(val, out.unlocked, unlocked); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::error& err, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::error& err) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, code, err.code); - INSERT_INTO_JSON_OBJECT(val, doc, error_str, err.error_str); - INSERT_INTO_JSON_OBJECT(val, doc, message, err.message); + INSERT_INTO_JSON_OBJECT(dest, code, err.code); + INSERT_INTO_JSON_OBJECT(dest, error_str, err.error_str); + INSERT_INTO_JSON_OBJECT(dest, message, err.message); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::error& error) @@ -943,20 +992,22 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::error& error) GET_FROM_JSON_OBJECT(val, error.message, message); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::BlockHeaderResponse& response, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::BlockHeaderResponse& response) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, major_version, response.major_version); - INSERT_INTO_JSON_OBJECT(val, doc, minor_version, response.minor_version); - INSERT_INTO_JSON_OBJECT(val, doc, timestamp, response.timestamp); - INSERT_INTO_JSON_OBJECT(val, doc, prev_id, response.prev_id); - INSERT_INTO_JSON_OBJECT(val, doc, nonce, response.nonce); - INSERT_INTO_JSON_OBJECT(val, doc, height, response.height); - INSERT_INTO_JSON_OBJECT(val, doc, depth, response.depth); - INSERT_INTO_JSON_OBJECT(val, doc, hash, response.hash); - INSERT_INTO_JSON_OBJECT(val, doc, difficulty, response.difficulty); - INSERT_INTO_JSON_OBJECT(val, doc, reward, response.reward); + INSERT_INTO_JSON_OBJECT(dest, major_version, response.major_version); + INSERT_INTO_JSON_OBJECT(dest, minor_version, response.minor_version); + INSERT_INTO_JSON_OBJECT(dest, timestamp, response.timestamp); + INSERT_INTO_JSON_OBJECT(dest, prev_id, response.prev_id); + INSERT_INTO_JSON_OBJECT(dest, nonce, response.nonce); + INSERT_INTO_JSON_OBJECT(dest, height, response.height); + INSERT_INTO_JSON_OBJECT(dest, depth, response.depth); + INSERT_INTO_JSON_OBJECT(dest, hash, response.hash); + INSERT_INTO_JSON_OBJECT(dest, difficulty, response.difficulty); + INSERT_INTO_JSON_OBJECT(dest, reward, response.reward); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::BlockHeaderResponse& response) @@ -978,34 +1029,36 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::BlockHeaderResp GET_FROM_JSON_OBJECT(val, response.reward, reward); } -void toJsonValue(rapidjson::Document& doc, const rct::rctSig& sig, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const rct::rctSig& sig) { using boost::adaptors::transform; - val.SetObject(); + dest.StartObject(); const auto just_mask = [] (rct::ctkey const& key) -> rct::key const& { return key.mask; }; - INSERT_INTO_JSON_OBJECT(val, doc, type, sig.type); - INSERT_INTO_JSON_OBJECT(val, doc, encrypted, sig.ecdhInfo); - INSERT_INTO_JSON_OBJECT(val, doc, commitments, transform(sig.outPk, just_mask)); - INSERT_INTO_JSON_OBJECT(val, doc, fee, sig.txnFee); + INSERT_INTO_JSON_OBJECT(dest, type, sig.type); + INSERT_INTO_JSON_OBJECT(dest, encrypted, sig.ecdhInfo); + INSERT_INTO_JSON_OBJECT(dest, commitments, transform(sig.outPk, just_mask)); + INSERT_INTO_JSON_OBJECT(dest, fee, sig.txnFee); // prunable { - rapidjson::Value prunable; - prunable.SetObject(); + dest.Key("prunable"); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(prunable, doc, range_proofs, sig.p.rangeSigs); - INSERT_INTO_JSON_OBJECT(prunable, doc, bulletproofs, sig.p.bulletproofs); - INSERT_INTO_JSON_OBJECT(prunable, doc, mlsags, sig.p.MGs); - INSERT_INTO_JSON_OBJECT(prunable, doc, pseudo_outs, sig.get_pseudo_outs()); + INSERT_INTO_JSON_OBJECT(dest, range_proofs, sig.p.rangeSigs); + INSERT_INTO_JSON_OBJECT(dest, bulletproofs, sig.p.bulletproofs); + INSERT_INTO_JSON_OBJECT(dest, mlsags, sig.p.MGs); + INSERT_INTO_JSON_OBJECT(dest, pseudo_outs, sig.get_pseudo_outs()); - val.AddMember("prunable", prunable, doc.GetAllocator()); + dest.EndObject(); } + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, rct::rctSig& sig) @@ -1046,12 +1099,12 @@ void fromJsonValue(const rapidjson::Value& val, rct::rctSig& sig) } } -void toJsonValue(rapidjson::Document& doc, const rct::ecdhTuple& tuple, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const rct::ecdhTuple& tuple) { - val.SetObject(); - - INSERT_INTO_JSON_OBJECT(val, doc, mask, tuple.mask); - INSERT_INTO_JSON_OBJECT(val, doc, amount, tuple.amount); + dest.StartObject(); + INSERT_INTO_JSON_OBJECT(dest, mask, tuple.mask); + INSERT_INTO_JSON_OBJECT(dest, amount, tuple.amount); + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple) @@ -1065,14 +1118,14 @@ void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple) GET_FROM_JSON_OBJECT(val, tuple.amount, amount); } -void toJsonValue(rapidjson::Document& doc, const rct::rangeSig& sig, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const rct::rangeSig& sig) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, asig, sig.asig); + INSERT_INTO_JSON_OBJECT(dest, asig, sig.asig); + INSERT_INTO_JSON_OBJECT(dest, Ci, epee::span{sig.Ci}); - std::vector keyVector(sig.Ci, std::end(sig.Ci)); - INSERT_INTO_JSON_OBJECT(val, doc, Ci, keyVector); + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig) @@ -1102,22 +1155,24 @@ void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig) } } -void toJsonValue(rapidjson::Document& doc, const rct::Bulletproof& p, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const rct::Bulletproof& p) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, V, p.V); - INSERT_INTO_JSON_OBJECT(val, doc, A, p.A); - INSERT_INTO_JSON_OBJECT(val, doc, S, p.S); - INSERT_INTO_JSON_OBJECT(val, doc, T1, p.T1); - INSERT_INTO_JSON_OBJECT(val, doc, T2, p.T2); - INSERT_INTO_JSON_OBJECT(val, doc, taux, p.taux); - INSERT_INTO_JSON_OBJECT(val, doc, mu, p.mu); - INSERT_INTO_JSON_OBJECT(val, doc, L, p.L); - INSERT_INTO_JSON_OBJECT(val, doc, R, p.R); - INSERT_INTO_JSON_OBJECT(val, doc, a, p.a); - INSERT_INTO_JSON_OBJECT(val, doc, b, p.b); - INSERT_INTO_JSON_OBJECT(val, doc, t, p.t); + INSERT_INTO_JSON_OBJECT(dest, V, p.V); + INSERT_INTO_JSON_OBJECT(dest, A, p.A); + INSERT_INTO_JSON_OBJECT(dest, S, p.S); + INSERT_INTO_JSON_OBJECT(dest, T1, p.T1); + INSERT_INTO_JSON_OBJECT(dest, T2, p.T2); + INSERT_INTO_JSON_OBJECT(dest, taux, p.taux); + INSERT_INTO_JSON_OBJECT(dest, mu, p.mu); + INSERT_INTO_JSON_OBJECT(dest, L, p.L); + INSERT_INTO_JSON_OBJECT(dest, R, p.R); + INSERT_INTO_JSON_OBJECT(dest, a, p.a); + INSERT_INTO_JSON_OBJECT(dest, b, p.b); + INSERT_INTO_JSON_OBJECT(dest, t, p.t); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, rct::Bulletproof& p) @@ -1141,17 +1196,15 @@ void fromJsonValue(const rapidjson::Value& val, rct::Bulletproof& p) GET_FROM_JSON_OBJECT(val, p.t, t); } -void toJsonValue(rapidjson::Document& doc, const rct::boroSig& sig, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const rct::boroSig& sig) { - val.SetObject(); + dest.StartObject(); - std::vector keyVector(sig.s0, std::end(sig.s0)); - INSERT_INTO_JSON_OBJECT(val, doc, s0, keyVector); + INSERT_INTO_JSON_OBJECT(dest, s0, epee::span{sig.s0}); + INSERT_INTO_JSON_OBJECT(dest, s1, epee::span{sig.s1}); + INSERT_INTO_JSON_OBJECT(dest, ee, sig.ee); - keyVector.assign(sig.s1, std::end(sig.s1)); - INSERT_INTO_JSON_OBJECT(val, doc, s1, keyVector); - - INSERT_INTO_JSON_OBJECT(val, doc, ee, sig.ee); + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig) @@ -1188,12 +1241,14 @@ void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig) GET_FROM_JSON_OBJECT(val, sig.ee, ee); } -void toJsonValue(rapidjson::Document& doc, const rct::mgSig& sig, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const rct::mgSig& sig) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, ss, sig.ss); - INSERT_INTO_JSON_OBJECT(val, doc, cc, sig.cc); + INSERT_INTO_JSON_OBJECT(dest, ss, sig.ss); + INSERT_INTO_JSON_OBJECT(dest, cc, sig.cc); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig) @@ -1207,32 +1262,34 @@ void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig) GET_FROM_JSON_OBJECT(val, sig.cc, cc); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::DaemonInfo& info, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::DaemonInfo& info) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, height, info.height); - INSERT_INTO_JSON_OBJECT(val, doc, target_height, info.target_height); - INSERT_INTO_JSON_OBJECT(val, doc, difficulty, info.difficulty); - INSERT_INTO_JSON_OBJECT(val, doc, target, info.target); - INSERT_INTO_JSON_OBJECT(val, doc, tx_count, info.tx_count); - INSERT_INTO_JSON_OBJECT(val, doc, tx_pool_size, info.tx_pool_size); - INSERT_INTO_JSON_OBJECT(val, doc, alt_blocks_count, info.alt_blocks_count); - INSERT_INTO_JSON_OBJECT(val, doc, outgoing_connections_count, info.outgoing_connections_count); - INSERT_INTO_JSON_OBJECT(val, doc, incoming_connections_count, info.incoming_connections_count); - INSERT_INTO_JSON_OBJECT(val, doc, white_peerlist_size, info.white_peerlist_size); - INSERT_INTO_JSON_OBJECT(val, doc, grey_peerlist_size, info.grey_peerlist_size); - INSERT_INTO_JSON_OBJECT(val, doc, mainnet, info.mainnet); - INSERT_INTO_JSON_OBJECT(val, doc, testnet, info.testnet); - INSERT_INTO_JSON_OBJECT(val, doc, stagenet, info.stagenet); - INSERT_INTO_JSON_OBJECT(val, doc, nettype, info.nettype); - INSERT_INTO_JSON_OBJECT(val, doc, top_block_hash, info.top_block_hash); - INSERT_INTO_JSON_OBJECT(val, doc, cumulative_difficulty, info.cumulative_difficulty); - INSERT_INTO_JSON_OBJECT(val, doc, block_size_limit, info.block_size_limit); - INSERT_INTO_JSON_OBJECT(val, doc, block_weight_limit, info.block_weight_limit); - INSERT_INTO_JSON_OBJECT(val, doc, block_size_median, info.block_size_median); - INSERT_INTO_JSON_OBJECT(val, doc, block_weight_median, info.block_weight_median); - INSERT_INTO_JSON_OBJECT(val, doc, start_time, info.start_time); + INSERT_INTO_JSON_OBJECT(dest, height, info.height); + INSERT_INTO_JSON_OBJECT(dest, target_height, info.target_height); + INSERT_INTO_JSON_OBJECT(dest, difficulty, info.difficulty); + INSERT_INTO_JSON_OBJECT(dest, target, info.target); + INSERT_INTO_JSON_OBJECT(dest, tx_count, info.tx_count); + INSERT_INTO_JSON_OBJECT(dest, tx_pool_size, info.tx_pool_size); + INSERT_INTO_JSON_OBJECT(dest, alt_blocks_count, info.alt_blocks_count); + INSERT_INTO_JSON_OBJECT(dest, outgoing_connections_count, info.outgoing_connections_count); + INSERT_INTO_JSON_OBJECT(dest, incoming_connections_count, info.incoming_connections_count); + INSERT_INTO_JSON_OBJECT(dest, white_peerlist_size, info.white_peerlist_size); + INSERT_INTO_JSON_OBJECT(dest, grey_peerlist_size, info.grey_peerlist_size); + INSERT_INTO_JSON_OBJECT(dest, mainnet, info.mainnet); + INSERT_INTO_JSON_OBJECT(dest, testnet, info.testnet); + INSERT_INTO_JSON_OBJECT(dest, stagenet, info.stagenet); + INSERT_INTO_JSON_OBJECT(dest, nettype, info.nettype); + INSERT_INTO_JSON_OBJECT(dest, top_block_hash, info.top_block_hash); + INSERT_INTO_JSON_OBJECT(dest, cumulative_difficulty, info.cumulative_difficulty); + INSERT_INTO_JSON_OBJECT(dest, block_size_limit, info.block_size_limit); + INSERT_INTO_JSON_OBJECT(dest, block_weight_limit, info.block_weight_limit); + INSERT_INTO_JSON_OBJECT(dest, block_size_median, info.block_size_median); + INSERT_INTO_JSON_OBJECT(dest, block_weight_median, info.block_weight_median); + INSERT_INTO_JSON_OBJECT(dest, start_time, info.start_time); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& info) @@ -1266,14 +1323,16 @@ void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& inf GET_FROM_JSON_OBJECT(val, info.start_time, start_time); } -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_distribution& dist, rapidjson::Value& val) +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_distribution& dist) { - val.SetObject(); + dest.StartObject(); - INSERT_INTO_JSON_OBJECT(val, doc, distribution, dist.data.distribution); - INSERT_INTO_JSON_OBJECT(val, doc, amount, dist.amount); - INSERT_INTO_JSON_OBJECT(val, doc, start_height, dist.data.start_height); - INSERT_INTO_JSON_OBJECT(val, doc, base, dist.data.base); + INSERT_INTO_JSON_OBJECT(dest, distribution, dist.data.distribution); + INSERT_INTO_JSON_OBJECT(dest, amount, dist.amount); + INSERT_INTO_JSON_OBJECT(dest, start_height, dist.data.start_height); + INSERT_INTO_JSON_OBJECT(dest, base, dist.data.base); + + dest.EndObject(); } void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_distribution& dist) diff --git a/src/serialization/json_object.h b/src/serialization/json_object.h index 5ef75b863..d2c579589 100644 --- a/src/serialization/json_object.h +++ b/src/serialization/json_object.h @@ -28,8 +28,13 @@ #pragma once +#include +#include +#include +#include +#include + #include "string_tools.h" -#include "rapidjson/document.h" #include "cryptonote_basic/cryptonote_basic.h" #include "rpc/message_data_structs.h" #include "cryptonote_protocol/cryptonote_protocol_defs.h" @@ -44,10 +49,9 @@ } \ } while (0); -#define INSERT_INTO_JSON_OBJECT(jsonVal, doc, key, source) \ - rapidjson::Value key##Val; \ - cryptonote::json::toJsonValue(doc, source, key##Val); \ - jsonVal.AddMember(#key, key##Val, doc.GetAllocator()); +#define INSERT_INTO_JSON_OBJECT(dest, key, source) \ + dest.Key(#key, sizeof(#key) - 1); \ + cryptonote::json::toJsonValue(dest, source); #define GET_FROM_JSON_OBJECT(source, dst, key) \ OBJECT_HAS_MEMBER_OR_THROW(source, #key) \ @@ -114,16 +118,24 @@ inline constexpr bool is_to_hex() return std::is_pod() && !std::is_integral(); } +// POD to json key +template +inline typename std::enable_if()>::type toJsonKey(rapidjson::Writer& dest, const Type& pod) +{ + const auto hex = epee::to_hex::array(pod); + dest.Key(hex.data(), hex.size()); +} // POD to json value template -typename std::enable_if()>::type toJsonValue(rapidjson::Document& doc, const Type& pod, rapidjson::Value& value) +inline typename std::enable_if()>::type toJsonValue(rapidjson::Writer& dest, const Type& pod) { - value = rapidjson::Value(epee::string_tools::pod_to_hex(pod).c_str(), doc.GetAllocator()); + const auto hex = epee::to_hex::array(pod); + dest.String(hex.data(), hex.size()); } template -typename std::enable_if()>::type fromJsonValue(const rapidjson::Value& val, Type& t) +inline typename std::enable_if()>::type fromJsonValue(const rapidjson::Value& val, Type& t) { if (!val.IsString()) { @@ -139,10 +151,16 @@ typename std::enable_if()>::type fromJsonValue(const rapidjson:: } } -void toJsonValue(rapidjson::Document& doc, const std::string& i, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const rapidjson::Value& src); + +void toJsonValue(rapidjson::Writer& dest, boost::string_ref i); +inline void toJsonValue(rapidjson::Writer& dest, const std::string& i) +{ + toJsonValue(dest, boost::string_ref{i}); +} void fromJsonValue(const rapidjson::Value& val, std::string& str); -void toJsonValue(rapidjson::Document& doc, bool i, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, bool i); void fromJsonValue(const rapidjson::Value& val, bool& b); // integers overloads for toJsonValue are not needed for standard promotions @@ -157,144 +175,144 @@ void fromJsonValue(const rapidjson::Value& val, unsigned short& i); void fromJsonValue(const rapidjson::Value& val, short& i); -void toJsonValue(rapidjson::Document& doc, const unsigned i, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const unsigned i); void fromJsonValue(const rapidjson::Value& val, unsigned& i); -void toJsonValue(rapidjson::Document& doc, const int, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const int); void fromJsonValue(const rapidjson::Value& val, int& i); -void toJsonValue(rapidjson::Document& doc, const unsigned long long i, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const unsigned long long i); void fromJsonValue(const rapidjson::Value& val, unsigned long long& i); -void toJsonValue(rapidjson::Document& doc, const long long i, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const long long i); void fromJsonValue(const rapidjson::Value& val, long long& i); -inline void toJsonValue(rapidjson::Document& doc, const unsigned long i, rapidjson::Value& val) { - toJsonValue(doc, static_cast(i), val); +inline void toJsonValue(rapidjson::Writer& dest, const unsigned long i) { + toJsonValue(dest, static_cast(i)); } void fromJsonValue(const rapidjson::Value& val, unsigned long& i); -inline void toJsonValue(rapidjson::Document& doc, const long i, rapidjson::Value& val) { - toJsonValue(doc, static_cast(i), val); +inline void toJsonValue(rapidjson::Writer& dest, const long i) { + toJsonValue(dest, static_cast(i)); } void fromJsonValue(const rapidjson::Value& val, long& i); // end integers -void toJsonValue(rapidjson::Document& doc, const cryptonote::transaction& tx, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::transaction& tx); void fromJsonValue(const rapidjson::Value& val, cryptonote::transaction& tx); -void toJsonValue(rapidjson::Document& doc, const cryptonote::block& b, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::block& b); void fromJsonValue(const rapidjson::Value& val, cryptonote::block& b); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_v& txin, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_v& txin); void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_gen& txin, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_gen& txin); void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_script& txin, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_to_script& txin); void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_script& txin); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_scripthash& txin, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_to_scripthash& txin); void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_scripthash& txin); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_key& txin, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txin_to_key& txin); void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_key& txin); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_target_v& txout, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txout_target_v& txout); void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_target_v& txout); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_script& txout, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txout_to_script& txout); void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_script& txout); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_scripthash& txout, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txout_to_scripthash& txout); void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_scripthash& txout); -void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_key& txout, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::txout_to_key& txout); void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_key& txout); -void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_out& txout, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::tx_out& txout); void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout); -void toJsonValue(rapidjson::Document& doc, const cryptonote::connection_info& info, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::connection_info& info); void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& info); -void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_blob_entry& tx, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::tx_blob_entry& tx); void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_blob_entry& tx); -void toJsonValue(rapidjson::Document& doc, const cryptonote::block_complete_entry& blk, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::block_complete_entry& blk); void fromJsonValue(const rapidjson::Value& val, cryptonote::block_complete_entry& blk); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::block_with_transactions& blk, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::block_with_transactions& blk); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::block_with_transactions& blk); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::transaction_info& tx_info, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::transaction_info& tx_info); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::transaction_info& tx_info); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_and_amount_index& out, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_key_and_amount_index& out); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_and_amount_index& out); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::amount_with_random_outputs& out, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::amount_with_random_outputs& out); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::amount_with_random_outputs& out); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::peer& peer, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::peer& peer); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::peer& peer); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::tx_in_pool& tx, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::tx_in_pool& tx); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::tx_in_pool& tx); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::hard_fork_info& info, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::hard_fork_info& info); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::hard_fork_info& info); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_count& out, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_amount_count& out); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_count& out); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_and_index& out, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_amount_and_index& out); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_and_index& out); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_mask_unlocked& out, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_key_mask_unlocked& out); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_mask_unlocked& out); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::error& err, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::error& err); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::error& error); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::BlockHeaderResponse& response, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::BlockHeaderResponse& response); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::BlockHeaderResponse& response); -void toJsonValue(rapidjson::Document& doc, const rct::rctSig& i, rapidjson::Value& val); -void fromJsonValue(const rapidjson::Value& i, rct::rctSig& sig); +void toJsonValue(rapidjson::Writer& dest, const rct::rctSig& i); +void fromJsonValue(const rapidjson::Value& val, rct::rctSig& sig); -void toJsonValue(rapidjson::Document& doc, const rct::ecdhTuple& tuple, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const rct::ecdhTuple& tuple); void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple); -void toJsonValue(rapidjson::Document& doc, const rct::rangeSig& sig, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const rct::rangeSig& sig); void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig); -void toJsonValue(rapidjson::Document& doc, const rct::Bulletproof& p, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const rct::Bulletproof& p); void fromJsonValue(const rapidjson::Value& val, rct::Bulletproof& p); -void toJsonValue(rapidjson::Document& doc, const rct::boroSig& sig, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const rct::boroSig& sig); void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig); -void toJsonValue(rapidjson::Document& doc, const rct::mgSig& sig, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const rct::mgSig& sig); void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::DaemonInfo& info, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::DaemonInfo& info); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& info); -void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_distribution& dist, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer& dest, const cryptonote::rpc::output_distribution& dist); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_distribution& dist); template -typename std::enable_if::value, void>::type toJsonValue(rapidjson::Document& doc, const Map& map, rapidjson::Value& val); +typename std::enable_if::value, void>::type toJsonValue(rapidjson::Writer& dest, const Map& map); template typename std::enable_if::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map); template -typename std::enable_if::value, void>::type toJsonValue(rapidjson::Document& doc, const Vec &vec, rapidjson::Value& val); +typename std::enable_if::value, void>::type toJsonValue(rapidjson::Writer& dest, const Vec &vec); template typename std::enable_if::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec); @@ -304,24 +322,22 @@ typename std::enable_if::value, void>::type fromJson // unfortunately because of how templates work they have to be here. template -typename std::enable_if::value, void>::type toJsonValue(rapidjson::Document& doc, const Map& map, rapidjson::Value& val) +inline typename std::enable_if::value, void>::type toJsonValue(rapidjson::Writer& dest, const Map& map) { - val.SetObject(); - - auto& al = doc.GetAllocator(); + using key_type = typename Map::key_type; + static_assert(std::is_same() || is_to_hex(), "invalid map key type"); + dest.StartObject(); for (const auto& i : map) { - rapidjson::Value k; - rapidjson::Value m; - toJsonValue(doc, i.first, k); - toJsonValue(doc, i.second, m); - val.AddMember(k, m, al); + toJsonKey(dest, i.first); + toJsonValue(dest, i.second); } + dest.EndObject(); } template -typename std::enable_if::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map) +inline typename std::enable_if::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map) { if (!val.IsObject()) { @@ -342,20 +358,16 @@ typename std::enable_if::value, void>::type fromJsonVal } template -typename std::enable_if::value, void>::type toJsonValue(rapidjson::Document& doc, const Vec &vec, rapidjson::Value& val) +inline typename std::enable_if::value, void>::type toJsonValue(rapidjson::Writer& dest, const Vec &vec) { - val.SetArray(); - + dest.StartArray(); for (const auto& t : vec) - { - rapidjson::Value v; - toJsonValue(doc, t, v); - val.PushBack(v, doc.GetAllocator()); - } + toJsonValue(dest, t); + dest.EndArray(vec.size()); } template -typename std::enable_if::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec) +inline typename std::enable_if::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec) { if (!val.IsArray()) { diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index 6f887afda..ee44ea2d5 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -850,6 +850,17 @@ TEST(ToHex, Array) ); } +TEST(ToHex, ArrayFromPod) +{ + std::array expected{{'5', 'f', '2', 'b', '0', '1'}}; + std::fill(expected.begin() + 6, expected.end(), '0'); + + EXPECT_EQ( + expected, + (epee::to_hex::array(crypto::ec_point{{0x5F, 0x2B, 0x01, 0x00}})) + ); +} + TEST(ToHex, Ostream) { std::stringstream out; diff --git a/tests/unit_tests/json_serialization.cpp b/tests/unit_tests/json_serialization.cpp index 53d9f84c7..6f98d854d 100644 --- a/tests/unit_tests/json_serialization.cpp +++ b/tests/unit_tests/json_serialization.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include "crypto/hash.h" @@ -80,6 +82,27 @@ namespace return tx; } + + template + T test_json(const T& value) + { + rapidjson::StringBuffer buffer; + { + rapidjson::Writer dest{buffer}; + cryptonote::json::toJsonValue(dest, value); + } + + rapidjson::Document doc; + doc.Parse(buffer.GetString()); + if (doc.HasParseError() || !doc.IsObject()) + { + throw cryptonote::json::PARSE_FAIL(); + } + + T out{}; + cryptonote::json::fromJsonValue(doc, out); + return out; + } } // anonymous TEST(JsonSerialization, MinerTransaction) @@ -91,11 +114,7 @@ TEST(JsonSerialization, MinerTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, miner_tx, doc); - - cryptonote::transaction miner_tx_copy; - cryptonote::json::fromJsonValue(doc, miner_tx_copy); + cryptonote::transaction miner_tx_copy = test_json(miner_tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(miner_tx_copy, tx_copy_hash)); @@ -126,11 +145,7 @@ TEST(JsonSerialization, RegularTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, tx, doc); - - cryptonote::transaction tx_copy; - cryptonote::json::fromJsonValue(doc, tx_copy); + cryptonote::transaction tx_copy = test_json(tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); @@ -161,11 +176,7 @@ TEST(JsonSerialization, RingctTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, tx, doc); - - cryptonote::transaction tx_copy; - cryptonote::json::fromJsonValue(doc, tx_copy); + cryptonote::transaction tx_copy = test_json(tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash)); @@ -196,11 +207,7 @@ TEST(JsonSerialization, BulletproofTransaction) crypto::hash tx_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx, tx_hash)); - rapidjson::Document doc; - cryptonote::json::toJsonValue(doc, tx, doc); - - cryptonote::transaction tx_copy; - cryptonote::json::fromJsonValue(doc, tx_copy); + cryptonote::transaction tx_copy = test_json(tx); crypto::hash tx_copy_hash{}; ASSERT_TRUE(cryptonote::get_transaction_hash(tx_copy, tx_copy_hash));