JsonSerializeFormat.h 16 KB

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