danicoin/src/Serialization/JsonInputValueSerializer.cpp

179 lines
4.7 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 "JsonInputValueSerializer.h"
2015-07-30 15:22:07 +00:00
2014-09-15 17:33:47 +00:00
#include <cassert>
#include <stdexcept>
2015-07-30 15:22:07 +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-07-30 15:22:07 +00:00
JsonInputValueSerializer::JsonInputValueSerializer(const Common::JsonValue& value) {
if (!value.isObject()) {
throw std::runtime_error("Serializer doesn't support this type of serialization: Object expected.");
}
chain.push_back(&value);
}
JsonInputValueSerializer::JsonInputValueSerializer(Common::JsonValue&& value) : value(std::move(value)) {
if (!this->value.isObject()) {
throw std::runtime_error("Serializer doesn't support this type of serialization: Object expected.");
}
chain.push_back(&this->value);
2014-09-15 17:33:47 +00:00
}
JsonInputValueSerializer::~JsonInputValueSerializer() {
}
ISerializer::SerializerType JsonInputValueSerializer::type() const {
return ISerializer::INPUT;
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::beginObject(Common::StringView name) {
2014-09-15 17:33:47 +00:00
const JsonValue* parent = chain.back();
2015-07-15 12:23:00 +00:00
2014-09-15 17:33:47 +00:00
if (parent->isArray()) {
const JsonValue& v = (*parent)[idxs.back()++];
chain.push_back(&v);
2015-07-15 12:23:00 +00:00
return true;
}
if (parent->contains(std::string(name))) {
const JsonValue& v = (*parent)(std::string(name));
2014-09-15 17:33:47 +00:00
chain.push_back(&v);
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
return false;
}
2014-09-15 17:33:47 +00:00
2015-07-15 12:23:00 +00:00
void JsonInputValueSerializer::endObject() {
assert(!chain.empty());
2014-09-15 17:33:47 +00:00
chain.pop_back();
}
2015-07-30 15:22:07 +00:00
bool JsonInputValueSerializer::beginArray(size_t& size, Common::StringView name) {
2014-09-15 17:33:47 +00:00
const JsonValue* parent = chain.back();
2015-07-15 12:23:00 +00:00
std::string strName(name);
2014-09-15 17:33:47 +00:00
2015-07-15 12:23:00 +00:00
if (parent->contains(strName)) {
const JsonValue& arr = (*parent)(strName);
size = arr.size();
chain.push_back(&arr);
2015-07-15 12:23:00 +00:00
idxs.push_back(0);
return true;
}
2015-07-15 12:23:00 +00:00
size = 0;
return false;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
void JsonInputValueSerializer::endArray() {
assert(!chain.empty());
assert(!idxs.empty());
2014-09-15 17:33:47 +00:00
chain.pop_back();
idxs.pop_back();
}
2015-07-30 15:22:07 +00:00
bool JsonInputValueSerializer::operator()(uint16_t& value, Common::StringView name) {
return getNumber(name, value);
}
bool JsonInputValueSerializer::operator()(int16_t& value, Common::StringView name) {
return getNumber(name, value);
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(uint32_t& value, Common::StringView name) {
return getNumber(name, value);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(int32_t& value, Common::StringView name) {
return getNumber(name, value);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(int64_t& value, Common::StringView name) {
return getNumber(name, value);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(uint64_t& value, Common::StringView name) {
return getNumber(name, value);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(double& value, Common::StringView name) {
return getNumber(name, value);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(uint8_t& value, Common::StringView name) {
return getNumber(name, value);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(std::string& value, Common::StringView name) {
auto ptr = getValue(name);
2015-07-30 15:22:07 +00:00
if (ptr == nullptr) {
2015-07-15 12:23:00 +00:00
return false;
}
value = ptr->getString();
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::operator()(bool& value, Common::StringView name) {
auto ptr = getValue(name);
2015-07-30 15:22:07 +00:00
if (ptr == nullptr) {
2015-07-15 12:23:00 +00:00
return false;
}
value = ptr->getBool();
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-30 15:22:07 +00:00
bool JsonInputValueSerializer::binary(void* value, size_t size, Common::StringView name) {
2015-07-15 12:23:00 +00:00
auto ptr = getValue(name);
if (ptr == nullptr) {
return false;
2015-05-27 12:08:46 +00:00
}
2015-07-15 12:23:00 +00:00
Common::fromHex(ptr->getString(), value, size);
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool JsonInputValueSerializer::binary(std::string& value, Common::StringView name) {
auto ptr = getValue(name);
if (ptr == nullptr) {
return false;
}
std::string valueHex = ptr->getString();
value = Common::asString(Common::fromHex(valueHex));
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
const JsonValue* JsonInputValueSerializer::getValue(Common::StringView name) {
const JsonValue& val = *chain.back();
2015-07-15 12:23:00 +00:00
if (val.isArray()) {
return &val[idxs.back()++];
}
2014-09-15 17:33:47 +00:00
2015-07-15 12:23:00 +00:00
std::string strName(name);
return val.contains(strName) ? &val(strName) : nullptr;
2014-09-15 17:33:47 +00:00
}