JsonNode.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. {
  67. static_assert(std::is_arithmetic<T>::value, "This works with numbers only.");
  68. std::vector<T> ret;
  69. BOOST_FOREACH(const JsonNode &node, Vector())
  70. {
  71. ret.push_back(node.Float());
  72. }
  73. return ret;
  74. }
  75. //operator [], for structs only - get child node by name
  76. JsonNode & operator[](std::string child);
  77. const JsonNode & operator[](std::string child) const;
  78. //error value for const operator[]
  79. static const JsonNode nullNode;
  80. /// recursivly merges source into dest, replacing identical fields
  81. /// struct : recursively calls this function
  82. /// arrays : append array in dest with data from source
  83. /// values : value in source will replace value in dest
  84. /// null : if value in source is present but set to null it will delete entry in dest
  85. /// this function will destroy data in source
  86. static void merge(JsonNode & dest, JsonNode & source);
  87. /// this function will preserve data stored in source by creating copy
  88. static void mergeCopy(JsonNode & dest, JsonNode source);
  89. };
  90. class JsonWriter
  91. {
  92. //prefix for each line (tabulation)
  93. std::string prefix;
  94. std::ostream &out;
  95. public:
  96. template<typename Iterator>
  97. void writeContainer(Iterator begin, Iterator end);
  98. void writeEntry(JsonMap::const_iterator entry);
  99. void writeEntry(JsonVector::const_iterator entry);
  100. void writeString(const std::string &string);
  101. void writeNode(const JsonNode &node);
  102. JsonWriter(std::ostream &output, const JsonNode &node);
  103. };
  104. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const JsonNode &node);
  105. //Tiny string class that uses const char* as data for speed, members are private
  106. //for ease of debugging and some compatibility with std::string
  107. class constString
  108. {
  109. const char *data;
  110. const size_t datasize;
  111. public:
  112. constString(const char * inputString, size_t stringSize):
  113. data(inputString),
  114. datasize(stringSize)
  115. {
  116. }
  117. inline size_t size() const
  118. {
  119. return datasize;
  120. };
  121. inline const char& operator[] (size_t position)
  122. {
  123. assert (position < datasize);
  124. return data[position];
  125. }
  126. };
  127. //Internal class for string -> JsonNode conversion
  128. class JsonParser
  129. {
  130. std::string errors; // Contains description of all encountered errors
  131. constString input; // Input data
  132. ui32 lineCount; // Currently parsed line, starting from 1
  133. size_t lineStart; // Position of current line start
  134. size_t pos; // Current position of parser
  135. //Helpers
  136. bool extractEscaping(std::string &str);
  137. bool extractLiteral(const std::string &literal);
  138. bool extractString(std::string &string);
  139. bool extractWhitespace(bool verbose = true);
  140. bool extractSeparator();
  141. bool extractElement(JsonNode &node, char terminator);
  142. //Methods for extracting JSON data
  143. bool extractArray(JsonNode &node);
  144. bool extractFalse(JsonNode &node);
  145. bool extractFloat(JsonNode &node);
  146. bool extractNull(JsonNode &node);
  147. bool extractString(JsonNode &node);
  148. bool extractStruct(JsonNode &node);
  149. bool extractTrue(JsonNode &node);
  150. bool extractValue(JsonNode &node);
  151. //Add error\warning message to list
  152. bool error(const std::string &message, bool warning=false);
  153. public:
  154. JsonParser(const char * inputString, size_t stringSize, JsonNode &root);
  155. };
  156. //Internal class for Json validation, used automaticaly in JsonNode constructor. Behaviour:
  157. // - "schema" entry from root node is used for validation and will be removed
  158. // - any missing entries will be replaced with default value from schema (if present)
  159. // - if entry uses different type than defined in schema it will be removed
  160. // - entries nod described in schema will be kept unchanged
  161. class JsonValidator
  162. {
  163. std::string errors; // Contains description of all encountered errors
  164. std::list<std::string> currentPath; // path from root node to current one
  165. bool minimize;
  166. bool validateType(JsonNode &node, const JsonNode &schema, JsonNode::JsonType type);
  167. bool validateSchema(JsonNode::JsonType &type, const JsonNode &schema);
  168. bool validateNode(JsonNode &node, const JsonNode &schema, const std::string &name);
  169. bool validateItems(JsonNode &node, const JsonNode &schema);
  170. bool validateProperties(JsonNode &node, const JsonNode &schema);
  171. bool addMessage(const std::string &message);
  172. public:
  173. // validate node with "schema" entry
  174. JsonValidator(JsonNode &root, bool minimize=false);
  175. // validate with external schema
  176. JsonValidator(JsonNode &root, const JsonNode &schema, bool minimize=false);
  177. };
  178. DLL_LINKAGE Bonus * ParseBonus (const JsonVector &ability_vec);
  179. DLL_LINKAGE Bonus * ParseBonus (const JsonNode &bonus);
  180. DLL_LINKAGE void UnparseBonus (JsonNode &node, const Bonus * bonus);