diff options
author | Lee Clagett <code@leeclagett.com> | 2019-11-07 05:45:06 +0000 |
---|---|---|
committer | Lee Clagett <code@leeclagett.com> | 2020-03-05 14:20:56 +0000 |
commit | 0f78b06e8c578819f831a513490278a5f70b01af (patch) | |
tree | 2f940ff50b1676b92887a4a755b7eb1ba462c04c /src/serialization | |
parent | Merge pull request #6057 (diff) | |
download | monero-0f78b06e8c578819f831a513490278a5f70b01af.tar.xz |
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.
Diffstat (limited to 'src/serialization')
-rw-r--r-- | src/serialization/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/serialization/json_object.cpp | 621 | ||||
-rw-r--r-- | src/serialization/json_object.h | 162 |
3 files changed, 428 insertions, 356 deletions
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<rapidjson::StringBuffer>& dest, const rapidjson::Value& src) { - val = rapidjson::Value(i.c_str(), doc.GetAllocator()); + src.Accept(dest); +} + +void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const unsigned long long i) { - static_assert(!precision_loss<unsigned long long, std::uint64_t>(), "precision loss"); - val = rapidjson::Value(std::uint64_t(i)); + static_assert(std::numeric_limits<unsigned long long>::max() <= std::numeric_limits<std::uint64_t>::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<rapidjson::StringBuffer>& dest, const long long i) { - static_assert(!precision_loss<long long, std::int64_t>(), "precision loss"); - val = rapidjson::Value(std::int64_t(i)); + static_assert(std::numeric_limits<std::uint64_t>::min() <= std::numeric_limits<long long>::min(), "bad int64 conversion"); + static_assert(std::numeric_limits<long long>::max() <= std::numeric_limits<std::uint64_t>::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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::block& b) { - val.SetObject(); + dest.StartObject(); + + 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); - 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); + 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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,13 +357,14 @@ 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<rapidjson::StringBuffer>& 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) { @@ -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<rapidjson::StringBuffer>& dest, const cryptonote::txin_to_script& txin) { - val.SetObject(); + dest.StartObject(); + + INSERT_INTO_JSON_OBJECT(dest, prev, txin.prev); + INSERT_INTO_JSON_OBJECT(dest, prevout, txin.prevout); + INSERT_INTO_JSON_OBJECT(dest, sigset, txin.sigset); - 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); + 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) + +void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const cryptonote::txin_to_scripthash& txin) { - val.SetObject(); + 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); - 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); + dest.EndObject(); } @@ -412,15 +427,16 @@ 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<rapidjson::StringBuffer>& 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) { @@ -434,14 +450,16 @@ 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<rapidjson::StringBuffer>& 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) { @@ -454,13 +472,15 @@ 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<rapidjson::StringBuffer>& 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) { @@ -472,13 +492,15 @@ 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<rapidjson::StringBuffer>& 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) { @@ -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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::connection_info& info) { - val.SetObject(); + dest.StartObject(); + + 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, 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, 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, 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, peer_id, info.peer_id); - INSERT_INTO_JSON_OBJECT(val, doc, peer_id, info.peer_id); + 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, recv_count, info.recv_count); - INSERT_INTO_JSON_OBJECT(val, doc, recv_idle_time, info.recv_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, send_count, info.send_count); - INSERT_INTO_JSON_OBJECT(val, doc, send_idle_time, info.send_idle_time); + INSERT_INTO_JSON_OBJECT(dest, state, info.state); - INSERT_INTO_JSON_OBJECT(val, doc, state, info.state); + INSERT_INTO_JSON_OBJECT(dest, live_time, info.live_time); - INSERT_INTO_JSON_OBJECT(val, doc, live_time, info.live_time); + 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_download, info.avg_download); - INSERT_INTO_JSON_OBJECT(val, doc, current_download, info.current_download); + INSERT_INTO_JSON_OBJECT(dest, avg_upload, info.avg_upload); + INSERT_INTO_JSON_OBJECT(dest, current_upload, info.current_upload); - INSERT_INTO_JSON_OBJECT(val, doc, avg_upload, info.avg_upload); - INSERT_INTO_JSON_OBJECT(val, doc, 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<rapidjson::StringBuffer>& dest, const cryptonote::tx_blob_entry& tx) { - val.SetObject(); + dest.StartObject(); + + INSERT_INTO_JSON_OBJECT(dest, blob, tx.blob); + INSERT_INTO_JSON_OBJECT(dest, prunable_hash, tx.prunable_hash); - INSERT_INTO_JSON_OBJECT(val, doc, blob, tx.blob); - INSERT_INTO_JSON_OBJECT(val, doc, 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::block_with_transactions& blk) { - val.SetObject(); + dest.StartObject(); + + INSERT_INTO_JSON_OBJECT(dest, block, blk.block); + INSERT_INTO_JSON_OBJECT(dest, transactions, blk.transactions); - INSERT_INTO_JSON_OBJECT(val, doc, block, blk.block); - INSERT_INTO_JSON_OBJECT(val, doc, 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::transaction_info& tx_info) { - val.SetObject(); + dest.StartObject(); + + 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); - 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); + 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::amount_with_random_outputs& out) { - val.SetObject(); + dest.StartObject(); + + INSERT_INTO_JSON_OBJECT(dest, amount, out.amount); + INSERT_INTO_JSON_OBJECT(dest, outputs, out.outputs); - INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount); - INSERT_INTO_JSON_OBJECT(val, doc, 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::peer& peer) { - val.SetObject(); + dest.StartObject(); + + 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); - 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); + 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::hard_fork_info& info) { - val.SetObject(); + dest.StartObject(); + + 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); - 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); + 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::output_amount_and_index& out) { - val.SetObject(); + dest.StartObject(); + + INSERT_INTO_JSON_OBJECT(dest, amount, out.amount); + INSERT_INTO_JSON_OBJECT(dest, index, out.index); - INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount); - INSERT_INTO_JSON_OBJECT(val, doc, 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::output_key_mask_unlocked& out) { - val.SetObject(); + dest.StartObject(); + + INSERT_INTO_JSON_OBJECT(dest, key, out.key); + INSERT_INTO_JSON_OBJECT(dest, mask, out.mask); + INSERT_INTO_JSON_OBJECT(dest, unlocked, out.unlocked); - 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); + 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::BlockHeaderResponse& response) { - val.SetObject(); + dest.StartObject(); + + 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); - 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); + 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<const rct::key>{sig.Ci}); - std::vector<rct::key> 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<rapidjson::StringBuffer>& dest, const rct::Bulletproof& p) { - val.SetObject(); + dest.StartObject(); + + 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); - 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); + 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<rapidjson::StringBuffer>& dest, const rct::boroSig& sig) { - val.SetObject(); - - std::vector<rct::key> keyVector(sig.s0, std::end(sig.s0)); - INSERT_INTO_JSON_OBJECT(val, doc, s0, keyVector); + dest.StartObject(); - keyVector.assign(sig.s1, std::end(sig.s1)); - INSERT_INTO_JSON_OBJECT(val, doc, s1, keyVector); + INSERT_INTO_JSON_OBJECT(dest, s0, epee::span<const rct::key>{sig.s0}); + INSERT_INTO_JSON_OBJECT(dest, s1, epee::span<const rct::key>{sig.s1}); + INSERT_INTO_JSON_OBJECT(dest, ee, sig.ee); - 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<rapidjson::StringBuffer>& 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) -{ - val.SetObject(); - - 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); +void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const cryptonote::rpc::DaemonInfo& info) +{ + dest.StartObject(); + + 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::output_distribution& dist) { - val.SetObject(); + dest.StartObject(); + + 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); - 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); + 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 <boost/utility/string_ref.hpp> +#include <cstring> +#include <rapidjson/document.h> +#include <rapidjson/stringbuffer.h> +#include <rapidjson/writer.h> + #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<Type>() && !std::is_integral<Type>(); } +// POD to json key +template <class Type> +inline typename std::enable_if<is_to_hex<Type>()>::type toJsonKey(rapidjson::Writer<rapidjson::StringBuffer>& dest, const Type& pod) +{ + const auto hex = epee::to_hex::array(pod); + dest.Key(hex.data(), hex.size()); +} // POD to json value template <class Type> -typename std::enable_if<is_to_hex<Type>()>::type toJsonValue(rapidjson::Document& doc, const Type& pod, rapidjson::Value& value) +inline typename std::enable_if<is_to_hex<Type>()>::type toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& 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 <class Type> -typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t) +inline typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t) { if (!val.IsString()) { @@ -139,10 +151,16 @@ typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson:: } } -void toJsonValue(rapidjson::Document& doc, const std::string& i, rapidjson::Value& val); +void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const rapidjson::Value& src); + +void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, boost::string_ref i); +inline void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<unsigned long long>(i), val); +inline void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const unsigned long i) { + toJsonValue(dest, static_cast<unsigned long long>(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<long long>(i), val); +inline void toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const long i) { + toJsonValue(dest, static_cast<long long>(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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& 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<rapidjson::StringBuffer>& dest, const cryptonote::rpc::output_distribution& dist); void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_distribution& dist); template <typename Map> -typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Document& doc, const Map& map, rapidjson::Value& val); +typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const Map& map); template <typename Map> typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map); template <typename Vec> -typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Document& doc, const Vec &vec, rapidjson::Value& val); +typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const Vec &vec); template <typename Vec> typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec); @@ -304,24 +322,22 @@ typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJson // unfortunately because of how templates work they have to be here. template <typename Map> -typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Document& doc, const Map& map, rapidjson::Value& val) +inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& dest, const Map& map) { - val.SetObject(); - - auto& al = doc.GetAllocator(); + using key_type = typename Map::key_type; + static_assert(std::is_same<std::string, key_type>() || is_to_hex<key_type>(), "invalid map key type"); + 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 Map> -typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map) +inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map) { if (!val.IsObject()) { @@ -342,20 +358,16 @@ typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonVal } template <typename Vec> -typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Document& doc, const Vec &vec, rapidjson::Value& val) +inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<rapidjson::StringBuffer>& 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 Vec> -typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec) +inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec) { if (!val.IsArray()) { |