1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Advance Json serializer

* added bool, numeric and string support
* added some enum support
* PoC implementation of logical id condition
* Refactoring
This commit is contained in:
AlexVinS
2016-02-14 14:22:46 +03:00
parent 67b03b9658
commit ec760632a6
11 changed files with 517 additions and 248 deletions

View File

@@ -15,9 +15,94 @@
#include "../JsonNode.h"
JsonDeserializer::JsonDeserializer(JsonNode & root_):
JsonSerializeFormat(root_)
JsonSerializeFormat(root_, false)
{
}
void JsonDeserializer::serializeBool(const std::string & fieldName, bool & value)
{
value = current->operator[](fieldName).Bool();
}
void JsonDeserializer::serializeBoolEnum(const std::string & fieldName, const std::string & trueValue, const std::string & falseValue, bool & value)
{
const JsonNode & tmp = current->operator[](fieldName);
value = tmp.String() == trueValue;
}
void JsonDeserializer::serializeFloat(const std::string & fieldName, double & value)
{
value = current->operator[](fieldName).Float();
}
void JsonDeserializer::serializeIntEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const si32 defaultValue, si32 & value)
{
const std::string & valueName = current->operator[](fieldName).String();
si32 rawValue = vstd::find_pos(enumMap, valueName);
if(rawValue < 0)
value = defaultValue;
else
value = rawValue;
}
void JsonDeserializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
{
const JsonNode & field = current->operator[](fieldName);
if(field.isNull())
return;
const JsonVector & anyOf = field["anyOf"].Vector();
const JsonVector & allOf = field["allOf"].Vector();
const JsonVector & noneOf = field["noneOf"].Vector();
if(anyOf.empty() && allOf.empty())
{
//permissive mode
value = standard;
}
else
{
//restrictive mode
value.clear();
value.resize(standard.size(), false);
for(size_t index = 0; index < anyOf.size(); index++)
{
const std::string & identifier = anyOf[index].String();
si32 rawId = decoder(identifier);
if(rawId >= 0)
value[rawId] = true;
}
for(size_t index = 0; index < allOf.size(); index++)
{
const std::string & identifier = allOf[index].String();
si32 rawId = decoder(identifier);
if(rawId >=0)
value[rawId] = true;
}
}
for(size_t index = 0; index < noneOf.size(); index++)
{
const std::string & identifier = noneOf[index].String();
si32 rawId = decoder(identifier);
if(rawId >=0 )
value[rawId] = false;
}
}
void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
{
value = current->operator[](fieldName).String();
}