1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
* 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::serializeEnum(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;
const JsonNode & anyOf = field["anyOf"];
const JsonNode & allOf = field["allOf"];
const JsonNode & noneOf = field["noneOf"];
if(anyOf.Vector().empty() && allOf.Vector().empty())
{
//permissive mode
value = standard;
}
else
{
//restrictive mode
value.clear();
value.resize(standard.size(), false);
readLICPart(anyOf, decoder, true, value);
readLICPart(allOf, decoder, true, value);
}
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);
//remove any banned from allowed and required
for(si32 idx = 0; idx < value.none.size(); idx++)
{
if(value.none[idx])
{
value.all[idx] = false;
value.any[idx] = false;
}
}
//add all required to allowed
for(si32 idx = 0; idx < value.all.size(); idx++)
{
if(value.all[idx])
{
value.any[idx] = true;
}
}
}
void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
{
value = current->operator[](fieldName).String();
}
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;
}
}
}
|