JsonSerializeFormat.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. #include "../JsonNode.h"
  12. #include "../modding/IdentifierStorage.h"
  13. #include "../modding/ModScope.h"
  14. #include "../VCMI_Lib.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class JsonSerializeFormat;
  17. class JsonStructSerializer;
  18. class JsonArraySerializer;
  19. class DLL_LINKAGE IInstanceResolver
  20. {
  21. public:
  22. virtual ~IInstanceResolver(){};
  23. virtual si32 decode(const std::string & identifier) const = 0;
  24. virtual std::string encode(si32 identifier) const = 0;
  25. };
  26. class DLL_LINKAGE JsonSerializeHelper: public boost::noncopyable
  27. {
  28. public:
  29. JsonSerializeHelper(JsonSerializeHelper && other) noexcept;
  30. virtual ~JsonSerializeHelper();
  31. JsonSerializeFormat * operator->();
  32. protected:
  33. JsonSerializeHelper(JsonSerializeFormat * owner_);
  34. JsonSerializeFormat * owner;
  35. private:
  36. bool restoreState;
  37. };
  38. class DLL_LINKAGE JsonStructSerializer: public JsonSerializeHelper
  39. {
  40. public:
  41. JsonStructSerializer(JsonStructSerializer && other) noexcept;
  42. protected:
  43. JsonStructSerializer(JsonSerializeFormat * owner_);
  44. friend class JsonSerializeFormat;
  45. friend class JsonArraySerializer;
  46. };
  47. class DLL_LINKAGE JsonArraySerializer: public JsonSerializeHelper
  48. {
  49. public:
  50. JsonArraySerializer(JsonArraySerializer && other) noexcept;
  51. JsonStructSerializer enterStruct(const size_t index);
  52. JsonArraySerializer enterArray(const size_t index);
  53. template <typename Container>
  54. void syncSize(Container & c, JsonNode::JsonType type = JsonNode::JsonType::DATA_NULL);
  55. ///Anything int64-convertible <-> Json integer
  56. template <typename T>
  57. void serializeInt(const size_t index, T & value);
  58. ///String <-> Json string
  59. void serializeString(const size_t index, std::string & value);
  60. ///vector of serializable <-> Json vector of structs
  61. template <typename Element>
  62. void serializeStruct(std::vector<Element> & value)
  63. {
  64. syncSize(value, JsonNode::JsonType::DATA_STRUCT);
  65. for(size_t idx = 0; idx < size(); idx++)
  66. {
  67. auto s = enterStruct(idx);
  68. value[idx].serializeJson(*owner);
  69. }
  70. }
  71. void resize(const size_t newSize);
  72. void resize(const size_t newSize, JsonNode::JsonType type);
  73. size_t size() const;
  74. protected:
  75. JsonArraySerializer(JsonSerializeFormat * owner_);
  76. friend class JsonSerializeFormat;
  77. private:
  78. const JsonNode * thisNode;
  79. void serializeInt64(const size_t index, int64_t & value);
  80. };
  81. class DLL_LINKAGE JsonSerializeFormat: public boost::noncopyable
  82. {
  83. public:
  84. ///user-provided callback to resolve string identifier
  85. ///returns resolved identifier or -1 on error
  86. using TDecoder = std::function<si32(const std::string &)>;
  87. ///user-provided callback to get string identifier
  88. ///may assume that object index is valid
  89. using TEncoder = std::function<std::string(si32)>;
  90. using TSerialize = std::function<void(JsonSerializeFormat &)>;
  91. struct LIC
  92. {
  93. LIC(const std::vector<bool> & Standard, TDecoder Decoder, TEncoder Encoder);
  94. const std::vector<bool> & standard;
  95. const TDecoder decoder;
  96. const TEncoder encoder;
  97. std::vector<bool> all, any, none;
  98. };
  99. struct LICSet
  100. {
  101. LICSet(const std::set<si32> & Standard, TDecoder Decoder, TEncoder Encoder);
  102. const std::set<si32> & standard;
  103. const TDecoder decoder;
  104. const TEncoder encoder;
  105. std::set<si32> all, any, none;
  106. };
  107. const bool saving;
  108. const bool updating;
  109. JsonSerializeFormat() = delete;
  110. virtual ~JsonSerializeFormat() = default;
  111. virtual const JsonNode & getCurrent() = 0;
  112. JsonStructSerializer enterStruct(const std::string & fieldName);
  113. JsonArraySerializer enterArray(const std::string & fieldName);
  114. ///Anything comparable <-> Json bool
  115. template <typename T>
  116. void serializeBool(const std::string & fieldName, T & value, const T trueValue, const T falseValue, const T defaultValue)
  117. {
  118. boost::logic::tribool temp(boost::logic::indeterminate);
  119. if(value == defaultValue)
  120. ;//leave as indeterminate
  121. else if(value == trueValue)
  122. temp = true;
  123. else if(value == falseValue)
  124. temp = false;
  125. serializeInternal(fieldName, temp);
  126. if(!saving)
  127. {
  128. if(boost::logic::indeterminate(temp))
  129. value = defaultValue;
  130. else
  131. value = temp ? trueValue : falseValue;
  132. }
  133. }
  134. ///bool <-> Json bool
  135. void serializeBool(const std::string & fieldName, bool & value);
  136. void serializeBool(const std::string & fieldName, bool & value, const bool defaultValue);
  137. ///tribool <-> Json bool
  138. void serializeBool(const std::string & fieldName, boost::logic::tribool & value)
  139. {
  140. serializeInternal(fieldName, value);
  141. };
  142. /** @brief Restrictive ("anyOf") simple serialization of Logical identifier condition, simple deserialization (allOf=anyOf)
  143. *
  144. * @param fieldName
  145. * @param decoder resolve callback, should report errors itself and do not throw
  146. * @param encoder encode callback, should report errors itself and do not throw
  147. * @param value target value, must be resized properly
  148. *
  149. */
  150. virtual void serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value) = 0;
  151. /** @brief Complete serialization of Logical identifier condition
  152. */
  153. virtual void serializeLIC(const std::string & fieldName, LIC & value) = 0;
  154. /** @brief Complete serialization of Logical identifier condition. (Special version)
  155. * Assumes that all values are allowed by default, and standard contains them
  156. */
  157. virtual void serializeLIC(const std::string & fieldName, LICSet & value) = 0;
  158. ///String <-> Json string
  159. virtual void serializeString(const std::string & fieldName, std::string & value) = 0;
  160. ///si32-convertible enum <-> Json string enum
  161. template <typename T>
  162. void serializeEnum(const std::string & fieldName, T & value, const std::vector<std::string> & enumMap)
  163. {
  164. doSerializeInternal<T, T, si32>(fieldName, value, std::nullopt, enumMap);
  165. };
  166. ///si32-convertible enum <-> Json string enum
  167. template <typename T, typename U>
  168. void serializeEnum(const std::string & fieldName, T & value, const U & defaultValue, const std::vector<std::string> & enumMap)
  169. {
  170. doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, enumMap);
  171. };
  172. template <typename T, typename U, typename C>
  173. void serializeEnum(const std::string & fieldName, T & value, const U & defaultValue, const C & enumMap)
  174. {
  175. std::vector<std::string> enumMapCopy;
  176. std::copy(std::begin(enumMap), std::end(enumMap), std::back_inserter(enumMapCopy));
  177. doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, enumMapCopy);
  178. };
  179. ///Anything double-convertible <-> Json double
  180. template <typename T>
  181. void serializeFloat(const std::string & fieldName, T & value)
  182. {
  183. doSerializeInternal<T, T, double>(fieldName, value, std::nullopt);
  184. };
  185. ///Anything double-convertible <-> Json double
  186. template <typename T, typename U>
  187. void serializeFloat(const std::string & fieldName, T & value, const U & defaultValue)
  188. {
  189. doSerializeInternal<T, U, double>(fieldName, value, defaultValue);
  190. };
  191. ///Anything int64-convertible <-> Json integer
  192. ///no default value
  193. template <typename T>
  194. void serializeInt(const std::string & fieldName, T & value)
  195. {
  196. doSerializeInternal<T, T, si64>(fieldName, value, std::nullopt);
  197. };
  198. ///Anything int64-convertible <-> Json integer
  199. ///custom default value
  200. template <typename T, typename U>
  201. void serializeInt(const std::string & fieldName, T & value, const U & defaultValue)
  202. {
  203. doSerializeInternal<T, U, si64>(fieldName, value, defaultValue);
  204. };
  205. ///Anything int64-convertible <-> Json integer
  206. ///default value is std::nullopt
  207. template<typename T>
  208. void serializeInt(const std::string & fieldName, std::optional<T> & value)
  209. {
  210. dispatchOptional<T, si64>(fieldName, value);
  211. };
  212. ///si32-convertible identifier <-> Json string
  213. template <typename T, typename U>
  214. void serializeId(const std::string & fieldName, T & value, const U & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
  215. {
  216. doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, decoder, encoder);
  217. }
  218. ///si32-convertible identifier <-> Json string
  219. template <typename T, typename U, typename E = T>
  220. void serializeId(const std::string & fieldName, T & value, const U & defaultValue)
  221. {
  222. if (saving)
  223. {
  224. if (value != defaultValue)
  225. {
  226. std::string fieldValue = E::encode(value);
  227. serializeString(fieldName, fieldValue);
  228. }
  229. }
  230. else
  231. {
  232. std::string fieldValue;
  233. serializeString(fieldName, fieldValue);
  234. if (!fieldValue.empty())
  235. {
  236. VLC->identifiers()->requestIdentifier(ModScope::scopeGame(), E::entityType(), fieldValue, [&value](int32_t index){
  237. value = T(index);
  238. });
  239. }
  240. else
  241. {
  242. value = T(defaultValue);
  243. }
  244. }
  245. }
  246. ///si32-convertible identifier vector <-> Json array of string
  247. template <typename T, typename E = T>
  248. void serializeIdArray(const std::string & fieldName, std::vector<T> & value)
  249. {
  250. if (saving)
  251. {
  252. std::vector<std::string> fieldValue;
  253. for(const T & vitem : value)
  254. fieldValue.push_back(E::encode(vitem));
  255. serializeInternal(fieldName, fieldValue);
  256. }
  257. else
  258. {
  259. std::vector<std::string> fieldValue;
  260. serializeInternal(fieldName, fieldValue);
  261. value.resize(fieldValue.size());
  262. for(size_t i = 0; i < fieldValue.size(); ++i)
  263. {
  264. VLC->identifiers()->requestIdentifier(ModScope::scopeGame(), E::entityType(), fieldValue[i], [&value, i](int32_t index){
  265. value[i] = T(index);
  266. });
  267. }
  268. }
  269. }
  270. ///si32-convertible identifier set <-> Json array of string
  271. template <typename T, typename U = T>
  272. void serializeIdArray(const std::string & fieldName, std::set<T> & value)
  273. {
  274. if (saving)
  275. {
  276. std::vector<std::string> fieldValue;
  277. for(const T & vitem : value)
  278. fieldValue.push_back(U::encode(vitem));
  279. serializeInternal(fieldName, fieldValue);
  280. }
  281. else
  282. {
  283. std::vector<std::string> fieldValue;
  284. serializeInternal(fieldName, fieldValue);
  285. for(size_t i = 0; i < fieldValue.size(); ++i)
  286. {
  287. VLC->identifiers()->requestIdentifier(ModScope::scopeGame(), U::entityType(), fieldValue[i], [&value](int32_t index){
  288. value.insert(T(index));
  289. });
  290. }
  291. }
  292. }
  293. ///si32-convertible instance identifier <-> Json string
  294. template <typename T>
  295. void serializeInstance(const std::string & fieldName, T & value, const T & defaultValue)
  296. {
  297. const TDecoder decoder = std::bind(&IInstanceResolver::decode, instanceResolver, _1);
  298. const TEncoder encoder = std::bind(&IInstanceResolver::encode, instanceResolver, _1);
  299. serializeId<T>(fieldName, value, defaultValue, decoder, encoder);
  300. }
  301. ///any serializable object <-> Json struct
  302. template <typename T>
  303. void serializeStruct(const std::string & fieldName, T & value)
  304. {
  305. auto guard = enterStruct(fieldName);
  306. value.serializeJson(*this);
  307. }
  308. virtual void serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue) = 0;
  309. protected:
  310. JsonSerializeFormat(const IInstanceResolver * instanceResolver_, const bool saving_, const bool updating_);
  311. ///bool <-> Json bool, indeterminate is default
  312. virtual void serializeInternal(const std::string & fieldName, boost::logic::tribool & value) = 0;
  313. ///Numeric Id <-> String Id
  314. virtual void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder) = 0;
  315. ///Numeric Id vector <-> String Id vector
  316. virtual void serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder) = 0;
  317. ///Numeric <-> Json double
  318. virtual void serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue) = 0;
  319. ///Numeric <-> Json integer
  320. virtual void serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue) = 0;
  321. ///Enum/Numeric <-> Json string enum
  322. virtual void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap) = 0;
  323. ///String vector <-> Json string vector
  324. virtual void serializeInternal(const std::string & fieldName, std::vector<std::string> & value) = 0;
  325. virtual void pop() = 0;
  326. virtual void pushStruct(const std::string & fieldName) = 0;
  327. virtual void pushArray(const std::string & fieldName) = 0;
  328. virtual void pushArrayElement(const size_t index) = 0;
  329. virtual void pushField(const std::string & fieldName) = 0;
  330. virtual void resizeCurrent(const size_t newSize, JsonNode::JsonType type){};
  331. virtual void serializeInternal(std::string & value) = 0;
  332. virtual void serializeInternal(int64_t & value) = 0;
  333. private:
  334. const IInstanceResolver * instanceResolver;
  335. template<typename VType, typename DVType, typename IType, typename... Args>
  336. void doSerializeInternal(const std::string & fieldName, VType & value, const std::optional<DVType> & defaultValue, Args... args)
  337. {
  338. const std::optional<IType> tempDefault = defaultValue ? std::optional<IType>(static_cast<IType>(defaultValue.value())) : std::nullopt;
  339. auto temp = static_cast<IType>(value);
  340. serializeInternal(fieldName, temp, tempDefault, args...);
  341. if(!saving)
  342. value = static_cast<VType>(temp);
  343. }
  344. template<typename VType, typename IType, typename... Args>
  345. void dispatchOptional(const std::string & fieldName, std::optional<VType> & value, Args... args)
  346. {
  347. if(saving)
  348. {
  349. if(value)
  350. {
  351. auto temp = static_cast<IType>(value.value());
  352. pushField(fieldName);
  353. serializeInternal(temp, args...);
  354. pop();
  355. }
  356. }
  357. else
  358. {
  359. pushField(fieldName);
  360. if(getCurrent().getType() == JsonNode::JsonType::DATA_NULL)
  361. {
  362. value = std::nullopt;
  363. }
  364. else
  365. {
  366. IType temp = IType();
  367. serializeInternal(temp, args...);
  368. value = std::make_optional(temp);
  369. }
  370. pop();
  371. }
  372. }
  373. friend class JsonSerializeHelper;
  374. friend class JsonStructSerializer;
  375. friend class JsonArraySerializer;
  376. };
  377. template <typename Container>
  378. void JsonArraySerializer::syncSize(Container & c, JsonNode::JsonType type)
  379. {
  380. if(owner->saving)
  381. resize(c.size(), type);
  382. else
  383. c.resize(size());
  384. }
  385. template <typename T>
  386. void JsonArraySerializer::serializeInt(const size_t index, T & value)
  387. {
  388. auto temp = static_cast<int64_t>(value);
  389. serializeInt64(index, temp);
  390. if (!owner->saving)
  391. value = static_cast<T>(temp);
  392. };
  393. VCMI_LIB_NAMESPACE_END