JsonSerializeFormat.h 15 KB

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