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"
|
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
|
|
|
|
|
|
|
JsonInputValueSerializer::JsonInputValueSerializer() : root(nullptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonInputValueSerializer::~JsonInputValueSerializer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonInputValueSerializer::setJsonValue(const JsonValue* value) {
|
|
|
|
root = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer::SerializerType JsonInputValueSerializer::type() const {
|
|
|
|
return ISerializer::INPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::beginObject(const std::string& name) {
|
|
|
|
assert(root);
|
|
|
|
|
|
|
|
if (chain.size() == 0) {
|
|
|
|
chain.push_back(root);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonValue* parent = chain.back();
|
|
|
|
if (parent->isArray()) {
|
|
|
|
const JsonValue& v = (*parent)[idxs.back()++];
|
|
|
|
chain.push_back(&v);
|
|
|
|
} else {
|
|
|
|
const JsonValue& v = (*parent)(name);
|
|
|
|
chain.push_back(&v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::endObject() {
|
|
|
|
assert(root);
|
|
|
|
|
|
|
|
chain.pop_back();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::beginArray(std::size_t& size, const std::string& name) {
|
|
|
|
assert(root);
|
|
|
|
|
|
|
|
const JsonValue* parent = chain.back();
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
if (parent->count(name)) {
|
|
|
|
const JsonValue& arr = (*parent)(name);
|
|
|
|
size = arr.size();
|
|
|
|
chain.push_back(&arr);
|
|
|
|
} else {
|
|
|
|
size = 0;
|
|
|
|
chain.push_back(0);
|
|
|
|
}
|
2014-09-15 17:33:47 +00:00
|
|
|
|
|
|
|
idxs.push_back(0);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::endArray() {
|
|
|
|
assert(root);
|
|
|
|
|
|
|
|
chain.pop_back();
|
|
|
|
idxs.pop_back();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(uint32_t& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-04-06 16:13:07 +00:00
|
|
|
value = static_cast<uint32_t>(getNumber(name));
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(int32_t& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-04-06 16:13:07 +00:00
|
|
|
value = static_cast<int32_t>(getNumber(name));
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(int64_t& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-04-06 16:13:07 +00:00
|
|
|
value = getNumber(name);
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(uint64_t& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-04-06 16:13:07 +00:00
|
|
|
value = static_cast<uint64_t>(getNumber(name));
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(double& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-05-27 12:08:46 +00:00
|
|
|
value = getValue(name).getReal();
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(std::string& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-04-06 16:13:07 +00:00
|
|
|
value = getValue(name).getString();
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(uint8_t& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-04-06 16:13:07 +00:00
|
|
|
value = static_cast<uint8_t>(getNumber(name));
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISerializer& JsonInputValueSerializer::operator()(bool& value, const std::string& name) {
|
|
|
|
assert(root);
|
2015-04-06 16:13:07 +00:00
|
|
|
value = getValue(name).getBool();
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonInputValueSerializer::hasObject(const std::string& name) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(root);
|
|
|
|
|
|
|
|
const Common::JsonValue* value;
|
|
|
|
if (chain.empty()) {
|
|
|
|
value = root;
|
|
|
|
} else {
|
|
|
|
value = chain.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value->count(name) != 0;
|
2014-09-15 17:33:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
ISerializer& JsonInputValueSerializer::binary(void* value, std::size_t size, const std::string& name) {
|
2015-05-27 12:08:46 +00:00
|
|
|
auto str = getValue(name).getString();
|
|
|
|
Common::fromHex(str, value, size);
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
ISerializer& JsonInputValueSerializer::binary(std::string& value, const std::string& name) {
|
2015-05-27 12:08:46 +00:00
|
|
|
auto str = getValue(name).getString();
|
|
|
|
value = Common::asString(Common::fromHex(str));
|
2014-09-15 17:33:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
JsonValue JsonInputValueSerializer::getValue(const std::string& name) {
|
|
|
|
const JsonValue& val = *chain.back();
|
|
|
|
return val.isArray() ? val[idxs.back()++] : val(name);
|
2014-09-15 17:33:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
int64_t JsonInputValueSerializer::getNumber(const std::string& name) {
|
2015-05-27 12:08:46 +00:00
|
|
|
return getValue(name).getInteger();
|
2014-09-15 17:33:47 +00:00
|
|
|
}
|