60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include <map>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace mocca {
|
|
|
|
/**
|
|
* Minimal JSON value type used by the modern configuration and result layer.
|
|
*
|
|
* The project only needs a small subset of JSON infrastructure, so this keeps
|
|
* parsing and serialization self-contained instead of adding another external
|
|
* dependency.
|
|
*/
|
|
class JsonValue {
|
|
public:
|
|
using Array = std::vector<JsonValue>;
|
|
using Object = std::map<std::string, JsonValue>;
|
|
|
|
JsonValue() = default;
|
|
JsonValue(std::nullptr_t);
|
|
JsonValue(bool value);
|
|
JsonValue(double value);
|
|
JsonValue(int value);
|
|
JsonValue(std::string value);
|
|
JsonValue(const char* value);
|
|
JsonValue(Array value);
|
|
JsonValue(Object value);
|
|
|
|
[[nodiscard]] bool is_null() const;
|
|
[[nodiscard]] bool is_bool() const;
|
|
[[nodiscard]] bool is_number() const;
|
|
[[nodiscard]] bool is_string() const;
|
|
[[nodiscard]] bool is_array() const;
|
|
[[nodiscard]] bool is_object() const;
|
|
|
|
[[nodiscard]] bool as_bool() const;
|
|
[[nodiscard]] double as_number() const;
|
|
[[nodiscard]] const std::string& as_string() const;
|
|
[[nodiscard]] const Array& as_array() const;
|
|
[[nodiscard]] const Object& as_object() const;
|
|
[[nodiscard]] Array& as_array();
|
|
[[nodiscard]] Object& as_object();
|
|
|
|
[[nodiscard]] bool contains(const std::string& key) const;
|
|
[[nodiscard]] const JsonValue& at(const std::string& key) const;
|
|
|
|
private:
|
|
std::variant<std::nullptr_t, bool, double, std::string, Array, Object> data_{nullptr};
|
|
};
|
|
|
|
JsonValue parse_json(std::string_view text);
|
|
std::string to_json_string(const JsonValue& value, int indent = 2);
|
|
|
|
} // namespace mocca
|