JsonDeserializer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 boost::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
  28. {
  29. std::string identifier;
  30. serializeString(fieldName, identifier);
  31. value = defaultValue ? defaultValue.get() : 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 boost::optional<double> & defaultValue)
  61. {
  62. const JsonNode & data = currentObject->operator[](fieldName);
  63. if(!data.isNumber())
  64. value = defaultValue ? defaultValue.get() : 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 boost::optional<si64> & defaultValue)
  69. {
  70. const JsonNode & data = currentObject->operator[](fieldName);
  71. if(!data.isNumber())
  72. value = defaultValue ? defaultValue.get() : 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 boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
  77. {
  78. const std::string & valueName = currentObject->operator[](fieldName).String();
  79. const si32 actualOptional = defaultValue ? defaultValue.get() : 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(std::string & value)
  87. {
  88. value = currentObject->String();
  89. }
  90. void JsonDeserializer::serializeInternal(int64_t & value)
  91. {
  92. value = currentObject->Integer();
  93. }
  94. void JsonDeserializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  95. {
  96. const JsonNode & field = currentObject->operator[](fieldName);
  97. const JsonNode & anyOf = field["anyOf"];
  98. const JsonNode & allOf = field["allOf"];
  99. const JsonNode & noneOf = field["noneOf"];
  100. if(anyOf.Vector().empty() && allOf.Vector().empty())
  101. {
  102. //permissive mode
  103. value = standard;
  104. }
  105. else
  106. {
  107. //restrictive mode
  108. value.clear();
  109. value.resize(standard.size(), false);
  110. readLICPart(anyOf, decoder, true, value);
  111. readLICPart(allOf, decoder, true, value);
  112. }
  113. readLICPart(noneOf, decoder, false, value);
  114. }
  115. void JsonDeserializer::serializeLIC(const std::string & fieldName, LIC & value)
  116. {
  117. const JsonNode & field = currentObject->operator[](fieldName);
  118. const JsonNode & anyOf = field["anyOf"];
  119. const JsonNode & allOf = field["allOf"];
  120. const JsonNode & noneOf = field["noneOf"];
  121. if(anyOf.Vector().empty())
  122. {
  123. //permissive mode
  124. value.any = value.standard;
  125. }
  126. else
  127. {
  128. //restrictive mode
  129. value.any.clear();
  130. value.any.resize(value.standard.size(), false);
  131. readLICPart(anyOf, value.decoder, true, value.any);
  132. }
  133. readLICPart(allOf, value.decoder, true, value.all);
  134. readLICPart(noneOf, value.decoder, true, value.none);
  135. //remove any banned from allowed and required
  136. for(si32 idx = 0; idx < value.none.size(); idx++)
  137. {
  138. if(value.none[idx])
  139. {
  140. value.all[idx] = false;
  141. value.any[idx] = false;
  142. }
  143. }
  144. //add all required to allowed
  145. for(si32 idx = 0; idx < value.all.size(); idx++)
  146. {
  147. if(value.all[idx])
  148. {
  149. value.any[idx] = true;
  150. }
  151. }
  152. }
  153. void JsonDeserializer::serializeLIC(const std::string & fieldName, LICSet & value)
  154. {
  155. const JsonNode & field = currentObject->operator[](fieldName);
  156. const JsonNode & anyOf = field["anyOf"];
  157. const JsonNode & allOf = field["allOf"];
  158. const JsonNode & noneOf = field["noneOf"];
  159. value.all.clear();
  160. value.none.clear();
  161. if(anyOf.Vector().empty())
  162. {
  163. //permissive mode
  164. value.any = value.standard;
  165. }
  166. else
  167. {
  168. //restrictive mode
  169. value.any.clear();
  170. readLICPart(anyOf, value.decoder, value.any);
  171. for(si32 item : value.standard)
  172. if(!vstd::contains(value.any, item))
  173. value.none.insert(item);
  174. }
  175. readLICPart(allOf, value.decoder, value.all);
  176. readLICPart(noneOf, value.decoder, value.none);
  177. //remove any banned from allowed and required
  178. auto isBanned = [&value](const si32 item)->bool
  179. {
  180. return vstd::contains(value.none, item);
  181. };
  182. vstd::erase_if(value.all, isBanned);
  183. vstd::erase_if(value.any, isBanned);
  184. //add all required to allowed
  185. for(si32 item : value.all)
  186. {
  187. value.any.insert(item);
  188. }
  189. }
  190. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  191. {
  192. value = currentObject->operator[](fieldName).String();
  193. }
  194. void JsonDeserializer::serializeRaw(const std::string & fieldName, JsonNode & value, const boost::optional<const JsonNode &> defaultValue)
  195. {
  196. const JsonNode & data = currentObject->operator[](fieldName);
  197. if(data.getType() == JsonNode::JsonType::DATA_NULL)
  198. {
  199. if(defaultValue)
  200. value = defaultValue.get();
  201. else
  202. value.clear();
  203. }
  204. else
  205. {
  206. value = data;
  207. }
  208. }
  209. VCMI_LIB_NAMESPACE_END