123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*
- * 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_):
- 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::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;
- }
- 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;
- auto loadPart = [&](const JsonVector & part, const bool val)
- {
- for(size_t index = 0; index < part.size(); index++)
- {
- const std::string & identifier = part[index].String();
- si32 rawId = decoder(identifier);
- if(rawId >= 0)
- {
- if(rawId < value.size())
- value[rawId] = val;
- else
- logGlobal->errorStream() << "JsonDeserializer::serializeLIC: " << fieldName <<" id out of bounds " << rawId;
- }
- else
- {
- logGlobal->errorStream() << "JsonDeserializer::serializeLIC: " << fieldName <<" identifier not resolved " << identifier;
- }
- }
- };
- 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);
- loadPart(anyOf, true);
- loadPart(allOf, true);
- }
- loadPart(noneOf, false);
- }
- void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
- {
- value = current->operator[](fieldName).String();
- }
|