JsonParser.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * JsonParser.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. //Internal class for string -> JsonNode conversion
  14. class JsonParser
  15. {
  16. const JsonParsingSettings settings;
  17. std::string errors; // Contains description of all encountered errors
  18. std::string_view input; // Input data
  19. ui32 lineCount; // Currently parsed line, starting from 1
  20. size_t lineStart; // Position of current line start
  21. size_t pos; // Current position of parser
  22. //Helpers
  23. bool extractEscaping(std::string &str);
  24. bool extractLiteral(const std::string &literal);
  25. bool extractString(std::string &string);
  26. bool extractWhitespace(bool verbose = true);
  27. bool extractSeparator();
  28. bool extractElement(JsonNode &node, char terminator);
  29. //Methods for extracting JSON data
  30. bool extractArray(JsonNode &node);
  31. bool extractFalse(JsonNode &node);
  32. bool extractFloat(JsonNode &node);
  33. bool extractNull(JsonNode &node);
  34. bool extractString(JsonNode &node);
  35. bool extractStruct(JsonNode &node);
  36. bool extractTrue(JsonNode &node);
  37. bool extractValue(JsonNode &node);
  38. //Add error\warning message to list
  39. bool error(const std::string &message, bool warning=false);
  40. public:
  41. JsonParser(const char * inputString, size_t stringSize, const JsonParsingSettings & settings);
  42. /// do actual parsing. filename is name of file that will printed to console if any errors were found
  43. JsonNode parse(const std::string & fileName);
  44. /// returns true if parsing was successful
  45. bool isValid();
  46. };
  47. VCMI_LIB_NAMESPACE_END