JsonDetail.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * JsonDetail.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "JsonNode.h"
  12. class JsonWriter
  13. {
  14. //prefix for each line (tabulation)
  15. std::string prefix;
  16. std::ostream &out;
  17. public:
  18. template<typename Iterator>
  19. void writeContainer(Iterator begin, Iterator end);
  20. void writeEntry(JsonMap::const_iterator entry);
  21. void writeEntry(JsonVector::const_iterator entry);
  22. void writeString(const std::string &string);
  23. void writeNode(const JsonNode &node);
  24. JsonWriter(std::ostream &output, const JsonNode &node);
  25. };
  26. //Tiny string class that uses const char* as data for speed, members are private
  27. //for ease of debugging and some compatibility with std::string
  28. class constString
  29. {
  30. const char *data;
  31. const size_t datasize;
  32. public:
  33. constString(const char * inputString, size_t stringSize):
  34. data(inputString),
  35. datasize(stringSize)
  36. {
  37. }
  38. inline size_t size() const
  39. {
  40. return datasize;
  41. };
  42. inline const char& operator[] (size_t position)
  43. {
  44. assert (position < datasize);
  45. return data[position];
  46. }
  47. };
  48. //Internal class for string -> JsonNode conversion
  49. class JsonParser
  50. {
  51. std::string errors; // Contains description of all encountered errors
  52. constString input; // Input data
  53. ui32 lineCount; // Currently parsed line, starting from 1
  54. size_t lineStart; // Position of current line start
  55. size_t pos; // Current position of parser
  56. //Helpers
  57. bool extractEscaping(std::string &str);
  58. bool extractLiteral(const std::string &literal);
  59. bool extractString(std::string &string);
  60. bool extractWhitespace(bool verbose = true);
  61. bool extractSeparator();
  62. bool extractElement(JsonNode &node, char terminator);
  63. //Methods for extracting JSON data
  64. bool extractArray(JsonNode &node);
  65. bool extractFalse(JsonNode &node);
  66. bool extractFloat(JsonNode &node);
  67. bool extractNull(JsonNode &node);
  68. bool extractString(JsonNode &node);
  69. bool extractStruct(JsonNode &node);
  70. bool extractTrue(JsonNode &node);
  71. bool extractValue(JsonNode &node);
  72. //Add error\warning message to list
  73. bool error(const std::string &message, bool warning=false);
  74. public:
  75. JsonParser(const char * inputString, size_t stringSize);
  76. /// do actual parsing. filename is name of file that will printed to console if any errors were found
  77. JsonNode parse(std::string fileName);
  78. /// returns true if parsing was successful
  79. bool isValid();
  80. };
  81. //Internal class for Json validation. Mostly compilant with json-schema v4 draft
  82. namespace Validation
  83. {
  84. /// struct used to pass data around during validation
  85. struct ValidationData
  86. {
  87. /// path from root node to current one.
  88. /// JsonNode is used as variant - either string (name of node) or as float (index in list)
  89. std::vector<JsonNode> currentPath;
  90. /// Stack of used schemas. Last schema is the one used currently.
  91. /// May contain multiple items in case if remote references were found
  92. std::vector<std::string> usedSchemas;
  93. /// generates error message
  94. std::string makeErrorMessage(const std::string &message);
  95. };
  96. typedef std::function<std::string(const JsonNode &)> TFormatValidator;
  97. typedef std::unordered_map<std::string, TFormatValidator> TFormatMap;
  98. typedef std::function<std::string(ValidationData &, const JsonNode &, const JsonNode &, const JsonNode &)> TFieldValidator;
  99. typedef std::unordered_map<std::string, TFieldValidator> TValidatorMap;
  100. /// map of known fields in schema
  101. const TValidatorMap & getKnownFieldsFor(JsonNode::JsonType type);
  102. const TFormatMap & getKnownFormats();
  103. std::string check(std::string schemaName, const JsonNode & data);
  104. std::string check(std::string schemaName, const JsonNode & data, ValidationData & validator);
  105. std::string check(const JsonNode & schema, const JsonNode & data, ValidationData & validator);
  106. }