JsonDetail.h 4.0 KB

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