2016-01-18 15:33:29 +00:00
|
|
|
// Copyright (c) 2011-2016 The Cryptonote developers
|
2015-09-18 11:55:31 +00:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-07-15 12:23:00 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
#include <Common/MemoryInputStream.h>
|
|
|
|
#include <Common/StringOutputStream.h>
|
2015-07-15 12:23:00 +00:00
|
|
|
#include "JsonInputStreamSerializer.h"
|
|
|
|
#include "JsonOutputStreamSerializer.h"
|
2015-07-30 15:22:07 +00:00
|
|
|
#include "KVBinaryInputStreamSerializer.h"
|
|
|
|
#include "KVBinaryOutputStreamSerializer.h"
|
2015-07-15 12:23:00 +00:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T getValueAs(const JsonValue& js) {
|
|
|
|
return js;
|
|
|
|
//cdstatic_assert(false, "undefined conversion");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline std::string getValueAs<std::string>(const JsonValue& js) { return js.getString(); }
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline uint64_t getValueAs<uint64_t>(const JsonValue& js) { return static_cast<uint64_t>(js.getInteger()); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace CryptoNote {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Common::JsonValue storeToJsonValue(const T& v) {
|
|
|
|
JsonOutputStreamSerializer s;
|
|
|
|
serialize(const_cast<T&>(v), s);
|
|
|
|
return s.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Common::JsonValue storeContainerToJsonValue(const T& cont) {
|
|
|
|
Common::JsonValue js(Common::JsonValue::ARRAY);
|
|
|
|
for (const auto& item : cont) {
|
|
|
|
js.pushBack(item);
|
|
|
|
}
|
|
|
|
return js;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Common::JsonValue storeToJsonValue(const std::vector<T>& v) { return storeContainerToJsonValue(v); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Common::JsonValue storeToJsonValue(const std::list<T>& v) { return storeContainerToJsonValue(v); }
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline Common::JsonValue storeToJsonValue(const std::string& v) { return Common::JsonValue(v); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void loadFromJsonValue(T& v, const Common::JsonValue& js) {
|
|
|
|
JsonInputValueSerializer s(js);
|
|
|
|
serialize(v, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void loadFromJsonValue(std::vector<T>& v, const Common::JsonValue& js) {
|
|
|
|
for (size_t i = 0; i < js.size(); ++i) {
|
|
|
|
v.push_back(Common::getValueAs<T>(js[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void loadFromJsonValue(std::list<T>& v, const Common::JsonValue& js) {
|
|
|
|
for (size_t i = 0; i < js.size(); ++i) {
|
|
|
|
v.push_back(Common::getValueAs<T>(js[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string storeToJson(const T& v) {
|
|
|
|
return storeToJsonValue(v).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool loadFromJson(T& v, const std::string& buf) {
|
|
|
|
try {
|
|
|
|
if (buf.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto js = Common::JsonValue::fromString(buf);
|
|
|
|
loadFromJsonValue(v, js);
|
|
|
|
} catch (std::exception&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string storeToBinaryKeyValue(const T& v) {
|
|
|
|
KVBinaryOutputStreamSerializer s;
|
|
|
|
serialize(const_cast<T&>(v), s);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::string result;
|
|
|
|
Common::StringOutputStream stream(result);
|
|
|
|
s.dump(stream);
|
|
|
|
return result;
|
2015-07-15 12:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool loadFromBinaryKeyValue(T& v, const std::string& buf) {
|
|
|
|
try {
|
2015-07-30 15:22:07 +00:00
|
|
|
Common::MemoryInputStream stream(buf.data(), buf.size());
|
2015-07-15 12:23:00 +00:00
|
|
|
KVBinaryInputStreamSerializer s(stream);
|
|
|
|
serialize(v, s);
|
2015-07-30 15:22:07 +00:00
|
|
|
return true;
|
2015-07-15 12:23:00 +00:00
|
|
|
} catch (std::exception&) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|