JsonDeserializer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * JsonDeserializer.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "JsonDeserializer.h"
  12. #include "../JsonNode.h"
  13. JsonDeserializer::JsonDeserializer(const IInstanceResolver * instanceResolver_, const JsonNode & root_):
  14. JsonTreeSerializer(instanceResolver_, &root_, false)
  15. {
  16. }
  17. void JsonDeserializer::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
  18. {
  19. const JsonNode & data = currentObject->operator[](fieldName);
  20. if(data.getType() != JsonNode::JsonType::DATA_BOOL)
  21. value = boost::logic::indeterminate;
  22. else
  23. value = data.Bool();
  24. }
  25. void JsonDeserializer::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
  26. {
  27. std::string identifier;
  28. serializeString(fieldName, identifier);
  29. value = defaultValue ? defaultValue.get() : 0;
  30. if(identifier != "")
  31. {
  32. si32 rawId = decoder(identifier);
  33. if(rawId < 0) //may be, user has installed the mod into another directory...
  34. {
  35. auto internalId = vstd::splitStringToPair(identifier, ':').second;
  36. auto currentScope = getCurrent().meta;
  37. auto actualId = currentScope.length() > 0 ? currentScope + ":" + internalId : internalId;
  38. rawId = decoder(actualId);
  39. if(rawId >= 0)
  40. logMod->warn("Identifier %s has been resolved as %s instead of %s", internalId, actualId, identifier);
  41. }
  42. if(rawId >= 0)
  43. value = rawId;
  44. }
  45. }
  46. void JsonDeserializer::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
  47. {
  48. const JsonVector & data = currentObject->operator[](fieldName).Vector();
  49. value.clear();
  50. value.reserve(data.size());
  51. for(const JsonNode elem : data)
  52. {
  53. si32 rawId = decoder(elem.String());
  54. if(rawId >= 0)
  55. value.push_back(rawId);
  56. }
  57. }
  58. void JsonDeserializer::serializeInternal(const std::string & fieldName, double & value, const boost::optional<double> & defaultValue)
  59. {
  60. const JsonNode & data = currentObject->operator[](fieldName);
  61. if(!data.isNumber())
  62. value = defaultValue ? defaultValue.get() : 0;//todo: report error on not null?
  63. else
  64. value = data.Float();
  65. }
  66. void JsonDeserializer::serializeInternal(const std::string & fieldName, si64 & value, const boost::optional<si64> & defaultValue)
  67. {
  68. const JsonNode & data = currentObject->operator[](fieldName);
  69. if(!data.isNumber())
  70. value = defaultValue ? defaultValue.get() : 0;//todo: report error on not null?
  71. else
  72. value = data.Integer();
  73. }
  74. void JsonDeserializer::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
  75. {
  76. const std::string & valueName = currentObject->operator[](fieldName).String();
  77. const si32 actualOptional = defaultValue ? defaultValue.get() : 0;
  78. si32 rawValue = vstd::find_pos(enumMap, valueName);
  79. if(rawValue < 0)
  80. value = actualOptional;
  81. else
  82. value = rawValue;
  83. }
  84. void JsonDeserializer::serializeInternal(std::string & value)
  85. {
  86. value = currentObject->String();
  87. }
  88. void JsonDeserializer::serializeInternal(int64_t & value)
  89. {
  90. value = currentObject->Integer();
  91. }
  92. void JsonDeserializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  93. {
  94. const JsonNode & field = currentObject->operator[](fieldName);
  95. const JsonNode & anyOf = field["anyOf"];
  96. const JsonNode & allOf = field["allOf"];
  97. const JsonNode & noneOf = field["noneOf"];
  98. if(anyOf.Vector().empty() && allOf.Vector().empty())
  99. {
  100. //permissive mode
  101. value = standard;
  102. }
  103. else
  104. {
  105. //restrictive mode
  106. value.clear();
  107. value.resize(standard.size(), false);
  108. readLICPart(anyOf, decoder, true, value);
  109. readLICPart(allOf, decoder, true, value);
  110. }
  111. readLICPart(noneOf, decoder, false, value);
  112. }
  113. void JsonDeserializer::serializeLIC(const std::string & fieldName, LIC & value)
  114. {
  115. const JsonNode & field = currentObject->operator[](fieldName);
  116. const JsonNode & anyOf = field["anyOf"];
  117. const JsonNode & allOf = field["allOf"];
  118. const JsonNode & noneOf = field["noneOf"];
  119. if(anyOf.Vector().empty())
  120. {
  121. //permissive mode
  122. value.any = value.standard;
  123. }
  124. else
  125. {
  126. //restrictive mode
  127. value.any.clear();
  128. value.any.resize(value.standard.size(), false);
  129. readLICPart(anyOf, value.decoder, true, value.any);
  130. }
  131. readLICPart(allOf, value.decoder, true, value.all);
  132. readLICPart(noneOf, value.decoder, true, value.none);
  133. //remove any banned from allowed and required
  134. for(si32 idx = 0; idx < value.none.size(); idx++)
  135. {
  136. if(value.none[idx])
  137. {
  138. value.all[idx] = false;
  139. value.any[idx] = false;
  140. }
  141. }
  142. //add all required to allowed
  143. for(si32 idx = 0; idx < value.all.size(); idx++)
  144. {
  145. if(value.all[idx])
  146. {
  147. value.any[idx] = true;
  148. }
  149. }
  150. }
  151. void JsonDeserializer::serializeLIC(const std::string & fieldName, LICSet & value)
  152. {
  153. const JsonNode & field = currentObject->operator[](fieldName);
  154. const JsonNode & anyOf = field["anyOf"];
  155. const JsonNode & allOf = field["allOf"];
  156. const JsonNode & noneOf = field["noneOf"];
  157. value.all.clear();
  158. value.none.clear();
  159. if(anyOf.Vector().empty())
  160. {
  161. //permissive mode
  162. value.any = value.standard;
  163. }
  164. else
  165. {
  166. //restrictive mode
  167. value.any.clear();
  168. readLICPart(anyOf, value.decoder, value.any);
  169. for(si32 item : value.standard)
  170. if(!vstd::contains(value.any, item))
  171. value.none.insert(item);
  172. }
  173. readLICPart(allOf, value.decoder, value.all);
  174. readLICPart(noneOf, value.decoder, value.none);
  175. //remove any banned from allowed and required
  176. auto isBanned = [&value](const si32 item)->bool
  177. {
  178. return vstd::contains(value.none, item);
  179. };
  180. vstd::erase_if(value.all, isBanned);
  181. vstd::erase_if(value.any, isBanned);
  182. //add all required to allowed
  183. for(si32 item : value.all)
  184. {
  185. value.any.insert(item);
  186. }
  187. }
  188. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  189. {
  190. value = currentObject->operator[](fieldName).String();
  191. }
  192. void JsonDeserializer::serializeRaw(const std::string & fieldName, JsonNode & value, const boost::optional<const JsonNode &> defaultValue)
  193. {
  194. const JsonNode & data = currentObject->operator[](fieldName);
  195. if(data.getType() == JsonNode::JsonType::DATA_NULL)
  196. {
  197. if(defaultValue)
  198. value = defaultValue.get();
  199. else
  200. value.clear();
  201. }
  202. else
  203. {
  204. value = data;
  205. }
  206. }
  207. void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
  208. {
  209. for(size_t index = 0; index < part.Vector().size(); index++)
  210. {
  211. const std::string & identifier = part.Vector()[index].String();
  212. const si32 rawId = decoder(identifier);
  213. if(rawId >= 0)
  214. {
  215. if(rawId < value.size())
  216. value[rawId] = val;
  217. else
  218. logGlobal->error("JsonDeserializer::serializeLIC: id out of bounds %d", rawId);
  219. }
  220. }
  221. }
  222. void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value)
  223. {
  224. for(size_t index = 0; index < part.Vector().size(); index++)
  225. {
  226. const std::string & identifier = part.Vector()[index].String();
  227. const si32 rawId = decoder(identifier);
  228. if(rawId != -1)
  229. value.insert(rawId);
  230. }
  231. }