JsonDeserializer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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::set<int32_t> & standard, std::set<int32_t> & value)
  106. {
  107. LICSet lic(standard, decoder, encoder);
  108. serializeLIC(fieldName, lic);
  109. value = lic.any;
  110. }
  111. void JsonDeserializer::serializeLIC(const std::string & fieldName, LICSet & value)
  112. {
  113. const JsonNode & field = currentObject->operator[](fieldName);
  114. const JsonNode & anyOf = field["anyOf"];
  115. const JsonNode & allOf = field["allOf"];
  116. const JsonNode & noneOf = field["noneOf"];
  117. value.all.clear();
  118. value.none.clear();
  119. if(anyOf.Vector().empty())
  120. {
  121. //permissive mode
  122. value.any = value.standard;
  123. }
  124. else
  125. {
  126. //restrictive mode
  127. value.any.clear();
  128. readLICPart(anyOf, value.decoder, value.any);
  129. for(si32 item : value.standard)
  130. if(!vstd::contains(value.any, item))
  131. value.none.insert(item);
  132. }
  133. readLICPart(allOf, value.decoder, value.all);
  134. readLICPart(noneOf, value.decoder, value.none);
  135. //remove any banned from allowed and required
  136. auto isBanned = [&value](const si32 item)->bool
  137. {
  138. return vstd::contains(value.none, item);
  139. };
  140. vstd::erase_if(value.all, isBanned);
  141. vstd::erase_if(value.any, isBanned);
  142. //add all required to allowed
  143. for(si32 item : value.all)
  144. {
  145. value.any.insert(item);
  146. }
  147. }
  148. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  149. {
  150. value = currentObject->operator[](fieldName).String();
  151. }
  152. void JsonDeserializer::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
  153. {
  154. const JsonNode & data = currentObject->operator[](fieldName);
  155. if(data.getType() == JsonNode::JsonType::DATA_NULL)
  156. {
  157. if(defaultValue)
  158. value = defaultValue.value();
  159. else
  160. value.clear();
  161. }
  162. else
  163. {
  164. value = data;
  165. }
  166. }
  167. VCMI_LIB_NAMESPACE_END