JsonDeserializer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_, JsonNode & root_):
  14. JsonSerializeFormat(instanceResolver_, root_, false)
  15. {
  16. }
  17. void JsonDeserializer::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
  18. {
  19. const JsonNode & data = current->operator[](fieldName);
  20. if(data.getType() != JsonNode::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 = current->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 = current->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 = current->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 = current->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::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  76. {
  77. const JsonNode & field = current->operator[](fieldName);
  78. const JsonNode & anyOf = field["anyOf"];
  79. const JsonNode & allOf = field["allOf"];
  80. const JsonNode & noneOf = field["noneOf"];
  81. if(anyOf.Vector().empty() && allOf.Vector().empty())
  82. {
  83. //permissive mode
  84. value = standard;
  85. }
  86. else
  87. {
  88. //restrictive mode
  89. value.clear();
  90. value.resize(standard.size(), false);
  91. readLICPart(anyOf, decoder, true, value);
  92. readLICPart(allOf, decoder, true, value);
  93. }
  94. readLICPart(noneOf, decoder, false, value);
  95. }
  96. void JsonDeserializer::serializeLIC(const std::string & fieldName, LIC & value)
  97. {
  98. const JsonNode & field = current->operator[](fieldName);
  99. const JsonNode & anyOf = field["anyOf"];
  100. const JsonNode & allOf = field["allOf"];
  101. const JsonNode & noneOf = field["noneOf"];
  102. if(anyOf.Vector().empty())
  103. {
  104. //permissive mode
  105. value.any = value.standard;
  106. }
  107. else
  108. {
  109. //restrictive mode
  110. value.any.clear();
  111. value.any.resize(value.standard.size(), false);
  112. readLICPart(anyOf, value.decoder, true, value.any);
  113. }
  114. readLICPart(allOf, value.decoder, true, value.all);
  115. readLICPart(noneOf, value.decoder, true, value.none);
  116. //remove any banned from allowed and required
  117. for(si32 idx = 0; idx < value.none.size(); idx++)
  118. {
  119. if(value.none[idx])
  120. {
  121. value.all[idx] = false;
  122. value.any[idx] = false;
  123. }
  124. }
  125. //add all required to allowed
  126. for(si32 idx = 0; idx < value.all.size(); idx++)
  127. {
  128. if(value.all[idx])
  129. {
  130. value.any[idx] = true;
  131. }
  132. }
  133. }
  134. void JsonDeserializer::serializeLIC(const std::string & fieldName, LICSet & value)
  135. {
  136. const JsonNode & field = current->operator[](fieldName);
  137. const JsonNode & anyOf = field["anyOf"];
  138. const JsonNode & allOf = field["allOf"];
  139. const JsonNode & noneOf = field["noneOf"];
  140. value.all.clear();
  141. value.none.clear();
  142. if(anyOf.Vector().empty())
  143. {
  144. //permissive mode
  145. value.any = value.standard;
  146. }
  147. else
  148. {
  149. //restrictive mode
  150. value.any.clear();
  151. readLICPart(anyOf, value.decoder, value.any);
  152. for(si32 item : value.standard)
  153. if(!vstd::contains(value.any, item))
  154. value.none.insert(item);
  155. }
  156. readLICPart(allOf, value.decoder, value.all);
  157. readLICPart(noneOf, value.decoder, value.none);
  158. //remove any banned from allowed and required
  159. auto isBanned = [&value](const si32 item)->bool
  160. {
  161. return vstd::contains(value.none, item);
  162. };
  163. vstd::erase_if(value.all, isBanned);
  164. vstd::erase_if(value.any, isBanned);
  165. //add all required to allowed
  166. for(si32 item : value.all)
  167. {
  168. value.any.insert(item);
  169. }
  170. }
  171. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  172. {
  173. value = current->operator[](fieldName).String();
  174. }
  175. void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
  176. {
  177. for(size_t index = 0; index < part.Vector().size(); index++)
  178. {
  179. const std::string & identifier = part.Vector()[index].String();
  180. const si32 rawId = decoder(identifier);
  181. if(rawId >= 0)
  182. {
  183. if(rawId < value.size())
  184. value[rawId] = val;
  185. else
  186. logGlobal->error("JsonDeserializer::serializeLIC: id out of bounds %d", rawId);
  187. }
  188. }
  189. }
  190. void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value)
  191. {
  192. for(size_t index = 0; index < part.Vector().size(); index++)
  193. {
  194. const std::string & identifier = part.Vector()[index].String();
  195. const si32 rawId = decoder(identifier);
  196. if(rawId != -1)
  197. value.insert(rawId);
  198. }
  199. }