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"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
if(!defaultValue || defaultValue.value() != value)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
std::string identifier = encoder(value);
|
|
|
|
serializeString(fieldName, identifier);
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2024-02-15 00:30:29 +02:00
|
|
|
if(!defaultValue || !vstd::isAlmostEqual(defaultValue.value(), value))
|
2017-07-20 06:08:49 +02:00
|
|
|
currentObject->operator[](fieldName).Float() = value;
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
if(!defaultValue || defaultValue.value() != value)
|
2017-07-20 06:08:49 +02:00
|
|
|
currentObject->operator[](fieldName).Integer() = value;
|
2016-11-13 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
if(!defaultValue || defaultValue.value() != 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)
|
2024-02-13 13:18:10 +02:00
|
|
|
data.emplace_back(rawId);
|
2023-09-04 21:23:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector<std::string> & value)
|
|
|
|
{
|
|
|
|
if(value.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
JsonVector & data = currentObject->operator[](fieldName).Vector();
|
|
|
|
data.reserve(value.size());
|
|
|
|
|
|
|
|
for(const auto & rawId : value)
|
2024-02-13 13:18:10 +02:00
|
|
|
data.emplace_back(rawId);
|
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
|
|
|
}
|
|
|
|
|
2023-11-05 16:35:51 +02:00
|
|
|
void JsonSerializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<int32_t> & standard, std::set<int32_t> & value)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
|
|
|
if(standard == value)
|
|
|
|
return;
|
2016-02-23 15:36:21 +02:00
|
|
|
|
|
|
|
writeLICPart(fieldName, "anyOf", encoder, value);
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonSerializer::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
if(!defaultValue || value != defaultValue.value())
|
2017-07-20 06:08:49 +02:00
|
|
|
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)
|
2024-02-13 13:18:10 +02:00
|
|
|
target.emplace_back(s);
|
2016-02-23 15:36:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|