JsonSerializeFormat.cpp 3.7 KB

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