From 78eb67d4cb4cd6959941fe964f48b3512c12e3e7 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 7 Jun 2019 09:26:03 -0400 Subject: [PATCH] added SerializableStruct which defines serialize() and toPropertyTree() --- src/daemon/MoneroDaemon.cpp | 24 ++++++++++++++++++++++++ src/daemon/MoneroDaemon.h | 23 +++++++++++++++++++++++ src/utils/MoneroUtils.cpp | 6 ++++++ src/utils/MoneroUtils.h | 4 ++-- 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/daemon/MoneroDaemon.cpp diff --git a/src/daemon/MoneroDaemon.cpp b/src/daemon/MoneroDaemon.cpp new file mode 100644 index 0000000..87d807d --- /dev/null +++ b/src/daemon/MoneroDaemon.cpp @@ -0,0 +1,24 @@ +#include "utils/MoneroUtils.h" + +#include +#include +#include "mnemonics/electrum-words.h" +#include "mnemonics/english.h" + +using namespace cryptonote; +using namespace epee; + +/** + * Public interface for libmonero-cpp library. + */ +namespace monero { + + string SerializableStruct::serialize() { + boost::property_tree::ptree node; + toPropertyTree(node); + return MoneroUtils::serialize(node); + } + + // ---------------------------- PRIVATE HELPERS ----------------------------- + +} diff --git a/src/daemon/MoneroDaemon.h b/src/daemon/MoneroDaemon.h index cff3931..08b6a63 100644 --- a/src/daemon/MoneroDaemon.h +++ b/src/daemon/MoneroDaemon.h @@ -1,6 +1,9 @@ +#include "utils/MoneroUtils.h" #include "include_base_utils.h" #include "common/util.h" #include "wallet/wallet2.h" // TODO: this is imported so BEGIN_KV_SERIALIZE_MAP works; more precise import? +#include +#include using namespace std; @@ -9,6 +12,26 @@ using namespace std; */ namespace monero { + /** + * Base struct model which is serializable. + */ + struct SerializableStruct { + + /** + * Serializes the struct to a json string. + * + * @return the struct serialized to a json string + */ + string serialize(); + + /** + * Initializes a property tree node from this struct. + * + * @param node is the node to initialize + */ + virtual void toPropertyTree(boost::property_tree::ptree& node); + }; + /** * Enumerates Monero network types. */ diff --git a/src/utils/MoneroUtils.cpp b/src/utils/MoneroUtils.cpp index db2ef29..67ddbc9 100644 --- a/src/utils/MoneroUtils.cpp +++ b/src/utils/MoneroUtils.cpp @@ -114,6 +114,12 @@ void MoneroUtils::binaryBlocksToJson(const std::string &bin, std::string &json) // throw runtime_error("deserializeOutputWallet(outputStr) not implemented"); //} +string MoneroUtils::serialize(const boost::property_tree::ptree& node) { + std::stringstream ss; + boost::property_tree::write_json(ss, node, false); + return ss.str(); +} + boost::property_tree::ptree MoneroUtils::toPropertyTree(const MoneroAccount& account) { cout << "toPropertyTree(account)" << endl; boost::property_tree::ptree accountNode; diff --git a/src/utils/MoneroUtils.h b/src/utils/MoneroUtils.h index 13bbbd3..b99fc24 100644 --- a/src/utils/MoneroUtils.h +++ b/src/utils/MoneroUtils.h @@ -5,8 +5,6 @@ #include "cryptonote_basic/cryptonote_basic.h" #include "serialization/keyvalue_serialization.h" // TODO: consolidate with other binary deps? #include "storages/portable_storage.h" -#include -#include /** * Collection of utilities for working with Monero's binary portable storage format. @@ -62,6 +60,8 @@ namespace MoneroUtils // return str; // } + string serialize(const boost::property_tree::ptree& node); + boost::property_tree::ptree toPropertyTree(const MoneroAccount& account); boost::property_tree::ptree toPropertyTree(const MoneroSubaddress& subaddress);