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"
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
//JsonSerializeHelper
|
|
|
|
JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper && other):
|
2016-02-13 09:47:40 +02:00
|
|
|
owner(other.owner),
|
2016-11-13 12:38:42 +02:00
|
|
|
thisNode(other.thisNode),
|
2016-02-13 09:47:40 +02:00
|
|
|
parentNode(other.parentNode),
|
2016-11-13 12:38:42 +02:00
|
|
|
restoreState(false)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
std::swap(restoreState, other.restoreState);
|
2016-02-13 09:47:40 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonSerializeHelper::~JsonSerializeHelper()
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
if(restoreState)
|
|
|
|
owner.current = parentNode;
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
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_),
|
2016-11-13 12:38:42 +02:00
|
|
|
thisNode(thisNode_),
|
2016-02-13 09:47:40 +02:00
|
|
|
parentNode(owner.current),
|
2016-11-13 12:38:42 +02:00
|
|
|
restoreState(true)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
owner.current = thisNode;
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper & parent, const std::string & fieldName):
|
2016-02-13 09:47:40 +02:00
|
|
|
owner(parent.owner),
|
2016-11-13 12:38:42 +02:00
|
|
|
thisNode(&(parent.thisNode->operator[](fieldName))),
|
2016-02-13 09:47:40 +02:00
|
|
|
parentNode(parent.thisNode),
|
2016-11-13 12:38:42 +02:00
|
|
|
restoreState(true)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
owner.current = thisNode;
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonStructSerializer JsonSerializeHelper::enterStruct(const std::string & fieldName)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
return JsonStructSerializer(*this, fieldName);
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonArraySerializer JsonSerializeHelper::enterArray(const std::string & fieldName)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
return JsonArraySerializer(*this, fieldName);
|
2016-02-13 09:47:40 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +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
|
|
|
{
|
2016-11-13 12:38:42 +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
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +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);
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
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
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonSerializeFormat::JsonSerializeFormat(const IInstanceResolver * instanceResolver_, JsonNode & root_, const bool saving_):
|
2016-02-14 13:22:46 +02:00
|
|
|
saving(saving_),
|
2016-02-13 09:47:40 +02:00
|
|
|
root(&root_),
|
2016-11-13 12:38:42 +02:00
|
|
|
current(root),
|
|
|
|
instanceResolver(instanceResolver_)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonStructSerializer JsonSerializeFormat::enterStruct(const std::string & fieldName)
|
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
return JsonStructSerializer(*this, fieldName);
|
|
|
|
}
|
2016-02-13 09:47:40 +02:00
|
|
|
|
2016-11-13 12:38:42 +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
|
|
|
}
|