JsonNode.h 5.6 KB

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