JsonSerializeFormat.h 15 KB

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