TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_json.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <string>
6#include <string_view>
7#include <utility>
8#include <variant>
9#include <vector>
10
11namespace tachyon
12{
15
19 {
20 public:
21 using Array = std::vector<JsonValue>;
22 using Object = std::vector<std::pair<std::string, JsonValue>>;
23
26 JsonValue() = default;
27 JsonValue(std::nullptr_t) {}
28 JsonValue(bool value) : m_value(value) {}
29 JsonValue(int value) : m_value(double(value)) {}
30 JsonValue(double value) : m_value(value) {}
31 JsonValue(std::string value) : m_value(std::move(value)) {}
32 JsonValue(std::string_view value) : m_value(std::string(value)) {}
33 JsonValue(const char* value) : m_value(std::string(value)) {}
34 JsonValue(Array value) : m_value(std::move(value)) {}
35 JsonValue(Object value) : m_value(std::move(value)) {}
37
40 static JsonValue MakeArray() { return JsonValue(Array{}); }
41 static JsonValue MakeObject() { return JsonValue(Object{}); }
43
45 static Result<JsonValue> Parse(std::string_view text);
47 std::string Write() const;
48
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; }
58
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; }
66 const std::string& AsString() const;
67 const Array& Items() const;
68 const Object& Members() const;
70
72 const JsonValue* Find(std::string_view key) const;
73
77 JsonValue& Set(std::string_view key, JsonValue value);
80
81 private:
82 void WriteTo(std::string& out) const;
83
84 std::variant<std::nullptr_t, bool, double, std::string, Array, Object> m_value = nullptr;
85 };
86
87}
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_id.h:91
Definition gui_logging.h:24