123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- /*
- * 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::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();
- }
|