JsonNode.h 5.6 KB

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