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