JsonSerializeFormat.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * JsonSerializeFormat.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 "JsonSerializeFormat.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. //JsonSerializeHelper
  14. JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper && other) noexcept: owner(other.owner), restoreState(false)
  15. {
  16. std::swap(restoreState, other.restoreState);
  17. }
  18. JsonSerializeHelper::~JsonSerializeHelper()
  19. {
  20. if(restoreState)
  21. owner->pop();
  22. }
  23. JsonSerializeFormat * JsonSerializeHelper::operator->()
  24. {
  25. return owner;
  26. }
  27. JsonSerializeHelper::JsonSerializeHelper(JsonSerializeFormat * owner_)
  28. : owner(owner_),
  29. restoreState(true)
  30. {
  31. }
  32. //JsonStructSerializer
  33. JsonStructSerializer::JsonStructSerializer(JsonStructSerializer && other) noexcept: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))) {}
  34. JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat * owner_)
  35. : JsonSerializeHelper(owner_)
  36. {
  37. }
  38. //JsonArraySerializer
  39. JsonArraySerializer::JsonArraySerializer(JsonArraySerializer && other) noexcept: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))) {}
  40. JsonArraySerializer::JsonArraySerializer(JsonSerializeFormat * owner_): JsonSerializeHelper(owner_), thisNode(&owner->getCurrent()) {}
  41. JsonStructSerializer JsonArraySerializer::enterStruct(const size_t index)
  42. {
  43. owner->pushArrayElement(index);
  44. return JsonStructSerializer(owner);
  45. }
  46. JsonArraySerializer JsonArraySerializer::enterArray(const size_t index)
  47. {
  48. owner->pushArrayElement(index);
  49. return JsonArraySerializer(owner);
  50. }
  51. void JsonArraySerializer::serializeString(const size_t index, std::string & value)
  52. {
  53. owner->pushArrayElement(index);
  54. owner->serializeInternal(value);
  55. owner->pop();
  56. }
  57. void JsonArraySerializer::serializeInt64(const size_t index, int64_t & value)
  58. {
  59. owner->pushArrayElement(index);
  60. owner->serializeInternal(value);
  61. owner->pop();
  62. }
  63. void JsonArraySerializer::resize(const size_t newSize)
  64. {
  65. resize(newSize, JsonNode::JsonType::DATA_NULL);
  66. }
  67. void JsonArraySerializer::resize(const size_t newSize, JsonNode::JsonType type)
  68. {
  69. owner->resizeCurrent(newSize, type);
  70. }
  71. size_t JsonArraySerializer::size() const
  72. {
  73. return thisNode->Vector().size();
  74. }
  75. JsonSerializeFormat::LICSet::LICSet(const std::set<si32> & Standard, TDecoder Decoder, TEncoder Encoder):
  76. standard(Standard),
  77. decoder(std::move(Decoder)),
  78. encoder(std::move(Encoder))
  79. {
  80. }
  81. //JsonSerializeFormat
  82. JsonSerializeFormat::JsonSerializeFormat(const IInstanceResolver * instanceResolver_, const bool saving_, const bool updating_)
  83. : saving(saving_),
  84. updating(updating_),
  85. instanceResolver(instanceResolver_)
  86. {
  87. }
  88. JsonStructSerializer JsonSerializeFormat::enterStruct(const std::string & fieldName)
  89. {
  90. pushStruct(fieldName);
  91. return JsonStructSerializer(this);
  92. }
  93. JsonArraySerializer JsonSerializeFormat::enterArray(const std::string & fieldName)
  94. {
  95. pushArray(fieldName);
  96. return JsonArraySerializer(this);
  97. }
  98. void JsonSerializeFormat::serializeBool(const std::string & fieldName, bool & value)
  99. {
  100. serializeBool<bool>(fieldName, value, true, false, false);
  101. }
  102. void JsonSerializeFormat::serializeBool(const std::string & fieldName, bool & value, const bool defaultValue)
  103. {
  104. serializeBool<bool>(fieldName, value, true, false, defaultValue);
  105. }
  106. void JsonSerializeFormat::readLICPart(const JsonNode & part, const JsonSerializeFormat::TDecoder & decoder, std::set<si32> & value) const
  107. {
  108. for(const auto & index : part.Vector())
  109. {
  110. const std::string & identifier = index.String();
  111. try
  112. {
  113. const si32 rawId = decoder(identifier);
  114. value.insert(rawId);
  115. }
  116. catch (const IdentifierResolutionException & e)
  117. {
  118. // downgrade exception to warning (printed as part of exception generation
  119. // this is usually caused by loading allowed heroes / artifacts list from vmap's
  120. }
  121. }
  122. }
  123. VCMI_LIB_NAMESPACE_END