8#include <unordered_map>
45 using Array = std::vector<Value>;
50 using Object = std::unordered_map<std::string, Value>;
58 explicit Value(
bool v) noexcept : m_value(v) {}
63 explicit Value(int64_t v) noexcept : m_value(v) {}
68 explicit Value(
const char* v) noexcept : m_value(std::string(v)) {}
73 explicit Value(std::string v) noexcept : m_value(std::move(v)) {}
78 explicit Value(std::initializer_list<Array::value_type> vs) noexcept : m_value(
Array(std::move(vs))) {}
83 explicit Value(
Array v) noexcept : m_value(std::move(v)) {}
88 explicit Value(std::initializer_list<Object::value_type> vs) noexcept : m_value(
Object(std::move(vs))) {}
120 bool isNull() const noexcept {
return std::holds_alternative<Null>(m_value); }
125 bool isBool() const noexcept {
return std::holds_alternative<bool>(m_value); }
130 bool isInteger() const noexcept {
return std::holds_alternative<int64_t>(m_value); }
135 bool isString() const noexcept {
return std::holds_alternative<std::string>(m_value); }
140 bool isArray() const noexcept {
return std::holds_alternative<Array>(m_value); }
145 bool isObject() const noexcept {
return std::holds_alternative<Object>(m_value); }
151 bool getBool()
const {
return get<bool>(
"Not a boolean value"); }
157 int64_t
getInteger()
const {
return get<int64_t>(
"Not an integer value"); }
163 std::string
getString()
const {
return get<std::string>(
"Not a string value"); }
188 template <
typename F>
189 auto visit(F&& f)
const {
return std::visit(f, m_value); }
191 using V = std::variant<Null, bool, int64_t, std::string, Array, Object, double>;
195 Value(std::in_place_t , V&& v) noexcept : m_value(std::move(v)) {}
197 template <
typename T>
198 T get(
const std::string& onError)
const
200 const auto* res = std::get_if<T>(&m_value);
202 throw Error(onError);
210 return std::visit([](
auto&& v) -> std::string {
211 using T = std::decay_t<
decltype(v)>;
212 if constexpr (std::is_same_v<T, Null>) {
214 }
else if constexpr (std::is_same_v<T, bool>) {
218 }
else if constexpr (std::is_same_v<T, std::string>) {
219 return "\"" + v +
"\"";
220 }
else if constexpr (std::is_same_v<T, Array>) {
222 std::accumulate(v.begin(), v.end(), std::string{}, [](
const auto& a,
const auto& i){
225 return a +
"," + i.toString();
228 }
else if constexpr (std::is_same_v<T, Object>) {
230 std::accumulate(v.begin(), v.end(), std::string{}, [](
const auto& a,
const auto& i){
232 return
"\"" + i.first +
"\":" + i.second.toString();
233 return a +
",\"" + i.first +
"\":" + i.second.toString();
237 return std::to_string(v);
bool getBool() const
Gets the boolean value.
Definition value.h:151
int64_t getInteger() const
Gets the integer value.
Definition value.h:157
bool isNull() const noexcept
Checks if the value is null.
Definition value.h:120
std::string getString() const
Gets the string value.
Definition value.h:163
bool isObject() const noexcept
Checks if the value is an object.
Definition value.h:145
bool isString() const noexcept
Checks if the value is a string.
Definition value.h:135
static Value number(double v) noexcept
Creates a floating point number Value.
Definition value.h:100
Value(std::string v) noexcept
String constructor.
Definition value.h:73
Value(Object v) noexcept
Object constructor.
Definition value.h:93
Value(const char *v) noexcept
C-string constructor.
Definition value.h:68
Value(Value &&)=default
Move constructor.
bool isInteger() const noexcept
Checks if the value is an integer.
Definition value.h:130
Array getArray() const
Gets the array value.
Definition value.h:169
Object getObject() const
Gets the object value.
Definition value.h:175
Value() noexcept
Default constructor. Creates a null value.
Definition value.h:53
Value(std::initializer_list< Object::value_type > vs) noexcept
Object initializer list constructor.
Definition value.h:88
auto visit(F &&f) const
Applies a visitor function to the value.
Definition value.h:189
std::vector< Value > Array
Represents a JSON array (vector of Values).
Definition value.h:45
Value(Array v) noexcept
Array constructor.
Definition value.h:83
std::string toString() const
Converts the value to its JSON string representation.
Definition value.h:208
Value(int64_t v) noexcept
Integer constructor.
Definition value.h:63
Value(bool v) noexcept
Boolean constructor.
Definition value.h:58
Value & operator=(const Value &)=default
Copy assignment operator.
Value(const Value &)=default
Copy constructor.
bool isBool() const noexcept
Checks if the value is a boolean.
Definition value.h:125
Value & operator=(Value &&)=default
Move assignment operator.
Value(std::initializer_list< Array::value_type > vs) noexcept
Array initializer list constructor.
Definition value.h:78
std::unordered_map< std::string, Value > Object
Represents a JSON object (string to Value map).
Definition value.h:50
bool isArray() const noexcept
Checks if the value is an array.
Definition value.h:140
~Value()=default
Destructor.
All classes, functions and constants are here.
Definition error.h:6
Base class for all exceptions in the library.
Definition error.h:12
Error(const std::string &message) noexcept
Constructor.
Definition value.h:34
Represents a JSON null value.
Definition value.h:40