1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00
vcmi/lib/serializer/JsonSerializeFormat.cpp

193 lines
4.7 KiB
C++
Raw Normal View History

2016-02-13 09:47:40 +02:00
/*
* JsonSerializeFormat.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "JsonSerializeFormat.h"
#include "../JsonNode.h"
//JsonSerializeHelper
JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper && other):
2016-02-13 09:47:40 +02:00
owner(other.owner),
thisNode(other.thisNode),
2016-02-13 09:47:40 +02:00
parentNode(other.parentNode),
restoreState(false)
2016-02-13 09:47:40 +02:00
{
std::swap(restoreState, other.restoreState);
2016-02-13 09:47:40 +02:00
}
JsonSerializeHelper::~JsonSerializeHelper()
2016-02-13 09:47:40 +02:00
{
if(restoreState)
owner.current = parentNode;
}
JsonNode & JsonSerializeHelper::get()
{
return * thisNode;
}
JsonSerializeFormat * JsonSerializeHelper::operator->()
{
return &owner;
}
JsonSerializeHelper::JsonSerializeHelper(JsonSerializeFormat & owner_, JsonNode * thisNode_):
2016-02-13 09:47:40 +02:00
owner(owner_),
thisNode(thisNode_),
2016-02-13 09:47:40 +02:00
parentNode(owner.current),
restoreState(true)
2016-02-13 09:47:40 +02:00
{
owner.current = thisNode;
}
JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper & parent, const std::string & fieldName):
2016-02-13 09:47:40 +02:00
owner(parent.owner),
thisNode(&(parent.thisNode->operator[](fieldName))),
2016-02-13 09:47:40 +02:00
parentNode(parent.thisNode),
restoreState(true)
2016-02-13 09:47:40 +02:00
{
owner.current = thisNode;
}
JsonStructSerializer JsonSerializeHelper::enterStruct(const std::string & fieldName)
2016-02-13 09:47:40 +02:00
{
return JsonStructSerializer(*this, fieldName);
}
JsonArraySerializer JsonSerializeHelper::enterArray(const std::string & fieldName)
2016-02-13 09:47:40 +02:00
{
return JsonArraySerializer(*this, fieldName);
2016-02-13 09:47:40 +02:00
}
//JsonStructSerializer
JsonStructSerializer::JsonStructSerializer(JsonStructSerializer && other):
JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))),
optional(other.optional)
2016-02-13 09:47:40 +02:00
{
}
JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat & owner_, const std::string & fieldName):
JsonSerializeHelper(owner_, &(owner_.current->operator[](fieldName))),
optional(false)
{
if(owner.saving)
thisNode->setType(JsonNode::DATA_STRUCT);
}
JsonStructSerializer::JsonStructSerializer(JsonSerializeHelper & parent, const std::string & fieldName):
JsonSerializeHelper(parent, fieldName),
optional(false)
{
if(owner.saving)
thisNode->setType(JsonNode::DATA_STRUCT);
}
JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat & owner_, JsonNode * thisNode_):
JsonSerializeHelper(owner_, thisNode_),
optional(false)
{
if(owner.saving)
thisNode->setType(JsonNode::DATA_STRUCT);
}
JsonStructSerializer::~JsonStructSerializer()
{
//todo: delete empty optional node
if(owner.saving && optional && thisNode->Struct().empty())
{
//
}
}
//JsonArraySerializer
JsonArraySerializer::JsonArraySerializer(JsonStructSerializer && other):
JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other)))
{
2016-02-13 09:47:40 +02:00
}
JsonArraySerializer::JsonArraySerializer(JsonSerializeFormat & owner_, const std::string & fieldName):
JsonSerializeHelper(owner_, &(owner_.current->operator[](fieldName)))
{
}
JsonArraySerializer::JsonArraySerializer(JsonSerializeHelper & parent, const std::string & fieldName):
JsonSerializeHelper(parent, fieldName)
{
}
JsonStructSerializer JsonArraySerializer::enterStruct(const size_t index)
{
return JsonStructSerializer(owner, &(thisNode->Vector()[index]));
}
void JsonArraySerializer::resize(const size_t newSize)
{
thisNode->Vector().resize(newSize);
}
void JsonArraySerializer::resize(const size_t newSize, JsonNode::JsonType type)
{
resize(newSize);
for(JsonNode & n : thisNode->Vector())
if(n.getType() == JsonNode::DATA_NULL)
n.setType(type);
}
size_t JsonArraySerializer::size() const
{
return thisNode->Vector().size();
}
//JsonSerializeFormat::LIC
JsonSerializeFormat::LIC::LIC(const std::vector<bool> & Standard, const TDecoder Decoder, const TEncoder Encoder):
2016-02-23 15:36:21 +02:00
standard(Standard), decoder(Decoder), encoder(Encoder)
{
2016-02-25 21:59:17 +02:00
any.resize(standard.size(), false);
2016-02-23 15:36:21 +02:00
all.resize(standard.size(), false);
none.resize(standard.size(), false);
}
JsonSerializeFormat::LICSet::LICSet(const std::set<si32>& Standard, const TDecoder Decoder, const TEncoder Encoder):
standard(Standard), decoder(Decoder), encoder(Encoder)
{
}
2016-02-13 09:47:40 +02:00
//JsonSerializeFormat
JsonSerializeFormat::JsonSerializeFormat(const IInstanceResolver * instanceResolver_, JsonNode & root_, const bool saving_):
saving(saving_),
2016-02-13 09:47:40 +02:00
root(&root_),
current(root),
instanceResolver(instanceResolver_)
2016-02-13 09:47:40 +02:00
{
}
JsonStructSerializer JsonSerializeFormat::enterStruct(const std::string & fieldName)
{
return JsonStructSerializer(*this, fieldName);
}
2016-02-13 09:47:40 +02:00
JsonArraySerializer JsonSerializeFormat::enterArray(const std::string & fieldName)
{
return JsonArraySerializer(*this, fieldName);
}
void JsonSerializeFormat::serializeBool(const std::string & fieldName, bool & value)
{
serializeBool<bool>(fieldName, value, true, false, false);
2016-02-13 09:47:40 +02:00
}