JsonDeserializer.cpp 5.7 KB

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