2
0

JsonDeserializer.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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)
  34. value = rawId;
  35. }
  36. }
  37. void JsonDeserializer::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
  38. {
  39. const JsonVector & data = currentObject->operator[](fieldName).Vector();
  40. value.clear();
  41. value.reserve(data.size());
  42. for(const JsonNode elem : data)
  43. {
  44. si32 rawId = decoder(elem.String());
  45. if(rawId >= 0)
  46. value.push_back(rawId);
  47. }
  48. }
  49. void JsonDeserializer::serializeInternal(const std::string & fieldName, double & value, const boost::optional<double> & defaultValue)
  50. {
  51. const JsonNode & data = currentObject->operator[](fieldName);
  52. if(!data.isNumber())
  53. value = defaultValue ? defaultValue.get() : 0;//todo: report error on not null?
  54. else
  55. value = data.Float();
  56. }
  57. void JsonDeserializer::serializeInternal(const std::string & fieldName, si64 & value, const boost::optional<si64> & defaultValue)
  58. {
  59. const JsonNode & data = currentObject->operator[](fieldName);
  60. if(!data.isNumber())
  61. value = defaultValue ? defaultValue.get() : 0;//todo: report error on not null?
  62. else
  63. value = data.Integer();
  64. }
  65. void JsonDeserializer::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
  66. {
  67. const std::string & valueName = currentObject->operator[](fieldName).String();
  68. const si32 actualOptional = defaultValue ? defaultValue.get() : 0;
  69. si32 rawValue = vstd::find_pos(enumMap, valueName);
  70. if(rawValue < 0)
  71. value = actualOptional;
  72. else
  73. value = rawValue;
  74. }
  75. void JsonDeserializer::serializeInternal(std::string & value)
  76. {
  77. value = currentObject->String();
  78. }
  79. void JsonDeserializer::serializeInternal(int64_t & value)
  80. {
  81. value = currentObject->Integer();
  82. }
  83. void JsonDeserializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  84. {
  85. const JsonNode & field = currentObject->operator[](fieldName);
  86. const JsonNode & anyOf = field["anyOf"];
  87. const JsonNode & allOf = field["allOf"];
  88. const JsonNode & noneOf = field["noneOf"];
  89. if(anyOf.Vector().empty() && allOf.Vector().empty())
  90. {
  91. //permissive mode
  92. value = standard;
  93. }
  94. else
  95. {
  96. //restrictive mode
  97. value.clear();
  98. value.resize(standard.size(), false);
  99. readLICPart(anyOf, decoder, true, value);
  100. readLICPart(allOf, decoder, true, value);
  101. }
  102. readLICPart(noneOf, decoder, false, value);
  103. }
  104. void JsonDeserializer::serializeLIC(const std::string & fieldName, LIC & value)
  105. {
  106. const JsonNode & field = currentObject->operator[](fieldName);
  107. const JsonNode & anyOf = field["anyOf"];
  108. const JsonNode & allOf = field["allOf"];
  109. const JsonNode & noneOf = field["noneOf"];
  110. if(anyOf.Vector().empty())
  111. {
  112. //permissive mode
  113. value.any = value.standard;
  114. }
  115. else
  116. {
  117. //restrictive mode
  118. value.any.clear();
  119. value.any.resize(value.standard.size(), false);
  120. readLICPart(anyOf, value.decoder, true, value.any);
  121. }
  122. readLICPart(allOf, value.decoder, true, value.all);
  123. readLICPart(noneOf, value.decoder, true, value.none);
  124. //remove any banned from allowed and required
  125. for(si32 idx = 0; idx < value.none.size(); idx++)
  126. {
  127. if(value.none[idx])
  128. {
  129. value.all[idx] = false;
  130. value.any[idx] = false;
  131. }
  132. }
  133. //add all required to allowed
  134. for(si32 idx = 0; idx < value.all.size(); idx++)
  135. {
  136. if(value.all[idx])
  137. {
  138. value.any[idx] = true;
  139. }
  140. }
  141. }
  142. void JsonDeserializer::serializeLIC(const std::string & fieldName, LICSet & value)
  143. {
  144. const JsonNode & field = currentObject->operator[](fieldName);
  145. const JsonNode & anyOf = field["anyOf"];
  146. const JsonNode & allOf = field["allOf"];
  147. const JsonNode & noneOf = field["noneOf"];
  148. value.all.clear();
  149. value.none.clear();
  150. if(anyOf.Vector().empty())
  151. {
  152. //permissive mode
  153. value.any = value.standard;
  154. }
  155. else
  156. {
  157. //restrictive mode
  158. value.any.clear();
  159. readLICPart(anyOf, value.decoder, value.any);
  160. for(si32 item : value.standard)
  161. if(!vstd::contains(value.any, item))
  162. value.none.insert(item);
  163. }
  164. readLICPart(allOf, value.decoder, value.all);
  165. readLICPart(noneOf, value.decoder, value.none);
  166. //remove any banned from allowed and required
  167. auto isBanned = [&value](const si32 item)->bool
  168. {
  169. return vstd::contains(value.none, item);
  170. };
  171. vstd::erase_if(value.all, isBanned);
  172. vstd::erase_if(value.any, isBanned);
  173. //add all required to allowed
  174. for(si32 item : value.all)
  175. {
  176. value.any.insert(item);
  177. }
  178. }
  179. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  180. {
  181. value = currentObject->operator[](fieldName).String();
  182. }
  183. void JsonDeserializer::serializeRaw(const std::string & fieldName, JsonNode & value, const boost::optional<const JsonNode &> defaultValue)
  184. {
  185. const JsonNode & data = currentObject->operator[](fieldName);
  186. if(data.getType() == JsonNode::JsonType::DATA_NULL)
  187. {
  188. if(defaultValue)
  189. value = defaultValue.get();
  190. else
  191. value.clear();
  192. }
  193. else
  194. {
  195. value = data;
  196. }
  197. }
  198. void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
  199. {
  200. for(size_t index = 0; index < part.Vector().size(); index++)
  201. {
  202. const std::string & identifier = part.Vector()[index].String();
  203. const si32 rawId = decoder(identifier);
  204. if(rawId >= 0)
  205. {
  206. if(rawId < value.size())
  207. value[rawId] = val;
  208. else
  209. logGlobal->error("JsonDeserializer::serializeLIC: id out of bounds %d", rawId);
  210. }
  211. }
  212. }
  213. void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value)
  214. {
  215. for(size_t index = 0; index < part.Vector().size(); index++)
  216. {
  217. const std::string & identifier = part.Vector()[index].String();
  218. const si32 rawId = decoder(identifier);
  219. if(rawId != -1)
  220. value.insert(rawId);
  221. }
  222. }