JsonSerializeFormat.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. const bool saving;
  40. JsonSerializeFormat() = delete;
  41. virtual ~JsonSerializeFormat() = default;
  42. JsonNode & getRoot()
  43. {
  44. return * root;
  45. };
  46. JsonNode & getCurrent()
  47. {
  48. return * current;
  49. };
  50. JsonStructSerializer enterStruct(const std::string & fieldName);
  51. virtual void serializeBool(const std::string & fieldName, bool & value) = 0;
  52. virtual void serializeBoolEnum(const std::string & fieldName, const std::string & trueValue, const std::string & falseValue, bool & value) = 0;
  53. /** @brief Restrictive serialization of Logical identifier condition (only "anyOf" used), full deserialization
  54. *
  55. * @param fieldName
  56. * @param decoder resolve callback, should report errors itself and do not throw
  57. * @param encoder encode callback, should report errors itself and do not throw
  58. * @param value target value, must be resized properly
  59. *
  60. */
  61. virtual void serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value) = 0;
  62. template <typename T>
  63. void serializeNumericEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const T defaultValue, T & value)
  64. {
  65. si32 temp = value;
  66. serializeIntEnum(fieldName, enumMap, defaultValue, temp);
  67. if(!saving)
  68. value = temp;
  69. };
  70. template <typename T>
  71. void serializeNumeric(const std::string & fieldName, T & value)
  72. {
  73. double temp = value;
  74. serializeFloat(fieldName, temp);
  75. if(!saving)
  76. value = temp;
  77. };
  78. virtual void serializeString(const std::string & fieldName, std::string & value) = 0;
  79. template <typename T>
  80. void serializeId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const T & defaultValue, T & value)
  81. {
  82. const si32 tempDefault = defaultValue.num;
  83. si32 tempValue = value.num;
  84. serializeIntId(fieldName, decoder, encoder, tempDefault, tempValue);
  85. if(!saving)
  86. value = T(tempValue);
  87. }
  88. protected:
  89. JsonNode * root;
  90. JsonNode * current;
  91. JsonSerializeFormat(JsonNode & root_, const bool saving_);
  92. virtual void serializeFloat(const std::string & fieldName, double & value) = 0;
  93. virtual void serializeIntEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const si32 defaultValue, si32 & value) = 0;
  94. virtual void serializeIntId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const si32 defaultValue, si32 & value) = 0;
  95. private:
  96. friend class JsonStructSerializer;
  97. };