JsonSerializer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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(std::string & value)
  64. {
  65. currentObject->String() = value;
  66. currentObject->setModScope(ModScope::scopeGame());
  67. }
  68. void JsonSerializer::serializeInternal(int64_t & value)
  69. {
  70. currentObject->Integer() = value;
  71. }
  72. void JsonSerializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<int32_t> & standard, std::set<int32_t> & value)
  73. {
  74. if(standard == value)
  75. return;
  76. writeLICPart(fieldName, "anyOf", encoder, value);
  77. }
  78. void JsonSerializer::serializeLIC(const std::string & fieldName, LICSet & value)
  79. {
  80. if(value.any != value.standard)
  81. writeLICPart(fieldName, "anyOf", value.encoder, value.any);
  82. writeLICPart(fieldName, "allOf", value.encoder, value.all);
  83. writeLICPart(fieldName, "noneOf", value.encoder, value.none);
  84. }
  85. void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
  86. {
  87. if(!value.empty())
  88. {
  89. currentObject->operator[](fieldName).String() = value;
  90. currentObject->operator[](fieldName).setModScope(ModScope::scopeGame());
  91. }
  92. }
  93. void JsonSerializer::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
  94. {
  95. if(!defaultValue || value != defaultValue.value())
  96. currentObject->operator[](fieldName) = value;
  97. }
  98. void JsonSerializer::pushStruct(const std::string & fieldName)
  99. {
  100. JsonTreeSerializer::pushStruct(fieldName);
  101. currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
  102. }
  103. void JsonSerializer::pushArray(const std::string & fieldName)
  104. {
  105. JsonTreeSerializer::pushArray(fieldName);
  106. currentObject->setType(JsonNode::JsonType::DATA_VECTOR);
  107. }
  108. void JsonSerializer::pushArrayElement(const size_t index)
  109. {
  110. JsonTreeSerializer::pushArrayElement(index);
  111. currentObject->setType(JsonNode::JsonType::DATA_STRUCT);
  112. }
  113. void JsonSerializer::resizeCurrent(const size_t newSize, JsonNode::JsonType type)
  114. {
  115. currentObject->Vector().resize(newSize);
  116. if(type != JsonNode::JsonType::DATA_NULL)
  117. {
  118. for(JsonNode & n : currentObject->Vector())
  119. if(n.getType() == JsonNode::JsonType::DATA_NULL)
  120. n.setType(type);
  121. }
  122. }
  123. void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::vector<bool> & data)
  124. {
  125. std::vector<std::string> buf;
  126. buf.reserve(data.size());
  127. for(si32 idx = 0; idx < data.size(); idx++)
  128. if(data[idx])
  129. buf.push_back(encoder(idx));
  130. writeLICPartBuffer(fieldName, partName, buf);
  131. }
  132. void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::set<si32> & data)
  133. {
  134. std::vector<std::string> buf;
  135. buf.reserve(data.size());
  136. for(const si32 item : data)
  137. buf.push_back(encoder(item));
  138. writeLICPartBuffer(fieldName, partName, buf);
  139. }
  140. void JsonSerializer::writeLICPartBuffer(const std::string & fieldName, const std::string & partName, std::vector<std::string> & buffer)
  141. {
  142. if(!buffer.empty())
  143. {
  144. std::sort(buffer.begin(), buffer.end());
  145. auto & target = currentObject->operator[](fieldName)[partName].Vector();
  146. for(auto & s : buffer)
  147. target.emplace_back(s);
  148. }
  149. }
  150. VCMI_LIB_NAMESPACE_END