JsonDeserializer.cpp 6.8 KB

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