JsonSerializeFormat.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * JsonSerializeFormat.h, 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. #pragma once
  11. class JsonNode;
  12. class JsonSerializeFormat;
  13. class JsonStructSerializer: public boost::noncopyable
  14. {
  15. public:
  16. JsonStructSerializer(JsonStructSerializer && other);
  17. virtual ~JsonStructSerializer();
  18. JsonStructSerializer enterStruct(const std::string & fieldName);
  19. JsonNode & get();
  20. JsonSerializeFormat * operator->();
  21. private:
  22. JsonStructSerializer(JsonSerializeFormat & owner_, const std::string & fieldName);
  23. JsonStructSerializer(JsonStructSerializer & parent, const std::string & fieldName);
  24. bool restoreState;
  25. JsonSerializeFormat & owner;
  26. JsonNode * parentNode;
  27. JsonNode * thisNode;
  28. friend class JsonSerializeFormat;
  29. };
  30. class JsonSerializeFormat: public boost::noncopyable
  31. {
  32. public:
  33. ///user-provided callback to resolve string identifier
  34. ///returns resolved identifier or -1 on error
  35. typedef std::function<si32(const std::string &)> TDecoder;
  36. ///user-provided callback to get string identifier
  37. ///may assume that object index is valid
  38. typedef std::function<std::string(si32)> TEncoder;
  39. struct LIC
  40. {
  41. LIC(const std::vector<bool> & Standard, const TDecoder & Decoder, const TEncoder & Encoder);
  42. const std::vector<bool> & standard;
  43. const TDecoder & decoder;
  44. const TEncoder & encoder;
  45. std::vector<bool> all, any, none;
  46. };
  47. const bool saving;
  48. JsonSerializeFormat() = delete;
  49. virtual ~JsonSerializeFormat() = default;
  50. JsonNode & getRoot()
  51. {
  52. return * root;
  53. };
  54. JsonNode & getCurrent()
  55. {
  56. return * current;
  57. };
  58. JsonStructSerializer enterStruct(const std::string & fieldName);
  59. template <typename T>
  60. void serializeBool(const std::string & fieldName, const T trueValue, const T falseValue, T & value)
  61. {
  62. bool temp = (value == trueValue);
  63. serializeBool(fieldName, temp);
  64. if(!saving)
  65. value = temp ? trueValue : falseValue;
  66. }
  67. virtual void serializeBool(const std::string & fieldName, bool & value) = 0;
  68. virtual void serializeEnum(const std::string & fieldName, const std::string & trueValue, const std::string & falseValue, bool & value) = 0;
  69. /** @brief Restrictive ("anyOf") simple serialization of Logical identifier condition, simple deserialization (allOf=anyOf)
  70. *
  71. * @param fieldName
  72. * @param decoder resolve callback, should report errors itself and do not throw
  73. * @param encoder encode callback, should report errors itself and do not throw
  74. * @param value target value, must be resized properly
  75. *
  76. */
  77. virtual void serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value) = 0;
  78. /** @brief Complete serialization of Logical identifier condition
  79. */
  80. virtual void serializeLIC(const std::string & fieldName, LIC & value) = 0;
  81. template <typename T>
  82. void serializeNumericEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const T defaultValue, T & value)
  83. {
  84. si32 temp = value;
  85. serializeIntEnum(fieldName, enumMap, defaultValue, temp);
  86. if(!saving)
  87. value = temp;
  88. };
  89. template <typename T>
  90. void serializeNumeric(const std::string & fieldName, T & value)
  91. {
  92. double temp = value;
  93. serializeFloat(fieldName, temp);
  94. if(!saving)
  95. value = temp;
  96. };
  97. virtual void serializeString(const std::string & fieldName, std::string & value) = 0;
  98. template <typename T>
  99. void serializeId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const T & defaultValue, T & value)
  100. {
  101. const si32 tempDefault = defaultValue.num;
  102. si32 tempValue = value.num;
  103. serializeIntId(fieldName, decoder, encoder, tempDefault, tempValue);
  104. if(!saving)
  105. value = T(tempValue);
  106. }
  107. protected:
  108. JsonNode * root;
  109. JsonNode * current;
  110. JsonSerializeFormat(JsonNode & root_, const bool saving_);
  111. virtual void serializeFloat(const std::string & fieldName, double & value) = 0;
  112. virtual void serializeIntEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const si32 defaultValue, si32 & value) = 0;
  113. virtual void serializeIntId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const si32 defaultValue, si32 & value) = 0;
  114. private:
  115. friend class JsonStructSerializer;
  116. };