JsonDetail.h 4.0 KB

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