JsonNode.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #pragma once
  2. class JsonNode;
  3. typedef std::map <std::string, JsonNode> JsonMap;
  4. typedef std::vector <JsonNode> JsonVector;
  5. struct Bonus;
  6. class ResourceID;
  7. class DLL_LINKAGE JsonNode
  8. {
  9. public:
  10. enum JsonType
  11. {
  12. DATA_NULL,
  13. DATA_BOOL,
  14. DATA_FLOAT,
  15. DATA_STRING,
  16. DATA_VECTOR,
  17. DATA_STRUCT
  18. };
  19. private:
  20. union JsonData
  21. {
  22. bool Bool;
  23. double Float;
  24. std::string* String;
  25. JsonVector* Vector;
  26. JsonMap* Struct;
  27. };
  28. JsonType type;
  29. JsonData data;
  30. public:
  31. //Create empty node
  32. JsonNode(JsonType Type = DATA_NULL);
  33. //Create tree from Json-formatted input
  34. explicit JsonNode(const char * data, size_t datasize);
  35. //Create tree from JSON file
  36. explicit JsonNode(ResourceID && fileURI);
  37. //Copy c-tor
  38. JsonNode(const JsonNode &copy);
  39. ~JsonNode();
  40. void swap(JsonNode &b);
  41. JsonNode& operator =(JsonNode node);
  42. bool operator == (const JsonNode &other) const;
  43. bool operator != (const JsonNode &other) const;
  44. //removes all nodes that are identical to default entry in schema
  45. void minimize(const JsonNode& schema);
  46. //check schema
  47. void validate(const JsonNode& schema);
  48. //Convert node to another type. Converting to NULL will clear all data
  49. void setType(JsonType Type);
  50. JsonType getType() const;
  51. bool isNull() const;
  52. //non-const accessors, node will change type on type mismatch
  53. bool & Bool();
  54. double & Float();
  55. std::string & String();
  56. JsonVector & Vector();
  57. JsonMap & Struct();
  58. //const accessors, will cause assertion failure on type mismatch
  59. const bool & Bool() const;
  60. const double & Float() const;
  61. const std::string & String() const;
  62. const JsonVector & Vector() const;
  63. const JsonMap & Struct() const;
  64. template<typename T>
  65. std::vector<T> StdVector() const;
  66. //operator [], for structs only - get child node by name
  67. JsonNode & operator[](std::string child);
  68. const JsonNode & operator[](std::string child) const;
  69. //error value for const operator[]
  70. static const JsonNode nullNode;
  71. /// recursivly merges source into dest, replacing identical fields
  72. /// struct : recursively calls this function
  73. /// arrays : append array in dest with data from source
  74. /// values : value in source will replace value in dest
  75. /// null : if value in source is present but set to null it will delete entry in dest
  76. /// this function will destroy data in source
  77. static void merge(JsonNode & dest, JsonNode & source);
  78. /// this function will preserve data stored in source by creating copy
  79. static void mergeCopy(JsonNode & dest, JsonNode source);
  80. };
  81. template<>
  82. inline std::vector<std::string> JsonNode::StdVector() const
  83. {
  84. std::vector<std::string> ret;
  85. BOOST_FOREACH(const JsonNode &node, Vector())
  86. {
  87. ret.push_back(node.String());
  88. }
  89. return ret;
  90. }
  91. template<typename T>
  92. std::vector<T> JsonNode::StdVector() const
  93. {
  94. static_assert(std::is_arithmetic<T>::value, "This works with numbers only.");
  95. std::vector<T> ret;
  96. BOOST_FOREACH(const JsonNode &node, Vector())
  97. {
  98. ret.push_back(node.Float());
  99. }
  100. return ret;
  101. }
  102. class JsonWriter
  103. {
  104. //prefix for each line (tabulation)
  105. std::string prefix;
  106. std::ostream &out;
  107. public:
  108. template<typename Iterator>
  109. void writeContainer(Iterator begin, Iterator end);
  110. void writeEntry(JsonMap::const_iterator entry);
  111. void writeEntry(JsonVector::const_iterator entry);
  112. void writeString(const std::string &string);
  113. void writeNode(const JsonNode &node);
  114. JsonWriter(std::ostream &output, const JsonNode &node);
  115. };
  116. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const JsonNode &node);
  117. //Tiny string class that uses const char* as data for speed, members are private
  118. //for ease of debugging and some compatibility with std::string
  119. class constString
  120. {
  121. const char *data;
  122. const size_t datasize;
  123. public:
  124. constString(const char * inputString, size_t stringSize):
  125. data(inputString),
  126. datasize(stringSize)
  127. {
  128. }
  129. inline size_t size() const
  130. {
  131. return datasize;
  132. };
  133. inline const char& operator[] (size_t position)
  134. {
  135. assert (position < datasize);
  136. return data[position];
  137. }
  138. };
  139. //Internal class for string -> JsonNode conversion
  140. class JsonParser
  141. {
  142. std::string errors; // Contains description of all encountered errors
  143. constString input; // Input data
  144. ui32 lineCount; // Currently parsed line, starting from 1
  145. size_t lineStart; // Position of current line start
  146. size_t pos; // Current position of parser
  147. //Helpers
  148. bool extractEscaping(std::string &str);
  149. bool extractLiteral(const std::string &literal);
  150. bool extractString(std::string &string);
  151. bool extractWhitespace(bool verbose = true);
  152. bool extractSeparator();
  153. bool extractElement(JsonNode &node, char terminator);
  154. //Methods for extracting JSON data
  155. bool extractArray(JsonNode &node);
  156. bool extractFalse(JsonNode &node);
  157. bool extractFloat(JsonNode &node);
  158. bool extractNull(JsonNode &node);
  159. bool extractString(JsonNode &node);
  160. bool extractStruct(JsonNode &node);
  161. bool extractTrue(JsonNode &node);
  162. bool extractValue(JsonNode &node);
  163. //Add error\warning message to list
  164. bool error(const std::string &message, bool warning=false);
  165. public:
  166. JsonParser(const char * inputString, size_t stringSize, JsonNode &root);
  167. };
  168. //Internal class for Json validation, used automaticaly in JsonNode constructor. Behaviour:
  169. // - "schema" entry from root node is used for validation and will be removed
  170. // - any missing entries will be replaced with default value from schema (if present)
  171. // - if entry uses different type than defined in schema it will be removed
  172. // - entries nod described in schema will be kept unchanged
  173. class JsonValidator
  174. {
  175. std::string errors; // Contains description of all encountered errors
  176. std::list<std::string> currentPath; // path from root node to current one
  177. bool minimize;
  178. bool validateType(JsonNode &node, const JsonNode &schema, JsonNode::JsonType type);
  179. bool validateSchema(JsonNode::JsonType &type, const JsonNode &schema);
  180. bool validateNode(JsonNode &node, const JsonNode &schema, const std::string &name);
  181. bool validateItems(JsonNode &node, const JsonNode &schema);
  182. bool validateProperties(JsonNode &node, const JsonNode &schema);
  183. bool addMessage(const std::string &message);
  184. public:
  185. // validate node with "schema" entry
  186. JsonValidator(JsonNode &root, bool minimize=false);
  187. // validate with external schema
  188. JsonValidator(JsonNode &root, const JsonNode &schema, bool minimize=false);
  189. };
  190. DLL_LINKAGE Bonus * ParseBonus (const JsonVector &ability_vec);
  191. DLL_LINKAGE Bonus * ParseBonus (const JsonNode &bonus);
  192. DLL_LINKAGE void UnparseBonus (JsonNode &node, const Bonus * bonus);