JsonSerializeFormat.cpp 3.6 KB

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