JsonNode.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * JsonNode.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 "../filesystem/ResourcePath.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class JsonNode;
  14. using JsonMap = std::map<std::string, JsonNode>;
  15. using JsonVector = std::vector<JsonNode>;
  16. struct Bonus;
  17. class CSelector;
  18. class CAddInfo;
  19. class ILimiter;
  20. class DLL_LINKAGE JsonNode
  21. {
  22. public:
  23. enum class JsonType
  24. {
  25. DATA_NULL,
  26. DATA_BOOL,
  27. DATA_FLOAT,
  28. DATA_STRING,
  29. DATA_VECTOR,
  30. DATA_STRUCT,
  31. DATA_INTEGER
  32. };
  33. private:
  34. using JsonData = std::variant<std::monostate, bool, double, std::string, JsonVector, JsonMap, int64_t>;
  35. JsonData data;
  36. /// Mod-origin of this particular field
  37. std::string modScope;
  38. bool overrideFlag = false;
  39. public:
  40. JsonNode() = default;
  41. /// Create single node with specified value
  42. explicit JsonNode(bool boolean);
  43. explicit JsonNode(int32_t number);
  44. explicit JsonNode(uint32_t number);
  45. explicit JsonNode(int64_t number);
  46. explicit JsonNode(double number);
  47. explicit JsonNode(const std::string & string);
  48. /// Create tree from Json-formatted input
  49. explicit JsonNode(const std::byte * data, size_t datasize);
  50. /// Create tree from JSON file
  51. explicit JsonNode(const JsonPath & fileURI);
  52. explicit JsonNode(const JsonPath & fileURI, const std::string & modName);
  53. explicit JsonNode(const JsonPath & fileURI, bool & isValidSyntax);
  54. bool operator == (const JsonNode &other) const;
  55. bool operator != (const JsonNode &other) const;
  56. const std::string & getModScope() const;
  57. void setModScope(const std::string & metadata, bool recursive = true);
  58. void setOverrideFlag(bool value);
  59. bool getOverrideFlag() const;
  60. /// Convert node to another type. Converting to nullptr will clear all data
  61. void setType(JsonType Type);
  62. JsonType getType() const;
  63. bool isNull() const;
  64. bool isNumber() const;
  65. bool isString() const;
  66. bool isVector() const;
  67. bool isStruct() const;
  68. /// true if node contains not-null data that cannot be extended via merging
  69. /// used for generating common base node from multiple nodes (e.g. bonuses)
  70. bool containsBaseData() const;
  71. bool isCompact() const;
  72. /// removes all data from node and sets type to null
  73. void clear();
  74. /// returns bool or bool equivalent of string value if 'success' is true, or false otherwise
  75. bool TryBoolFromString(bool & success) const;
  76. /// non-const accessors, node will change type on type mismatch
  77. bool & Bool();
  78. double & Float();
  79. si64 & Integer();
  80. std::string & String();
  81. JsonVector & Vector();
  82. JsonMap & Struct();
  83. /// const accessors, will cause assertion failure on type mismatch
  84. bool Bool() const;
  85. ///float and integer allowed
  86. double Float() const;
  87. ///only integer allowed
  88. si64 Integer() const;
  89. const std::string & String() const;
  90. const JsonVector & Vector() const;
  91. const JsonMap & Struct() const;
  92. /// returns resolved "json pointer" (string in format "/path/to/node")
  93. const JsonNode & resolvePointer(const std::string & jsonPointer) const;
  94. JsonNode & resolvePointer(const std::string & jsonPointer);
  95. /// convert json tree into specified type. Json tree must have same type as Type
  96. /// Valid types: bool, string, any numeric, map and vector
  97. /// example: convertTo< std::map< std::vector<int> > >();
  98. template<typename Type>
  99. Type convertTo() const;
  100. //operator [], for structs only - get child node by name
  101. JsonNode & operator[](const std::string & child);
  102. const JsonNode & operator[](const std::string & child) const;
  103. JsonNode & operator[](size_t child);
  104. const JsonNode & operator[](size_t child) const;
  105. std::string toCompactString() const;
  106. std::string toString() const;
  107. std::vector<std::byte> toBytes() const;
  108. template <typename Handler> void serialize(Handler &h)
  109. {
  110. h & modScope;
  111. if (h.version >= Handler::Version::JSON_FLAGS)
  112. {
  113. h & overrideFlag;
  114. }
  115. else
  116. {
  117. std::vector<std::string> oldFlags;
  118. h & oldFlags;
  119. }
  120. h & data;
  121. }
  122. };
  123. namespace JsonDetail
  124. {
  125. inline void convert(bool & value, const JsonNode & node)
  126. {
  127. value = node.Bool();
  128. }
  129. template<typename T>
  130. auto convert(T & value, const JsonNode & node) -> std::enable_if_t<std::is_integral_v<T>>
  131. {
  132. value = node.Integer();
  133. }
  134. template<typename T>
  135. auto convert(T & value, const JsonNode & node) -> std::enable_if_t<std::is_floating_point_v<T>>
  136. {
  137. value = node.Float();
  138. }
  139. inline void convert(std::string & value, const JsonNode & node)
  140. {
  141. value = node.String();
  142. }
  143. template <typename Type>
  144. void convert(std::map<std::string,Type> & value, const JsonNode & node)
  145. {
  146. value.clear();
  147. for (const JsonMap::value_type & entry : node.Struct())
  148. value.insert(entry.first, entry.second.convertTo<Type>());
  149. }
  150. template <typename Type>
  151. void convert(std::set<Type> & value, const JsonNode & node)
  152. {
  153. value.clear();
  154. for(const JsonVector::value_type & entry : node.Vector())
  155. {
  156. value.insert(entry.convertTo<Type>());
  157. }
  158. }
  159. template <typename Type>
  160. void convert(std::vector<Type> & value, const JsonNode & node)
  161. {
  162. value.clear();
  163. for(const JsonVector::value_type & entry : node.Vector())
  164. {
  165. value.push_back(entry.convertTo<Type>());
  166. }
  167. }
  168. }
  169. template<typename Type>
  170. Type JsonNode::convertTo() const
  171. {
  172. Type result;
  173. JsonDetail::convert(result, *this);
  174. return result;
  175. }
  176. VCMI_LIB_NAMESPACE_END