JsonNode.h 5.4 KB

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