ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
json.h
Go to the documentation of this file.
1
17#pragma once
18
19#include <cstdint>
20#include <sstream>
21#include <string>
22#include <vector>
23
24namespace ascii_query {
25namespace json {
26
30inline std::string escape(const std::string &str) {
31 std::string result;
32 result.reserve(str.size() + 16);
33
34 for (char c : str) {
35 switch (c) {
36 case '"':
37 result += "\\\"";
38 break;
39 case '\\':
40 result += "\\\\";
41 break;
42 case '\b':
43 result += "\\b";
44 break;
45 case '\f':
46 result += "\\f";
47 break;
48 case '\n':
49 result += "\\n";
50 break;
51 case '\r':
52 result += "\\r";
53 break;
54 case '\t':
55 result += "\\t";
56 break;
57 default:
58 if (static_cast<unsigned char>(c) < 0x20) {
59 // Control character - escape as \uXXXX
60 char buf[8];
61 snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned char>(c));
62 result += buf;
63 } else {
64 result += c;
65 }
66 break;
67 }
68 }
69
70 return result;
71}
72
73// Forward declarations
74class JsonArray;
75class JsonObject;
76
80class JsonValue {
81public:
82 enum class Type { Null, Bool, Int, UInt, Double, String, Array, Object };
83
84 JsonValue() : type_(Type::Null) {}
85 JsonValue(std::nullptr_t) : type_(Type::Null) {}
86 JsonValue(bool b) : type_(Type::Bool), bool_val_(b) {}
87 JsonValue(int i) : type_(Type::Int), int_val_(i) {}
88 JsonValue(int64_t i) : type_(Type::Int), int_val_(i) {}
89 JsonValue(uint64_t u) : type_(Type::UInt), uint_val_(u) {}
90 JsonValue(double d) : type_(Type::Double), double_val_(d) {}
91 JsonValue(const char *s) : type_(Type::String), str_val_(s) {}
92 JsonValue(const std::string &s) : type_(Type::String), str_val_(s) {}
93 JsonValue(std::string &&s) : type_(Type::String), str_val_(std::move(s)) {}
94
95 // Array and Object are stored as pre-serialized strings
96 static JsonValue fromArray(const std::string &serialized) {
97 JsonValue v;
98 v.type_ = Type::Array;
99 v.str_val_ = serialized;
100 return v;
101 }
102
103 static JsonValue fromObject(const std::string &serialized) {
104 JsonValue v;
105 v.type_ = Type::Object;
106 v.str_val_ = serialized;
107 return v;
108 }
109
110 [[nodiscard]] std::string toString() const {
111 switch (type_) {
112 case Type::Null:
113 return "null";
114 case Type::Bool:
115 return bool_val_ ? "true" : "false";
116 case Type::Int:
117 return std::to_string(int_val_);
118 case Type::UInt:
119 return std::to_string(uint_val_);
120 case Type::Double: {
121 std::ostringstream oss;
122 oss << double_val_;
123 return oss.str();
124 }
125 case Type::String:
126 return "\"" + escape(str_val_) + "\"";
127 case Type::Array:
128 case Type::Object:
129 return str_val_; // Already serialized
130 }
131 return "null";
132 }
133
134private:
135 Type type_;
136 bool bool_val_ = false;
137 int64_t int_val_ = 0;
138 uint64_t uint_val_ = 0;
139 double double_val_ = 0.0;
140 std::string str_val_;
141};
142
147public:
148 JsonArray() = default;
149
150 JsonArray &add(const JsonValue &value) {
151 values_.push_back(value);
152 return *this;
153 }
154
155 JsonArray &add(const JsonArray &arr) {
156 values_.push_back(JsonValue::fromArray(arr.toString()));
157 return *this;
158 }
159
160 JsonArray &add(const JsonObject &obj);
161
162 [[nodiscard]] std::string toString() const {
163 std::string result = "[";
164 bool first = true;
165 for (const auto &v : values_) {
166 if (!first)
167 result += ",";
168 first = false;
169 result += v.toString();
170 }
171 result += "]";
172 return result;
173 }
174
175 [[nodiscard]] bool empty() const {
176 return values_.empty();
177 }
178 [[nodiscard]] size_t size() const {
179 return values_.size();
180 }
181
182private:
183 std::vector<JsonValue> values_;
184};
185
190public:
191 JsonObject() = default;
192
193 JsonObject &set(const std::string &key, const JsonValue &value) {
194 keys_.push_back(key);
195 values_.push_back(value);
196 return *this;
197 }
198
199 JsonObject &set(const std::string &key, const JsonArray &arr) {
200 keys_.push_back(key);
201 values_.push_back(JsonValue::fromArray(arr.toString()));
202 return *this;
203 }
204
205 JsonObject &set(const std::string &key, const JsonObject &obj) {
206 keys_.push_back(key);
207 values_.push_back(JsonValue::fromObject(obj.toString()));
208 return *this;
209 }
210
211 [[nodiscard]] std::string toString() const {
212 std::string result = "{";
213 for (size_t i = 0; i < keys_.size(); i++) {
214 if (i > 0)
215 result += ",";
216 result += "\"" + escape(keys_[i]) + "\":" + values_[i].toString();
217 }
218 result += "}";
219 return result;
220 }
221
222 [[nodiscard]] bool empty() const {
223 return keys_.empty();
224 }
225 [[nodiscard]] size_t size() const {
226 return keys_.size();
227 }
228
229private:
230 std::vector<std::string> keys_;
231 std::vector<JsonValue> values_;
232};
233
234// Deferred implementation
236 values_.push_back(JsonValue::fromObject(obj.toString()));
237 return *this;
238}
239
240} // namespace json
241} // namespace ascii_query
JSON array builder.
Definition json.h:146
JsonArray & add(const JsonArray &arr)
Definition json.h:155
size_t size() const
Definition json.h:178
std::string toString() const
Definition json.h:162
JsonArray & add(const JsonValue &value)
Definition json.h:150
JSON object builder.
Definition json.h:189
size_t size() const
Definition json.h:225
std::string toString() const
Definition json.h:211
JsonObject & set(const std::string &key, const JsonObject &obj)
Definition json.h:205
JsonObject & set(const std::string &key, const JsonArray &arr)
Definition json.h:199
JsonObject & set(const std::string &key, const JsonValue &value)
Definition json.h:193
JSON value wrapper (can hold any JSON type)
Definition json.h:80
JsonValue(uint64_t u)
Definition json.h:89
JsonValue(std::nullptr_t)
Definition json.h:85
JsonValue(const char *s)
Definition json.h:91
static JsonValue fromObject(const std::string &serialized)
Definition json.h:103
static JsonValue fromArray(const std::string &serialized)
Definition json.h:96
JsonValue(std::string &&s)
Definition json.h:93
JsonValue(const std::string &s)
Definition json.h:92
std::string toString() const
Definition json.h:110
unsigned long long uint64_t
Definition common.h:59
std::string escape(const std::string &str)
Escape a string for JSON output.
Definition json.h:30