2
0

JsonNode.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. //operator [], for structs only - get child node by name
  63. JsonNode & operator[](std::string child);
  64. const JsonNode & operator[](std::string child) const;
  65. //error value for const operator[]
  66. static const JsonNode nullNode;
  67. };
  68. class JsonWriter
  69. {
  70. //prefix for each line (tabulation)
  71. std::string prefix;
  72. std::ostream &out;
  73. public:
  74. template<typename Iterator>
  75. void writeContainer(Iterator begin, Iterator end);
  76. void writeEntry(JsonMap::const_iterator entry);
  77. void writeEntry(JsonVector::const_iterator entry);
  78. void writeString(const std::string &string);
  79. void writeNode(const JsonNode &node);
  80. JsonWriter(std::ostream &output, const JsonNode &node);
  81. };
  82. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const JsonNode &node);
  83. //Tiny string class that uses const char* as data for speed, members are private
  84. //for ease of debugging and some compatibility with std::string
  85. class constString
  86. {
  87. const char *data;
  88. const size_t datasize;
  89. public:
  90. constString(const char * inputString, size_t stringSize):
  91. data(inputString),
  92. datasize(stringSize)
  93. {
  94. }
  95. inline size_t size() const
  96. {
  97. return datasize;
  98. };
  99. inline const char& operator[] (size_t position)
  100. {
  101. assert (position < datasize);
  102. return data[position];
  103. }
  104. };
  105. //Internal class for string -> JsonNode conversion
  106. class JsonParser
  107. {
  108. std::string errors; // Contains description of all encountered errors
  109. constString input; // Input data
  110. ui32 lineCount; // Currently parsed line, starting from 1
  111. size_t lineStart; // Position of current line start
  112. size_t pos; // Current position of parser
  113. //Helpers
  114. bool extractEscaping(std::string &str);
  115. bool extractLiteral(const std::string &literal);
  116. bool extractString(std::string &string);
  117. bool extractWhitespace(bool verbose = true);
  118. bool extractSeparator();
  119. bool extractElement(JsonNode &node, char terminator);
  120. //Methods for extracting JSON data
  121. bool extractArray(JsonNode &node);
  122. bool extractFalse(JsonNode &node);
  123. bool extractFloat(JsonNode &node);
  124. bool extractNull(JsonNode &node);
  125. bool extractString(JsonNode &node);
  126. bool extractStruct(JsonNode &node);
  127. bool extractTrue(JsonNode &node);
  128. bool extractValue(JsonNode &node);
  129. //Add error\warning message to list
  130. bool error(const std::string &message, bool warning=false);
  131. public:
  132. JsonParser(const char * inputString, size_t stringSize, JsonNode &root);
  133. };
  134. //Internal class for Json validation, used automaticaly in JsonNode constructor. Behaviour:
  135. // - "schema" entry from root node is used for validation and will be removed
  136. // - any missing entries will be replaced with default value from schema (if present)
  137. // - if entry uses different type than defined in schema it will be removed
  138. // - entries nod described in schema will be kept unchanged
  139. class JsonValidator
  140. {
  141. std::string errors; // Contains description of all encountered errors
  142. std::list<std::string> currentPath; // path from root node to current one
  143. bool minimize;
  144. bool validateType(JsonNode &node, const JsonNode &schema, JsonNode::JsonType type);
  145. bool validateSchema(JsonNode::JsonType &type, const JsonNode &schema);
  146. bool validateNode(JsonNode &node, const JsonNode &schema, const std::string &name);
  147. bool validateItems(JsonNode &node, const JsonNode &schema);
  148. bool validateProperties(JsonNode &node, const JsonNode &schema);
  149. bool addMessage(const std::string &message);
  150. public:
  151. // validate node with "schema" entry
  152. JsonValidator(JsonNode &root, bool minimize=false);
  153. // validate with external schema
  154. JsonValidator(JsonNode &root, const JsonNode &schema, bool minimize=false);
  155. };