JsonSerializer.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * JsonSerializer.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 "JsonSerializer.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. JsonSerializer::JsonSerializer(const IInstanceResolver * instanceResolver_, JsonNode & root_):
  14. JsonTreeSerializer(instanceResolver_, &root_, true, false)
  15. {
  16. }
  17. void JsonSerializer::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
  18. {
  19. if(!boost::logic::indeterminate(value))
  20. currentObject->operator[](fieldName).Bool() = (bool)value;
  21. }
  22. void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
  23. {
  24. if(!defaultValue || defaultValue.value() != value)
  25. {
  26. std::string identifier = encoder(value);
  27. serializeString(fieldName, identifier);
  28. }
  29. }
  30. void JsonSerializer::serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue)
  31. {
  32. if(!defaultValue || !vstd::isAlmostEqual(defaultValue.value(), value))
  33. currentObject->operator[](fieldName).Float() = value;
  34. }
  35. void JsonSerializer::serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue)
  36. {
  37. if(!defaultValue || defaultValue.value() != value)
  38. currentObject->operator[](fieldName).Integer() = value;
  39. }
  40. void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
  41. {
  42. if(!defaultValue || defaultValue.value() != value)
  43. currentObject->operator[](fieldName).String() = enumMap.at(value);
  44. }
  45. void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
  46. {
  47. if(value.empty())
  48. return;
  49. JsonVector & data = currentObject->operator[](fieldName).Vector();
  50. data.reserve(value.size());
  51. for(const si32 rawId : value)
  52. data.emplace_back(rawId);
  53. }
  54. void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector<std::string> & value)
  55. {
  56. if(value.empty())
  57. return;
  58. JsonVector & data = currentObject->operator[](fieldName).Vector();
  59. data.reserve(value.size());
  60. for(const auto & rawId : value)
  61. data.emplace_back(rawId);
  62. }
  63. void JsonSerializer::serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value)
  64. {
  65. if(value.empty())
  66. return;
  67. JsonMap & data = currentObject->operator[](fieldName).Struct();
  68. for(const auto & [rawId, val] : value)
  69. data[rawId].Integer() = val;
  70. }
  71. void JsonSerializer::serializeInternal(std::string & value)
  72. {
  73. currentObject->String() = value;
  74. currentObject->setModScope(ModScope::scopeGame());
  75. }
  76. void JsonSerializer::serializeInternal(int64_t & value)
  77. {
  78. currentObject->Integer() = value;
  79. }
  80. void JsonSerializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<int32_t> & standard, std::set<int32_t> & value)
  81. {
  82. if(standard == value)
  83. return;
  84. writeLICPart(fieldName, "anyOf", encoder, value);
  85. }
  86. void JsonSerializer::serializeLIC(const std::string & fieldName, LICSet & value)
  87. {
  88. if(value.any != value.standard)
  89. writeLICPart(fieldName, "anyOf", value.encoder, value.any);
  90. writeLICPart(fieldName, "allOf", value.encoder, value.all);
  91. writeLICPart(fieldName, "noneOf", value.encoder, value.none);
  92. }
  93. void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
  94. {
  95. if(!value.empty())
  96. {
  97. currentObject->operator[](fieldName).String() = value;
  98. currentObject->operator[](fieldName).setModScope(ModScope::scopeGame());
  99. }
  100. }
  101. void JsonSerializer::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
  102. {
  103. if(!defaultValue || value != defaultValue.value())
  104. currentObject->operator[](fieldName) = value;
  105. }
  106. void JsonSerializer::pushStruct(const std::string & fieldName)
  107. {
  108. JsonTreeSerializer::pushStruct(fieldName);
  109. currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
  110. }
  111. void JsonSerializer::pushArray(const std::string & fieldName)
  112. {
  113. JsonTreeSerializer::pushArray(fieldName);
  114. currentObject->setType(JsonNode::JsonType::DATA_VECTOR);
  115. }
  116. void JsonSerializer::pushArrayElement(const size_t index)
  117. {
  118. JsonTreeSerializer::pushArrayElement(index);
  119. currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
  120. }
  121. void JsonSerializer::resizeCurrent(const size_t newSize, JsonNode::JsonType type)
  122. {
  123. currentObject->Vector().resize(newSize);
  124. if(type != JsonNode::JsonType::DATA_NULL)
  125. {
  126. for(JsonNode & n : currentObject->Vector())
  127. if(n.getType() == JsonNode::JsonType::DATA_NULL)
  128. n.setType(type);
  129. }
  130. }
  131. void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::vector<bool> & data)
  132. {
  133. std::vector<std::string> buf;
  134. buf.reserve(data.size());
  135. for(si32 idx = 0; idx < data.size(); idx++)
  136. if(data[idx])
  137. buf.push_back(encoder(idx));
  138. writeLICPartBuffer(fieldName, partName, buf);
  139. }
  140. void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::set<si32> & data)
  141. {
  142. std::vector<std::string> buf;
  143. buf.reserve(data.size());
  144. for(const si32 item : data)
  145. buf.push_back(encoder(item));
  146. writeLICPartBuffer(fieldName, partName, buf);
  147. }
  148. void JsonSerializer::writeLICPartBuffer(const std::string & fieldName, const std::string & partName, std::vector<std::string> & buffer)
  149. {
  150. if(!buffer.empty())
  151. {
  152. std::sort(buffer.begin(), buffer.end());
  153. auto & target = currentObject->operator[](fieldName)[partName].Vector();
  154. for(auto & s : buffer)
  155. target.emplace_back(s);
  156. }
  157. }
  158. VCMI_LIB_NAMESPACE_END