danicoin/src/Serialization/JsonOutputStreamSerializer.cpp

145 lines
4.2 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2014-09-15 17:33:47 +00:00
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
2015-05-27 12:08:46 +00:00
#include "JsonOutputStreamSerializer.h"
2014-09-15 17:33:47 +00:00
#include <cassert>
#include <stdexcept>
2015-05-27 12:08:46 +00:00
#include "Common/StringTools.h"
2014-09-15 17:33:47 +00:00
2015-05-27 12:08:46 +00:00
using Common::JsonValue;
using namespace CryptoNote;
2014-09-15 17:33:47 +00:00
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
std::ostream& operator<<(std::ostream& out, const JsonOutputStreamSerializer& enumerator) {
out << enumerator.root;
return out;
}
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
namespace {
template <typename T>
void insertOrPush(JsonValue& js, Common::StringView name, const T& value) {
if (js.isArray()) {
js.pushBack(JsonValue(value));
} else {
js.insert(std::string(name), JsonValue(value));
}
2014-09-15 17:33:47 +00:00
}
}
2015-07-15 12:23:00 +00:00
JsonOutputStreamSerializer::JsonOutputStreamSerializer() : root(JsonValue::OBJECT) {
chain.push_back(&root);
}
JsonOutputStreamSerializer::~JsonOutputStreamSerializer() {
2014-09-15 17:33:47 +00:00
}
ISerializer::SerializerType JsonOutputStreamSerializer::type() const {
return ISerializer::OUTPUT;
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::beginObject(Common::StringView name) {
JsonValue& parent = *chain.back();
2014-09-15 17:33:47 +00:00
JsonValue obj(JsonValue::OBJECT);
2015-07-15 12:23:00 +00:00
if (parent.isObject()) {
chain.push_back(&parent.insert(std::string(name), obj));
2014-09-15 17:33:47 +00:00
} else {
2015-07-15 12:23:00 +00:00
chain.push_back(&parent.pushBack(obj));
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
void JsonOutputStreamSerializer::endObject() {
assert(!chain.empty());
2014-09-15 17:33:47 +00:00
chain.pop_back();
}
2015-07-30 15:22:07 +00:00
bool JsonOutputStreamSerializer::beginArray(size_t& size, Common::StringView name) {
2014-09-15 17:33:47 +00:00
JsonValue val(JsonValue::ARRAY);
2015-07-15 12:23:00 +00:00
JsonValue& res = chain.back()->insert(std::string(name), val);
2014-09-15 17:33:47 +00:00
chain.push_back(&res);
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
void JsonOutputStreamSerializer::endArray() {
assert(!chain.empty());
2014-09-15 17:33:47 +00:00
chain.pop_back();
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(uint64_t& value, Common::StringView name) {
2014-09-15 17:33:47 +00:00
int64_t v = static_cast<int64_t>(value);
return operator()(v, name);
}
2015-07-30 15:22:07 +00:00
bool JsonOutputStreamSerializer::operator()(uint16_t& value, Common::StringView name) {
uint64_t v = static_cast<uint64_t>(value);
return operator()(v, name);
}
bool JsonOutputStreamSerializer::operator()(int16_t& value, Common::StringView name) {
int64_t v = static_cast<int64_t>(value);
return operator()(v, name);
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(uint32_t& value, Common::StringView name) {
2014-09-15 17:33:47 +00:00
uint64_t v = static_cast<uint64_t>(value);
return operator()(v, name);
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(int32_t& value, Common::StringView name) {
2014-09-15 17:33:47 +00:00
int64_t v = static_cast<int64_t>(value);
return operator()(v, name);
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(int64_t& value, Common::StringView name) {
insertOrPush(*chain.back(), name, value);
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(double& value, Common::StringView name) {
insertOrPush(*chain.back(), name, value);
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(std::string& value, Common::StringView name) {
insertOrPush(*chain.back(), name, value);
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(uint8_t& value, Common::StringView name) {
insertOrPush(*chain.back(), name, static_cast<int64_t>(value));
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::operator()(bool& value, Common::StringView name) {
insertOrPush(*chain.back(), name, value);
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-30 15:22:07 +00:00
bool JsonOutputStreamSerializer::binary(void* value, size_t size, Common::StringView name) {
2015-07-15 12:23:00 +00:00
std::string hex = Common::toHex(value, size);
2015-05-27 12:08:46 +00:00
return (*this)(hex, name);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonOutputStreamSerializer::binary(std::string& value, Common::StringView name) {
2015-05-27 12:08:46 +00:00
return binary(const_cast<char*>(value.data()), value.size(), name);
2014-09-15 17:33:47 +00:00
}