Add Exceptions indicating which line contains error when parsing JSON

This commit is contained in:
Vyn 2024-11-29 10:50:49 +01:00
parent fd034eff71
commit 2639dba60a
9 changed files with 307 additions and 177 deletions

View file

@ -14,6 +14,8 @@ namespace rei::json {
JsonArray();
JsonArray& push(FieldValue& value);
JsonArray& push(FieldValue&& value);
JsonArray& push(int value);
JsonArray& push(bool value);
JsonArray& push(const std::string& value);

View file

@ -16,6 +16,8 @@ namespace rei::json {
bool contains(const std::string& key) const;
JsonObject& set(const std::string& key, FieldValue& value);
JsonObject& set(const std::string& key, FieldValue&& value);
JsonObject& set(const std::string& key, int value);
JsonObject& set(const std::string& key, bool value);
JsonObject& set(const std::string& key, const std::string& value);

View file

@ -2,13 +2,27 @@
#include "Object.h"
#include "Array.h"
#include "Field.h"
#include <exception>
#include <format>
#include <string>
#include <variant>
namespace rei::json {
class std::variant<JsonObject, JsonArray> parse(const std::string& jsonStr);
std::string toString(std::variant<JsonObject, JsonArray>& element);
class ParsingError : public std::exception {
public:
ParsingError(const std::string& reason, int line, int column) {
str = std::format("{}:{}: {}", line, column, reason);
}
const char * what() const noexcept override {
return str.c_str();
}
private:
std::string str;
};
class FieldValue parse(const std::string& jsonStr);
std::string toString(JsonObject& object);
std::string toString(JsonArray& object);
}