2016-02-13 09:47:40 +02:00
|
|
|
/*
|
|
|
|
* JsonSerializer.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 "JsonSerializer.h"
|
|
|
|
|
|
|
|
#include "../JsonNode.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
JsonSerializer::JsonSerializer(const IInstanceResolver * instanceResolver_, JsonNode & root_):
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
JsonTreeSerializer(instanceResolver_, &root_, true, false)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!boost::logic::indeterminate(value))
|
2019-01-24 16:49:30 +02:00
|
|
|
currentObject->operator[](fieldName).Bool() = (bool)value;
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!defaultValue || defaultValue.get() != value)
|
|
|
|
{
|
|
|
|
std::string identifier = encoder(value);
|
|
|
|
serializeString(fieldName, identifier);
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, double & value, const boost::optional<double> & defaultValue)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!defaultValue || defaultValue.get() != value)
|
2017-07-20 06:08:49 +02:00
|
|
|
currentObject->operator[](fieldName).Float() = value;
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, si64 & value, const boost::optional<si64> & defaultValue)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!defaultValue || defaultValue.get() != value)
|
2017-07-20 06:08:49 +02:00
|
|
|
currentObject->operator[](fieldName).Integer() = value;
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
|
|
|
|
{
|
|
|
|
if(!defaultValue || defaultValue.get() != value)
|
2017-07-20 06:08:49 +02:00
|
|
|
currentObject->operator[](fieldName).String() = enumMap.at(value);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
|
2016-02-22 01:37:19 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
if(value.empty())
|
|
|
|
return;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
JsonVector & data = currentObject->operator[](fieldName).Vector();
|
2016-11-13 12:38:42 +02:00
|
|
|
data.reserve(value.size());
|
|
|
|
|
|
|
|
for(const si32 rawId : value)
|
2016-02-22 01:50:06 +02:00
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode jsonElement(JsonNode::JsonType::DATA_STRING);
|
2016-11-13 12:38:42 +02:00
|
|
|
jsonElement.String() = encoder(rawId);
|
|
|
|
data.push_back(std::move(jsonElement));
|
2016-02-22 01:50:06 +02:00
|
|
|
}
|
2016-02-22 01:37:19 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void JsonSerializer::serializeInternal(std::string & value)
|
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
currentObject->String() = value;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::serializeInternal(int64_t & value)
|
|
|
|
{
|
2018-03-09 20:11:20 +02:00
|
|
|
currentObject->Integer() = value;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
void JsonSerializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
|
|
|
|
{
|
|
|
|
assert(standard.size() == value.size());
|
|
|
|
if(standard == value)
|
|
|
|
return;
|
2016-02-23 15:36:21 +02:00
|
|
|
|
|
|
|
writeLICPart(fieldName, "anyOf", encoder, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::serializeLIC(const std::string & fieldName, LIC & value)
|
|
|
|
{
|
|
|
|
if(value.any != value.standard)
|
|
|
|
writeLICPart(fieldName, "anyOf", value.encoder, value.any);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
writeLICPart(fieldName, "allOf", value.encoder, value.all);
|
|
|
|
writeLICPart(fieldName, "noneOf", value.encoder, value.none);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::serializeLIC(const std::string & fieldName, LICSet & value)
|
|
|
|
{
|
|
|
|
if(value.any != value.standard)
|
|
|
|
writeLICPart(fieldName, "anyOf", value.encoder, value.any);
|
2016-02-23 15:36:21 +02:00
|
|
|
|
|
|
|
writeLICPart(fieldName, "allOf", value.encoder, value.all);
|
|
|
|
writeLICPart(fieldName, "noneOf", value.encoder, value.none);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
|
|
|
|
{
|
2022-11-15 02:20:55 +02:00
|
|
|
if(!value.empty())
|
2017-07-20 06:08:49 +02:00
|
|
|
currentObject->operator[](fieldName).String() = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::serializeRaw(const std::string & fieldName, JsonNode & value, const boost::optional<const JsonNode &> defaultValue)
|
|
|
|
{
|
|
|
|
if(!defaultValue || value != defaultValue.get())
|
|
|
|
currentObject->operator[](fieldName) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::pushStruct(const std::string & fieldName)
|
|
|
|
{
|
|
|
|
JsonTreeSerializer::pushStruct(fieldName);
|
|
|
|
currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::pushArray(const std::string & fieldName)
|
|
|
|
{
|
|
|
|
JsonTreeSerializer::pushArray(fieldName);
|
|
|
|
currentObject->setType(JsonNode::JsonType::DATA_VECTOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::pushArrayElement(const size_t index)
|
|
|
|
{
|
|
|
|
JsonTreeSerializer::pushArrayElement(index);
|
|
|
|
currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::resizeCurrent(const size_t newSize, JsonNode::JsonType type)
|
|
|
|
{
|
|
|
|
currentObject->Vector().resize(newSize);
|
|
|
|
if(type != JsonNode::JsonType::DATA_NULL)
|
|
|
|
{
|
|
|
|
for(JsonNode & n : currentObject->Vector())
|
|
|
|
if(n.getType() == JsonNode::JsonType::DATA_NULL)
|
|
|
|
n.setType(type);
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
2016-02-13 09:47:40 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::vector<bool> & data)
|
2016-02-23 15:36:21 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
std::vector<std::string> buf;
|
|
|
|
buf.reserve(data.size());
|
|
|
|
|
|
|
|
for(si32 idx = 0; idx < data.size(); idx++)
|
2016-02-23 15:36:21 +02:00
|
|
|
if(data[idx])
|
2016-11-13 12:38:42 +02:00
|
|
|
buf.push_back(encoder(idx));
|
|
|
|
|
|
|
|
writeLICPartBuffer(fieldName, partName, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::set<si32> & data)
|
|
|
|
{
|
|
|
|
std::vector<std::string> buf;
|
|
|
|
buf.reserve(data.size());
|
|
|
|
|
|
|
|
for(const si32 item : data)
|
|
|
|
buf.push_back(encoder(item));
|
|
|
|
|
|
|
|
writeLICPartBuffer(fieldName, partName, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::writeLICPartBuffer(const std::string & fieldName, const std::string & partName, std::vector<std::string> & buffer)
|
|
|
|
{
|
|
|
|
if(!buffer.empty())
|
|
|
|
{
|
|
|
|
std::sort(buffer.begin(), buffer.end());
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
auto & target = currentObject->operator[](fieldName)[partName].Vector();
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
for(auto & s : buffer)
|
2016-02-23 15:36:21 +02:00
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode val(JsonNode::JsonType::DATA_STRING);
|
2016-11-13 12:38:42 +02:00
|
|
|
std::swap(val.String(), s);
|
2016-02-23 15:36:21 +02:00
|
|
|
target.push_back(std::move(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|