-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalue_io.hpp
More file actions
315 lines (290 loc) · 7.73 KB
/
value_io.hpp
File metadata and controls
315 lines (290 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) 2025-2026 Voxgig Ltd. MIT LICENSE.
//
// Voxgig Struct — JSON I/O bridge.
//
// Hand-written recursive-descent JSON parser (no third-party deps).
// Runtime container is Value; this file converts to/from JSON text.
#ifndef VOXGIG_STRUCT_VALUE_IO_HPP
#define VOXGIG_STRUCT_VALUE_IO_HPP
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
#include "value.hpp"
#include "voxgig_struct.hpp" // jsonify() for the serializer side
namespace voxgig {
namespace structlib {
// ===========================================================================
// Parser — same algorithm as c/src/value_io.c jp_*. Accepts the standard
// JSON grammar including \\uXXXX escapes and surrogate pairs. On any
// malformed input returns Value::undef() (no exceptions thrown).
// ===========================================================================
namespace _jp {
struct State {
const std::string& src;
size_t pos = 0;
};
inline void skip_ws(State& p) {
while (p.pos < p.src.size()) {
char c = p.src[p.pos];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
p.pos++;
else
break;
}
}
inline int peek(const State& p) {
return p.pos < p.src.size() ? static_cast<unsigned char>(p.src[p.pos]) : -1;
}
inline bool match(State& p, const char* lit) {
size_t n = std::strlen(lit);
if (p.pos + n > p.src.size())
return false;
if (p.src.compare(p.pos, n, lit) != 0)
return false;
p.pos += n;
return true;
}
inline int hex(int c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
return -1;
}
inline void put_codepoint(std::string& dst, uint32_t cp) {
if (cp < 0x80) {
dst.push_back(static_cast<char>(cp));
} else if (cp < 0x800) {
dst.push_back(static_cast<char>(0xC0 | (cp >> 6)));
dst.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
} else if (cp < 0x10000) {
dst.push_back(static_cast<char>(0xE0 | (cp >> 12)));
dst.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
dst.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
} else {
dst.push_back(static_cast<char>(0xF0 | (cp >> 18)));
dst.push_back(static_cast<char>(0x80 | ((cp >> 12) & 0x3F)));
dst.push_back(static_cast<char>(0x80 | ((cp >> 6) & 0x3F)));
dst.push_back(static_cast<char>(0x80 | (cp & 0x3F)));
}
}
inline bool parse_string(State& p, std::string& out) {
if (peek(p) != '"')
return false;
p.pos++;
while (p.pos < p.src.size()) {
char c = p.src[p.pos++];
if (c == '"')
return true;
if (c == '\\') {
if (p.pos >= p.src.size())
return false;
char e = p.src[p.pos++];
switch (e) {
case '"':
out.push_back('"');
break;
case '\\':
out.push_back('\\');
break;
case '/':
out.push_back('/');
break;
case 'b':
out.push_back('\b');
break;
case 'f':
out.push_back('\f');
break;
case 'n':
out.push_back('\n');
break;
case 'r':
out.push_back('\r');
break;
case 't':
out.push_back('\t');
break;
case 'u': {
if (p.pos + 4 > p.src.size())
return false;
int h1 = hex(static_cast<unsigned char>(p.src[p.pos]));
int h2 = hex(static_cast<unsigned char>(p.src[p.pos + 1]));
int h3 = hex(static_cast<unsigned char>(p.src[p.pos + 2]));
int h4 = hex(static_cast<unsigned char>(p.src[p.pos + 3]));
if (h1 < 0 || h2 < 0 || h3 < 0 || h4 < 0)
return false;
uint32_t cp = static_cast<uint32_t>((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
p.pos += 4;
if (cp >= 0xD800 && cp <= 0xDBFF && p.pos + 6 <= p.src.size() && p.src[p.pos] == '\\' &&
p.src[p.pos + 1] == 'u') {
int g1 = hex(static_cast<unsigned char>(p.src[p.pos + 2]));
int g2 = hex(static_cast<unsigned char>(p.src[p.pos + 3]));
int g3 = hex(static_cast<unsigned char>(p.src[p.pos + 4]));
int g4 = hex(static_cast<unsigned char>(p.src[p.pos + 5]));
if (g1 >= 0 && g2 >= 0 && g3 >= 0 && g4 >= 0) {
uint32_t lo = static_cast<uint32_t>((g1 << 12) | (g2 << 8) | (g3 << 4) | g4);
if (lo >= 0xDC00 && lo <= 0xDFFF) {
p.pos += 6;
cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
}
}
}
put_codepoint(out, cp);
break;
}
default:
out.push_back(e);
break;
}
} else {
out.push_back(c);
}
}
return false;
}
inline Value parse_value(State& p);
inline Value parse_number(State& p) {
size_t start = p.pos;
if (peek(p) == '-')
p.pos++;
bool has_dot = false, has_exp = false;
while (p.pos < p.src.size()) {
char c = p.src[p.pos];
if (c >= '0' && c <= '9') {
p.pos++;
} else if (c == '.' && !has_dot && !has_exp) {
has_dot = true;
p.pos++;
} else if ((c == 'e' || c == 'E') && !has_exp) {
has_exp = true;
p.pos++;
if (p.pos < p.src.size() && (p.src[p.pos] == '+' || p.src[p.pos] == '-'))
p.pos++;
} else {
break;
}
}
if (p.pos == start)
return Value();
std::string tok = p.src.substr(start, p.pos - start);
if (!has_dot && !has_exp) {
return Value(static_cast<int64_t>(std::strtoll(tok.c_str(), nullptr, 10)));
}
return Value(std::strtod(tok.c_str(), nullptr));
}
inline Value parse_array(State& p) {
if (peek(p) != '[')
return Value();
p.pos++;
auto out = std::make_shared<List>();
skip_ws(p);
if (peek(p) == ']') {
p.pos++;
return Value(std::move(out));
}
while (true) {
skip_ws(p);
out->push_back(parse_value(p));
skip_ws(p);
int c = peek(p);
if (c == ',') {
p.pos++;
continue;
}
if (c == ']') {
p.pos++;
break;
}
break;
}
return Value(std::move(out));
}
inline Value parse_object(State& p) {
if (peek(p) != '{')
return Value();
p.pos++;
auto out = std::shared_ptr<Map>(new Map());
skip_ws(p);
if (peek(p) == '}') {
p.pos++;
return Value(std::move(out));
}
while (true) {
skip_ws(p);
std::string key;
if (!parse_string(p, key))
break;
skip_ws(p);
if (peek(p) != ':')
break;
p.pos++;
skip_ws(p);
Value val = parse_value(p);
out->set(key, val);
skip_ws(p);
int c = peek(p);
if (c == ',') {
p.pos++;
continue;
}
if (c == '}') {
p.pos++;
break;
}
break;
}
return Value(std::move(out));
}
inline Value parse_value(State& p) {
skip_ws(p);
int c = peek(p);
if (c < 0)
return Value();
if (c == 'n' && match(p, "null"))
return Value(nullptr);
if (c == 't' && match(p, "true"))
return Value(true);
if (c == 'f' && match(p, "false"))
return Value(false);
if (c == '"') {
std::string s;
parse_string(p, s);
return Value(std::move(s));
}
if (c == '-' || (c >= '0' && c <= '9'))
return parse_number(p);
if (c == '[')
return parse_array(p);
if (c == '{')
return parse_object(p);
p.pos++;
return Value();
}
} // namespace _jp
// ---- JSON text I/O ----
inline Value parse_json(const std::string& text) {
_jp::State p{text, 0};
return _jp::parse_value(p);
}
inline Value parse_json_file(const std::string& path) {
std::ifstream f(path);
if (!f)
throw std::runtime_error("Cannot open " + path);
std::stringstream ss;
ss << f.rdbuf();
return parse_json(ss.str());
}
inline std::string dump_json(const Value& v, int indent = -1) {
return jsonify(v, indent < 0 ? 0 : indent);
}
} // namespace structlib
} // namespace voxgig
#endif // VOXGIG_STRUCT_VALUE_IO_HPP