2
0

JsonSerializeFormat.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. thisNode(other.thisNode),
  17. parentNode(other.parentNode),
  18. restoreState(false)
  19. {
  20. std::swap(restoreState, other.restoreState);
  21. }
  22. JsonSerializeHelper::~JsonSerializeHelper()
  23. {
  24. if(restoreState)
  25. owner.current = parentNode;
  26. }
  27. JsonNode & JsonSerializeHelper::get()
  28. {
  29. return * thisNode;
  30. }
  31. JsonSerializeFormat * JsonSerializeHelper::operator->()
  32. {
  33. return &owner;
  34. }
  35. JsonSerializeHelper::JsonSerializeHelper(JsonSerializeFormat & owner_, JsonNode * thisNode_):
  36. owner(owner_),
  37. thisNode(thisNode_),
  38. parentNode(owner.current),
  39. restoreState(true)
  40. {
  41. owner.current = thisNode;
  42. }
  43. JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper & parent, const std::string & fieldName):
  44. owner(parent.owner),
  45. thisNode(&(parent.thisNode->operator[](fieldName))),
  46. parentNode(parent.thisNode),
  47. restoreState(true)
  48. {
  49. owner.current = thisNode;
  50. }
  51. JsonStructSerializer JsonSerializeHelper::enterStruct(const std::string & fieldName)
  52. {
  53. return JsonStructSerializer(*this, fieldName);
  54. }
  55. JsonArraySerializer JsonSerializeHelper::enterArray(const std::string & fieldName)
  56. {
  57. return JsonArraySerializer(*this, fieldName);
  58. }
  59. //JsonStructSerializer
  60. JsonStructSerializer::JsonStructSerializer(JsonStructSerializer && other):
  61. JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))),
  62. optional(other.optional)
  63. {
  64. }
  65. JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat & owner_, const std::string & fieldName):
  66. JsonSerializeHelper(owner_, &(owner_.current->operator[](fieldName))),
  67. optional(false)
  68. {
  69. if(owner.saving)
  70. thisNode->setType(JsonNode::DATA_STRUCT);
  71. }
  72. JsonStructSerializer::JsonStructSerializer(JsonSerializeHelper & parent, const std::string & fieldName):
  73. JsonSerializeHelper(parent, fieldName),
  74. optional(false)
  75. {
  76. if(owner.saving)
  77. thisNode->setType(JsonNode::DATA_STRUCT);
  78. }
  79. JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat & owner_, JsonNode * thisNode_):
  80. JsonSerializeHelper(owner_, thisNode_),
  81. optional(false)
  82. {
  83. if(owner.saving)
  84. thisNode->setType(JsonNode::DATA_STRUCT);
  85. }
  86. JsonStructSerializer::~JsonStructSerializer()
  87. {
  88. //todo: delete empty optional node
  89. if(owner.saving && optional && thisNode->Struct().empty())
  90. {
  91. //
  92. }
  93. }
  94. //JsonArraySerializer
  95. JsonArraySerializer::JsonArraySerializer(JsonStructSerializer && other):
  96. JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other)))
  97. {
  98. }
  99. JsonArraySerializer::JsonArraySerializer(JsonSerializeFormat & owner_, const std::string & fieldName):
  100. JsonSerializeHelper(owner_, &(owner_.current->operator[](fieldName)))
  101. {
  102. }
  103. JsonArraySerializer::JsonArraySerializer(JsonSerializeHelper & parent, const std::string & fieldName):
  104. JsonSerializeHelper(parent, fieldName)
  105. {
  106. }
  107. JsonStructSerializer JsonArraySerializer::enterStruct(const size_t index)
  108. {
  109. return JsonStructSerializer(owner, &(thisNode->Vector()[index]));
  110. }
  111. void JsonArraySerializer::resize(const size_t newSize)
  112. {
  113. thisNode->Vector().resize(newSize);
  114. }
  115. void JsonArraySerializer::resize(const size_t newSize, JsonNode::JsonType type)
  116. {
  117. resize(newSize);
  118. for(JsonNode & n : thisNode->Vector())
  119. if(n.getType() == JsonNode::DATA_NULL)
  120. n.setType(type);
  121. }
  122. size_t JsonArraySerializer::size() const
  123. {
  124. return thisNode->Vector().size();
  125. }
  126. //JsonSerializeFormat::LIC
  127. JsonSerializeFormat::LIC::LIC(const std::vector<bool> & Standard, const TDecoder Decoder, const TEncoder Encoder):
  128. standard(Standard), decoder(Decoder), encoder(Encoder)
  129. {
  130. any.resize(standard.size(), false);
  131. all.resize(standard.size(), false);
  132. none.resize(standard.size(), false);
  133. }
  134. JsonSerializeFormat::LICSet::LICSet(const std::set<si32>& Standard, const TDecoder Decoder, const TEncoder Encoder):
  135. standard(Standard), decoder(Decoder), encoder(Encoder)
  136. {
  137. }
  138. //JsonSerializeFormat
  139. JsonSerializeFormat::JsonSerializeFormat(const IInstanceResolver * instanceResolver_, JsonNode & root_, const bool saving_):
  140. saving(saving_),
  141. root(&root_),
  142. current(root),
  143. instanceResolver(instanceResolver_)
  144. {
  145. }
  146. JsonStructSerializer JsonSerializeFormat::enterStruct(const std::string & fieldName)
  147. {
  148. return JsonStructSerializer(*this, fieldName);
  149. }
  150. JsonArraySerializer JsonSerializeFormat::enterArray(const std::string & fieldName)
  151. {
  152. return JsonArraySerializer(*this, fieldName);
  153. }
  154. void JsonSerializeFormat::serializeBool(const std::string & fieldName, bool & value)
  155. {
  156. serializeBool<bool>(fieldName, value, true, false, false);
  157. }