2016-02-13 09:47:40 +02:00
|
|
|
/*
|
|
|
|
* JsonDeserializer.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 "JsonDeserializer.h"
|
|
|
|
|
|
|
|
#include "../JsonNode.h"
|
|
|
|
|
|
|
|
JsonDeserializer::JsonDeserializer(JsonNode & root_):
|
2016-02-14 13:22:46 +02:00
|
|
|
JsonSerializeFormat(root_, false)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
void JsonDeserializer::serializeBool(const std::string & fieldName, bool & value)
|
|
|
|
{
|
|
|
|
value = current->operator[](fieldName).Bool();
|
|
|
|
}
|
|
|
|
|
2016-02-23 15:36:21 +02:00
|
|
|
void JsonDeserializer::serializeEnum(const std::string & fieldName, const std::string & trueValue, const std::string & falseValue, bool & value)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-22 01:37:19 +02:00
|
|
|
void JsonDeserializer::serializeIntId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const si32 defaultValue, si32 & value)
|
|
|
|
{
|
|
|
|
std::string identifier;
|
|
|
|
serializeString(fieldName, identifier);
|
|
|
|
|
|
|
|
if(identifier == "")
|
|
|
|
{
|
|
|
|
value = defaultValue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 rawId = decoder(identifier);
|
|
|
|
if(rawId >= 0)
|
|
|
|
value = rawId;
|
|
|
|
else
|
|
|
|
value = defaultValue;
|
|
|
|
}
|
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
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;
|
|
|
|
|
2016-02-23 15:36:21 +02:00
|
|
|
const JsonNode & anyOf = field["anyOf"];
|
|
|
|
const JsonNode & allOf = field["allOf"];
|
|
|
|
const JsonNode & noneOf = field["noneOf"];
|
2016-02-21 19:58:09 +02:00
|
|
|
|
2016-02-23 15:36:21 +02:00
|
|
|
if(anyOf.Vector().empty() && allOf.Vector().empty())
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
|
|
|
//permissive mode
|
|
|
|
value = standard;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//restrictive mode
|
|
|
|
value.clear();
|
|
|
|
value.resize(standard.size(), false);
|
|
|
|
|
2016-02-23 15:36:21 +02:00
|
|
|
readLICPart(anyOf, decoder, true, value);
|
|
|
|
readLICPart(allOf, decoder, true, value);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-02-23 15:36:21 +02:00
|
|
|
readLICPart(noneOf, decoder, false, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonDeserializer::serializeLIC(const std::string & fieldName, LIC & value)
|
|
|
|
{
|
|
|
|
const JsonNode & field = current->operator[](fieldName);
|
|
|
|
|
|
|
|
const JsonNode & anyOf = field["anyOf"];
|
|
|
|
const JsonNode & allOf = field["allOf"];
|
|
|
|
const JsonNode & noneOf = field["noneOf"];
|
|
|
|
|
|
|
|
if(anyOf.Vector().empty())
|
|
|
|
{
|
|
|
|
//permissive mode
|
|
|
|
value.any = value.standard;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//restrictive mode
|
|
|
|
value.any.clear();
|
|
|
|
value.any.resize(value.standard.size(), false);
|
|
|
|
|
|
|
|
readLICPart(anyOf, value.decoder, true, value.any);
|
|
|
|
}
|
|
|
|
|
|
|
|
readLICPart(allOf, value.decoder, true, value.all);
|
|
|
|
readLICPart(noneOf, value.decoder, true, value.none);
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
|
|
|
|
{
|
|
|
|
value = current->operator[](fieldName).String();
|
|
|
|
}
|
2016-02-13 09:47:40 +02:00
|
|
|
|
2016-02-23 15:36:21 +02:00
|
|
|
void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
|
|
|
|
{
|
|
|
|
for(size_t index = 0; index < part.Vector().size(); index++)
|
|
|
|
{
|
|
|
|
const std::string & identifier = part.Vector()[index].String();
|
|
|
|
|
|
|
|
const si32 rawId = decoder(identifier);
|
|
|
|
if(rawId >= 0)
|
|
|
|
{
|
|
|
|
if(rawId < value.size())
|
|
|
|
value[rawId] = val;
|
|
|
|
else
|
|
|
|
logGlobal->errorStream() << "JsonDeserializer::serializeLIC: id out of bounds " << rawId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|