JsonSerializer.cpp 5.4 KB

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