JsonSerializeFormat.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 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 JsonSerializeHelper: public boost::noncopyable
  23. {
  24. public:
  25. JsonSerializeHelper(JsonSerializeHelper && other);
  26. virtual ~JsonSerializeHelper();
  27. JsonNode & get();
  28. JsonSerializeFormat * operator->();
  29. JsonStructSerializer enterStruct(const std::string & fieldName);
  30. JsonArraySerializer enterArray(const std::string & fieldName);
  31. protected:
  32. JsonSerializeHelper(JsonSerializeFormat & owner_, JsonNode * thisNode_);
  33. JsonSerializeHelper(JsonSerializeHelper & parent, const std::string & fieldName);
  34. JsonSerializeFormat & owner;
  35. JsonNode * thisNode;
  36. JsonNode * parentNode;
  37. friend class JsonStructSerializer;
  38. private:
  39. bool restoreState;
  40. };
  41. class JsonStructSerializer: public JsonSerializeHelper
  42. {
  43. public:
  44. bool optional;
  45. JsonStructSerializer(JsonStructSerializer && other);
  46. ~JsonStructSerializer();
  47. protected:
  48. JsonStructSerializer(JsonSerializeFormat & owner_, JsonNode * thisNode_);
  49. JsonStructSerializer(JsonSerializeFormat & owner_, const std::string & fieldName);
  50. JsonStructSerializer(JsonSerializeHelper & parent, const std::string & fieldName);
  51. friend class JsonSerializeFormat;
  52. friend class JsonSerializeHelper;
  53. friend class JsonArraySerializer;
  54. };
  55. class JsonArraySerializer: public JsonSerializeHelper
  56. {
  57. public:
  58. JsonArraySerializer(JsonStructSerializer && other);
  59. JsonStructSerializer enterStruct(const size_t index);
  60. template <typename Container>
  61. void syncSize(Container & c, JsonNode::JsonType type = JsonNode::DATA_NULL);
  62. ///vector of serializable <-> Json vector of structs
  63. template <typename Element>
  64. void serializeStruct(std::vector<Element> & value)
  65. {
  66. syncSize(value, JsonNode::DATA_STRUCT);
  67. for(size_t idx = 0; idx < size(); idx++)
  68. {
  69. auto s = enterStruct(idx);
  70. value[idx].serializeJson(owner);
  71. }
  72. }
  73. void resize(const size_t newSize);
  74. void resize(const size_t newSize, JsonNode::JsonType type);
  75. size_t size() const;
  76. protected:
  77. JsonArraySerializer(JsonSerializeFormat & owner_, const std::string & fieldName);
  78. JsonArraySerializer(JsonSerializeHelper & parent, const std::string & fieldName);
  79. friend class JsonSerializeFormat;
  80. friend class JsonSerializeHelper;
  81. };
  82. class JsonSerializeFormat: public boost::noncopyable
  83. {
  84. public:
  85. ///user-provided callback to resolve string identifier
  86. ///returns resolved identifier or -1 on error
  87. typedef std::function<si32(const std::string &)> TDecoder;
  88. ///user-provided callback to get string identifier
  89. ///may assume that object index is valid
  90. typedef std::function<std::string(si32)> TEncoder;
  91. typedef std::function<void(JsonSerializeFormat &)> TSerialize;
  92. struct LIC
  93. {
  94. LIC(const std::vector<bool> & Standard, const TDecoder Decoder, const TEncoder Encoder);
  95. const std::vector<bool> & standard;
  96. const TDecoder decoder;
  97. const TEncoder encoder;
  98. std::vector<bool> all, any, none;
  99. };
  100. struct LICSet
  101. {
  102. LICSet(const std::set<si32> & Standard, const TDecoder Decoder, const TEncoder Encoder);
  103. const std::set<si32> & standard;
  104. const TDecoder decoder;
  105. const TEncoder encoder;
  106. std::set<si32> all, any, none;
  107. };
  108. const bool saving;
  109. JsonSerializeFormat() = delete;
  110. virtual ~JsonSerializeFormat() = default;
  111. JsonNode & getRoot()
  112. {
  113. return * root;
  114. };
  115. JsonNode & getCurrent()
  116. {
  117. return * current;
  118. };
  119. JsonStructSerializer enterStruct(const std::string & fieldName);
  120. JsonArraySerializer enterArray(const std::string & fieldName);
  121. ///Anything comparable <-> Json bool
  122. template <typename T>
  123. void serializeBool(const std::string & fieldName, T & value, const T trueValue, const T falseValue, const T defaultValue)
  124. {
  125. boost::logic::tribool temp(boost::logic::indeterminate);
  126. if(value == defaultValue)
  127. ;//leave as indeterminate
  128. else if(value == trueValue)
  129. temp = true;
  130. else if(value == falseValue)
  131. temp = false;
  132. serializeInternal(fieldName, temp);
  133. if(!saving)
  134. {
  135. if(boost::logic::indeterminate(temp))
  136. value = defaultValue;
  137. else
  138. value = temp ? trueValue : falseValue;
  139. }
  140. }
  141. ///bool <-> Json bool
  142. void serializeBool(const std::string & fieldName, bool & value);
  143. ///tribool <-> Json bool
  144. void serializeBool(const std::string & fieldName, boost::logic::tribool & value)
  145. {
  146. serializeInternal(fieldName, value);
  147. };
  148. /** @brief Restrictive ("anyOf") simple serialization of Logical identifier condition, simple deserialization (allOf=anyOf)
  149. *
  150. * @param fieldName
  151. * @param decoder resolve callback, should report errors itself and do not throw
  152. * @param encoder encode callback, should report errors itself and do not throw
  153. * @param value target value, must be resized properly
  154. *
  155. */
  156. virtual void serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value) = 0;
  157. /** @brief Complete serialization of Logical identifier condition
  158. */
  159. virtual void serializeLIC(const std::string & fieldName, LIC & value) = 0;
  160. /** @brief Complete serialization of Logical identifier condition. (Special version)
  161. * Assumes that all values are allowed by default, and standard contains them
  162. */
  163. virtual void serializeLIC(const std::string & fieldName, LICSet & value) = 0;
  164. ///String <-> Json string
  165. virtual void serializeString(const std::string & fieldName, std::string & value) = 0;
  166. ///si32-convertible enum <-> Json string enum
  167. template <typename T>
  168. void serializeEnum(const std::string & fieldName, T & value, const std::vector<std::string> & enumMap)
  169. {
  170. doSerializeInternal<T, T, si32>(fieldName, value, boost::none, enumMap);
  171. };
  172. ///si32-convertible enum <-> Json string enum
  173. template <typename T, typename U>
  174. void serializeEnum(const std::string & fieldName, T & value, const U & defaultValue, const std::vector<std::string> & enumMap)
  175. {
  176. doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, enumMap);
  177. };
  178. template <typename T, typename U, typename C>
  179. void serializeEnum(const std::string & fieldName, T & value, const U & defaultValue, const C & enumMap)
  180. {
  181. std::vector<std::string> enumMapCopy;
  182. std::copy(std::begin(enumMap), std::end(enumMap), std::back_inserter(enumMapCopy));
  183. doSerializeInternal<T, U, si32>(fieldName, value, defaultValue, enumMapCopy);
  184. };
  185. ///Anything double-convertible <-> Json double
  186. template <typename T>
  187. void serializeFloat(const std::string & fieldName, T & value)
  188. {
  189. doSerializeInternal<T, T, double>(fieldName, value, boost::none);
  190. };
  191. ///Anything double-convertible <-> Json double
  192. template <typename T, typename U>
  193. void serializeFloat(const std::string & fieldName, T & value, const U & defaultValue)
  194. {
  195. doSerializeInternal<T, U, double>(fieldName, value, defaultValue);
  196. };
  197. ///Anything int64-convertible <-> Json integer
  198. template <typename T>
  199. void serializeInt(const std::string & fieldName, T & value)
  200. {
  201. doSerializeInternal<T, T, si64>(fieldName, value, boost::none);
  202. };
  203. ///Anything int64-convertible <-> Json integer
  204. template <typename T, typename U>
  205. void serializeInt(const std::string & fieldName, T & value, const U & defaultValue)
  206. {
  207. doSerializeInternal<T, U, si64>(fieldName, value, defaultValue);
  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 vector <-> Json array of string
  216. template <typename T>
  217. void serializeIdArray(const std::string & fieldName, std::vector<T> & value, const TDecoder & decoder, const TEncoder & encoder)
  218. {
  219. std::vector<si32> temp;
  220. if(saving)
  221. {
  222. temp.reserve(value.size());
  223. for(const T & vitem : value)
  224. {
  225. si32 item = static_cast<si32>(vitem);
  226. temp.push_back(item);
  227. }
  228. }
  229. serializeInternal(fieldName, temp, decoder, encoder);
  230. if(!saving)
  231. {
  232. value.clear();
  233. value.reserve(temp.size());
  234. for(const si32 item : temp)
  235. {
  236. T vitem = static_cast<T>(item);
  237. value.push_back(vitem);
  238. }
  239. }
  240. }
  241. ///si32-convertible identifier set <-> Json array of string
  242. template <typename T>
  243. void serializeIdArray(const std::string & fieldName, std::set<T> & value, const TDecoder & decoder, const TEncoder & encoder)
  244. {
  245. std::vector<si32> temp;
  246. if(saving)
  247. {
  248. temp.reserve(value.size());
  249. for(const T & vitem : value)
  250. {
  251. si32 item = static_cast<si32>(vitem);
  252. temp.push_back(item);
  253. }
  254. }
  255. serializeInternal(fieldName, temp, decoder, encoder);
  256. if(!saving)
  257. {
  258. value.clear();
  259. for(const si32 item : temp)
  260. {
  261. T vitem = static_cast<T>(item);
  262. value.insert(vitem);
  263. }
  264. }
  265. }
  266. ///bitmask <-> Json array of string
  267. template <typename T, int Size>
  268. void serializeIdArray(const std::string & fieldName, T & value, const T & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
  269. {
  270. static_assert(8 * sizeof(T) >= Size, "Mask size too small");
  271. std::vector<si32> temp;
  272. temp.reserve(Size);
  273. if(saving && value != defaultValue)
  274. {
  275. for(si32 i = 0; i < Size; i++)
  276. if(value & (1 << i))
  277. temp.push_back(i);
  278. serializeInternal(fieldName, temp, decoder, encoder);
  279. }
  280. if(!saving)
  281. {
  282. serializeInternal(fieldName, temp, decoder, encoder);
  283. if(temp.empty())
  284. value = defaultValue;
  285. else
  286. {
  287. value = 0;
  288. for(auto i : temp)
  289. value |= (1 << i);
  290. }
  291. }
  292. }
  293. ///si32-convertible instance identifier <-> Json string
  294. template <typename T>
  295. void serializeInstance(const std::string & fieldName, T & value, const T & defaultValue)
  296. {
  297. const TDecoder decoder = std::bind(&IInstanceResolver::decode, instanceResolver, _1);
  298. const TEncoder endoder = std::bind(&IInstanceResolver::encode, instanceResolver, _1);
  299. serializeId<T>(fieldName, value, defaultValue, decoder, endoder);
  300. }
  301. protected:
  302. JsonNode * root;
  303. JsonNode * current;
  304. JsonSerializeFormat(const IInstanceResolver * instanceResolver_, JsonNode & root_, const bool saving_);
  305. ///bool <-> Json bool, indeterminate is default
  306. virtual void serializeInternal(const std::string & fieldName, boost::logic::tribool & value) = 0;
  307. ///Numeric Id <-> String Id
  308. virtual void serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder) = 0;
  309. ///Numeric Id vector <-> String Id vector
  310. virtual void serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder) = 0;
  311. ///Numeric <-> Json double
  312. virtual void serializeInternal(const std::string & fieldName, double & value, const boost::optional<double> & defaultValue) = 0;
  313. ///Numeric <-> Json integer
  314. virtual void serializeInternal(const std::string & fieldName, si64 & value, const boost::optional<si64> & defaultValue) = 0;
  315. ///Enum/Numeric <-> Json string enum
  316. virtual void serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap) = 0;
  317. private:
  318. const IInstanceResolver * instanceResolver;
  319. template <typename VType, typename DVType, typename IType, typename... Args>
  320. void doSerializeInternal(const std::string & fieldName, VType & value, const boost::optional<DVType> & defaultValue, Args ... args)
  321. {
  322. const boost::optional<IType> tempDefault = defaultValue ? boost::optional<IType>(static_cast<IType>(defaultValue.get())) : boost::none;
  323. IType temp = static_cast<IType>(value);
  324. serializeInternal(fieldName, temp, tempDefault, args...);
  325. if(!saving)
  326. value = static_cast<VType>(temp);
  327. }
  328. friend class JsonSerializeHelper;
  329. friend class JsonStructSerializer;
  330. friend class JsonArraySerializer;
  331. };
  332. template <typename Container>
  333. void JsonArraySerializer::syncSize(Container & c, JsonNode::JsonType type)
  334. {
  335. if(owner.saving)
  336. resize(c.size(), type);
  337. else
  338. c.resize(size());
  339. }