21 using Array = std::vector<JsonValue>;
22 using Object = std::vector<std::pair<std::string, JsonValue>>;
51 bool IsNull()
const {
return m_value.index() == 0; }
52 bool IsBool()
const {
return m_value.index() == 1; }
53 bool IsNumber()
const {
return m_value.index() == 2; }
54 bool IsString()
const {
return m_value.index() == 3; }
55 bool IsArray()
const {
return m_value.index() == 4; }
56 bool IsObject()
const {
return m_value.index() == 5; }
61 bool AsBool(
bool fallback =
false)
const {
return IsBool() ? std::get<1>(m_value) : fallback; }
62 double AsNumber(
double fallback = 0.0)
const {
return IsNumber() ? std::get<2>(m_value) : fallback; }
82 void WriteTo(std::string& out)
const;
84 std::variant<std::nullptr_t, bool, double, std::string, Array, Object> m_value =
nullptr;
bool IsBool() const
True when the value is of that type.
Definition gui_json.h:52
const Array & Items() const
The string, array items or object members; empty when the value is of another type.
std::vector< JsonValue > Array
Definition gui_json.h:21
bool AsBool(bool fallback=false) const
The value, or fallback when it is of another type.
Definition gui_json.h:61
std::string Write() const
Compact JSON text (no whitespace); NaN and infinity are written as null.
bool IsString() const
True when the value is of that type.
Definition gui_json.h:54
bool IsObject() const
True when the value is of that type.
Definition gui_json.h:56
double AsNumber(double fallback=0.0) const
The value, or fallback when it is of another type.
Definition gui_json.h:62
bool IsNull() const
True when the value is of that type.
Definition gui_json.h:51
const JsonValue * Find(std::string_view key) const
An object's member by name, or null when absent (or not an object).
JsonValue & Set(std::string_view key, JsonValue value)
Set adds or replaces an object's member, Push appends to an array.
std::vector< std::pair< std::string, JsonValue > > Object
members in order
Definition gui_json.h:22
JsonValue & Push(JsonValue value)
Set adds or replaces an object's member, Push appends to an array.
const std::string & AsString() const
The string, array items or object members; empty when the value is of another type.
static JsonValue MakeObject()
An empty array / object, to fill with Push / Set.
Definition gui_json.h:41
static JsonValue MakeArray()
An empty array / object, to fill with Push / Set.
Definition gui_json.h:40
bool IsNumber() const
True when the value is of that type.
Definition gui_json.h:53
const Object & Members() const
The string, array items or object members; empty when the value is of another type.
bool IsArray() const
True when the value is of that type.
Definition gui_json.h:55
static Result< JsonValue > Parse(std::string_view text)
Parses JSON text. Nesting deeper than 64 is an error, not a stack overflow.
A value, or the Error saying why there is none.
Definition gui_result.h:36
JsonValue()=default
Default is null.
JsonValue(const char *value)
Definition gui_json.h:33
JsonValue(bool value)
Definition gui_json.h:28
JsonValue(std::string value)
Definition gui_json.h:31
JsonValue(Object value)
Definition gui_json.h:35
JsonValue(std::string_view value)
Definition gui_json.h:32
JsonValue(Array value)
Definition gui_json.h:34
JsonValue(double value)
Definition gui_json.h:30
JsonValue(std::nullptr_t)
Definition gui_json.h:27
JsonValue(int value)
Definition gui_json.h:29
Definition gui_logging.h:24