JsonSerializeFormat.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. const JsonNode & fieldValue = getCurrent()[fieldName];
  260. if (!fieldValue.String().empty())
  261. {
  262. LIBRARY->identifiers()->requestIdentifier(IdentifierType::entityType(), fieldValue, [&value](int32_t index){
  263. value = IdentifierType(index);
  264. });
  265. }
  266. else
  267. {
  268. value = IdentifierType(defaultValue);
  269. }
  270. }
  271. }
  272. /// si32-convertible identifier map <-> Json object of {key: string}
  273. template <typename Key, typename T, typename E = T>
  274. void serializeIdMap(const std::string & fieldName, std::map<Key, T> & value)
  275. {
  276. if (saving)
  277. {
  278. std::map<std::string, T> fieldValue;
  279. for (const auto & [key, val] : value)
  280. fieldValue[Key::encode(key.getNum())] = val;
  281. serializeInternal(fieldName, fieldValue);
  282. }
  283. else
  284. {
  285. const auto & node = getCurrent()[fieldName].Struct();
  286. for (const auto & n : node)
  287. {
  288. LIBRARY->identifiers()->requestIdentifier(n.second.getModScope(), Key::entityType(), n.first, [&value, n](int32_t index) {
  289. value[Key(index)] = n.second.Integer(); // TODO: only int supported yet
  290. });
  291. }
  292. }
  293. }
  294. ///si32-convertible identifier vector <-> Json array of string
  295. template <typename T, typename E = T>
  296. void serializeIdArray(const std::string & fieldName, std::vector<T> & value)
  297. {
  298. if (saving)
  299. {
  300. std::vector<std::string> fieldValue;
  301. for(const T & vitem : value)
  302. fieldValue.push_back(E::encode(vitem.getNum()));
  303. serializeInternal(fieldName, fieldValue);
  304. }
  305. else
  306. {
  307. const JsonVector & fieldValue = getCurrent()[fieldName].Vector();
  308. value.resize(fieldValue.size());
  309. for(size_t i = 0; i < fieldValue.size(); ++i)
  310. {
  311. LIBRARY->identifiers()->requestIdentifier(E::entityType(), fieldValue[i], [&value, i](int32_t index){
  312. value[i] = T(index);
  313. });
  314. }
  315. }
  316. }
  317. ///si32-convertible identifier set <-> Json array of string
  318. template <typename T, typename U = T>
  319. void serializeIdArray(const std::string & fieldName, std::set<T> & value)
  320. {
  321. if (saving)
  322. {
  323. std::vector<std::string> fieldValue;
  324. for(const T & vitem : value)
  325. fieldValue.push_back(U::encode(vitem.getNum()));
  326. serializeInternal(fieldName, fieldValue);
  327. }
  328. else
  329. {
  330. for (const auto & element : getCurrent()[fieldName].Vector())
  331. {
  332. LIBRARY->identifiers()->requestIdentifierIfFound(U::entityType(), element, [&value](int32_t index){
  333. value.insert(T(index));
  334. });
  335. }
  336. }
  337. }
  338. ///si32-convertible instance identifier <-> Json string
  339. template <typename T>
  340. void serializeInstance(const std::string & fieldName, T & value, const T & defaultValue)
  341. {
  342. const TDecoder decoder = std::bind(&IInstanceResolver::decode, instanceResolver, _1);
  343. const TEncoder encoder = std::bind(&IInstanceResolver::encode, instanceResolver, _1);
  344. if (saving)
  345. {
  346. if (value != defaultValue)
  347. {
  348. std::string fieldValue = encoder(value.getNum());
  349. serializeString(fieldName, fieldValue);
  350. }
  351. }
  352. else
  353. {
  354. std::string fieldValue;
  355. serializeString(fieldName, fieldValue);
  356. if (!fieldValue.empty())
  357. value = T(decoder(fieldValue));
  358. else
  359. value = T(defaultValue);
  360. }
  361. }
  362. ///any serializable object <-> Json struct
  363. template <typename T>
  364. void serializeStruct(const std::string & fieldName, T & value)
  365. {
  366. auto guard = enterStruct(fieldName);
  367. value.serializeJson(*this);
  368. }
  369. virtual void serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue) = 0;
  370. protected:
  371. JsonSerializeFormat(const IInstanceResolver * instanceResolver_, const bool saving_, const bool updating_);
  372. ///bool <-> Json bool, indeterminate is default
  373. virtual void serializeInternal(const std::string & fieldName, boost::logic::tribool & value) = 0;
  374. ///Numeric Id <-> String Id
  375. virtual void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder) = 0;
  376. ///Numeric Id vector <-> String Id vector
  377. virtual void serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder) = 0;
  378. ///Numeric <-> Json double
  379. virtual void serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue) = 0;
  380. ///Numeric <-> Json integer
  381. virtual void serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue) = 0;
  382. ///Enum/Numeric <-> Json string enum
  383. virtual void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap) = 0;
  384. ///String vector <-> Json string vector
  385. virtual void serializeInternal(const std::string & fieldName, std::vector<std::string> & value) = 0;
  386. ///String map <-> Json map of int
  387. virtual void serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value) = 0;
  388. virtual void pop() = 0;
  389. virtual void pushStruct(const std::string & fieldName) = 0;
  390. virtual void pushArray(const std::string & fieldName) = 0;
  391. virtual void pushArrayElement(const size_t index) = 0;
  392. virtual void pushField(const std::string & fieldName) = 0;
  393. virtual void resizeCurrent(const size_t newSize, JsonNode::JsonType type){};
  394. virtual void serializeInternal(std::string & value) = 0;
  395. virtual void serializeInternal(int64_t & value) = 0;
  396. void readLICPart(const JsonNode & part, const JsonSerializeFormat::TDecoder & decoder, std::set<si32> & value) const;
  397. private:
  398. const IInstanceResolver * instanceResolver;
  399. template<typename VType, typename DVType, typename IType, typename... Args>
  400. void doSerializeInternal(const std::string & fieldName, VType & value, const std::optional<DVType> & defaultValue, Args... args)
  401. {
  402. const std::optional<IType> tempDefault = defaultValue ? std::optional<IType>(static_cast<IType>(defaultValue.value())) : std::nullopt;
  403. auto temp = static_cast<IType>(value);
  404. serializeInternal(fieldName, temp, tempDefault, args...);
  405. if(!saving)
  406. value = static_cast<VType>(temp);
  407. }
  408. template<typename VType, typename IType, typename... Args>
  409. void dispatchOptional(const std::string & fieldName, std::optional<VType> & value, Args... args)
  410. {
  411. if(saving)
  412. {
  413. if(value)
  414. {
  415. auto temp = static_cast<IType>(value.value());
  416. pushField(fieldName);
  417. serializeInternal(temp, args...);
  418. pop();
  419. }
  420. }
  421. else
  422. {
  423. pushField(fieldName);
  424. if(getCurrent().getType() == JsonNode::JsonType::DATA_NULL)
  425. {
  426. value = std::nullopt;
  427. }
  428. else
  429. {
  430. IType temp = IType();
  431. serializeInternal(temp, args...);
  432. value = std::make_optional(temp);
  433. }
  434. pop();
  435. }
  436. }
  437. friend class JsonSerializeHelper;
  438. friend class JsonStructSerializer;
  439. friend class JsonArraySerializer;
  440. };
  441. template <typename Container>
  442. void JsonArraySerializer::syncSize(Container & c, JsonNode::JsonType type)
  443. {
  444. if(owner->saving)
  445. resize(c.size(), type);
  446. else
  447. c.resize(size());
  448. }
  449. template <typename T>
  450. void JsonArraySerializer::serializeInt(const size_t index, T & value)
  451. {
  452. auto temp = static_cast<int64_t>(value);
  453. serializeInt64(index, temp);
  454. if (!owner->saving)
  455. value = static_cast<T>(temp);
  456. };
  457. VCMI_LIB_NAMESPACE_END