JsonSerializeFormat.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. //JsonStructSerializer
  14. JsonStructSerializer::JsonStructSerializer(JsonStructSerializer&& other):
  15. restoreState(false),
  16. owner(other.owner),
  17. parentNode(other.parentNode),
  18. thisNode(other.thisNode)
  19. {
  20. }
  21. JsonStructSerializer::~JsonStructSerializer()
  22. {
  23. if(restoreState)
  24. owner.current = parentNode;
  25. }
  26. JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat& owner_, const std::string& fieldName):
  27. restoreState(true),
  28. owner(owner_),
  29. parentNode(owner.current),
  30. thisNode(&(parentNode->operator[](fieldName)))
  31. {
  32. owner.current = thisNode;
  33. }
  34. JsonStructSerializer::JsonStructSerializer(JsonStructSerializer & parent, const std::string & fieldName):
  35. restoreState(true),
  36. owner(parent.owner),
  37. parentNode(parent.thisNode),
  38. thisNode(&(parentNode->operator[](fieldName)))
  39. {
  40. owner.current = thisNode;
  41. }
  42. JsonStructSerializer JsonStructSerializer::enterStruct(const std::string & fieldName)
  43. {
  44. return JsonStructSerializer(*this, fieldName);
  45. }
  46. JsonNode& JsonStructSerializer::get()
  47. {
  48. return *thisNode;
  49. }
  50. JsonSerializeFormat * JsonStructSerializer::operator->()
  51. {
  52. return &owner;
  53. }
  54. //JsonSerializeFormat
  55. JsonSerializeFormat::JsonSerializeFormat(JsonNode & root_, const bool saving_):
  56. saving(saving_),
  57. root(&root_),
  58. current(root)
  59. {
  60. }
  61. JsonStructSerializer JsonSerializeFormat::enterStruct(const std::string & fieldName)
  62. {
  63. JsonStructSerializer res(*this, fieldName);
  64. return res;
  65. }