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