JsonParser.h 1.6 KB

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