mirror of
https://codeberg.org/vyn/rei-json.git
synced 2025-07-06 03:23:18 +00:00
Add Exceptions indicating which line contains error when parsing JSON
This commit is contained in:
parent
fd034eff71
commit
2639dba60a
9 changed files with 307 additions and 177 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue