载入中...
搜索中...
未找到
Json.cpp
浏览该文件的文档.
1#include "common/Json.h"
3
4#include "common/Value.h"
5
6#include <charconv>
7#include <cmath>
8#include <cstdlib>
9#include <limits>
10#include <locale>
11#include <sstream>
12#include <string_view>
13#include <type_traits>
14#include <utility>
15
16namespace eve::json {
17
18// ---------------------------------------------------------------------------
19// Node
20// ---------------------------------------------------------------------------
21
22struct Node {
23 enum class Kind { Null, Bool, Number, String, Object, Array };
24
26 bool boolVal = false;
27 double numberVal = 0.0;
29 bool integerLiteral = false;
31 bool hasInt64 = false;
32 std::int64_t intVal = 0;
33 std::string stringVal;
34 std::vector<std::string> memberNames; // object keys, document order
35 std::vector<Node> memberValues; // object values, parallel to memberNames
36 std::vector<Node> elements; // array
37};
38
39namespace {
40
45class Parser {
46public:
47 explicit Parser(const std::string& text) : s_(text) {}
48
49 bool parse(Node& out, std::string* error) {
50 skipWs();
51 if (!parseValue(out)) {
52 if (error) *error = "invalid JSON near offset " + std::to_string(pos_);
53 return false;
54 }
55 skipWs();
56 if (pos_ != s_.size()) {
57 if (error) *error = "trailing data at offset " + std::to_string(pos_);
58 return false;
59 }
60 return true;
61 }
62
63private:
64 static constexpr size_t kMaxDepth = 256;
65 const std::string& s_;
66 size_t pos_ = 0;
67
68 void skipWs() {
69 while (pos_ < s_.size() && (s_[pos_] == ' ' || s_[pos_] == '\t' || s_[pos_] == '\n' || s_[pos_] == '\r'))
70 ++pos_;
71 }
72
73 bool peek(char c) const { return pos_ < s_.size() && s_[pos_] == c; }
74
75 bool parseValue(Node& out, size_t depth = 0) {
76 if (depth > kMaxDepth) return false;
77 if (pos_ >= s_.size()) return false;
78 switch (s_[pos_]) {
79 case '{': return parseObject(out, depth);
80 case '[': return parseArray(out, depth);
81 case '"':
82 if (!parseString(out.stringVal)) return false;
84 return true;
85 case 't': return parseLiteral("true", out, true);
86 case 'f': return parseLiteral("false", out, false);
87 case 'n': return parseNull(out);
88 default: return parseNumber(out);
89 }
90 }
91
92 bool parseLiteral(const char* lit, Node& out, bool value) {
93 const size_t n = std::char_traits<char>::length(lit);
94 if (s_.compare(pos_, n, lit) != 0) return false;
95 pos_ += n;
96 out.kind = Node::Kind::Bool;
97 out.boolVal = value;
98 return true;
99 }
100
101 bool parseNull(Node& out) {
102 if (s_.compare(pos_, 4, "null") != 0) return false;
103 pos_ += 4;
104 out.kind = Node::Kind::Null;
105 return true;
106 }
107
108 bool parseNumber(Node& out) {
109 const size_t start = pos_;
110 if (pos_ < s_.size() && s_[pos_] == '-') ++pos_;
111 if (pos_ >= s_.size()) return false;
112 if (s_[pos_] == '0') {
113 ++pos_;
114 // RFC 8259 forbids leading zeroes in the integer component.
115 if (pos_ < s_.size() && s_[pos_] >= '0' && s_[pos_] <= '9') return false;
116 } else if (s_[pos_] >= '1' && s_[pos_] <= '9') {
117 while (pos_ < s_.size() && s_[pos_] >= '0' && s_[pos_] <= '9') ++pos_;
118 } else {
119 return false;
120 }
121 bool integral = true;
122 if (pos_ < s_.size() && s_[pos_] == '.') {
123 integral = false;
124 ++pos_;
125 const size_t fractionStart = pos_;
126 while (pos_ < s_.size() && s_[pos_] >= '0' && s_[pos_] <= '9') ++pos_;
127 if (pos_ == fractionStart) return false;
128 }
129 if (pos_ < s_.size() && (s_[pos_] == 'e' || s_[pos_] == 'E')) {
130 integral = false;
131 ++pos_;
132 if (pos_ < s_.size() && (s_[pos_] == '-' || s_[pos_] == '+')) ++pos_;
133 const size_t exponentStart = pos_;
134 while (pos_ < s_.size() && s_[pos_] >= '0' && s_[pos_] <= '9') ++pos_;
135 if (pos_ == exponentStart) return false;
136 }
137 const std::string num = s_.substr(start, pos_ - start);
138 try {
139 size_t used = 0;
140 out.numberVal = std::stod(num, &used);
141 if (used != num.size() || !std::isfinite(out.numberVal)) return false;
142 out.integerLiteral = integral;
143 if (integral) {
144 try {
145 out.intVal = std::stoll(num);
146 out.hasInt64 = true;
147 } catch (...) {
148 // Keep the finite double for compatibility with the
149 // lenient read-only JSON facade. Owning Value conversion
150 // rejects this integer because it cannot preserve Int64.
151 out.hasInt64 = false;
152 }
153 }
154 } catch (...) {
155 // Out of long long range but still a valid double (or vice versa):
156 // keep whichever conversion succeeded.
157 if (!integral) return false;
158 try {
159 out.numberVal = std::stod(num);
160 } catch (...) {
161 return false;
162 }
163 }
164 out.kind = Node::Kind::Number;
165 return true;
166 }
167
168 bool parseString(std::string& out) {
169 if (!peek('"')) return false;
170 ++pos_;
171 out.clear();
172 while (pos_ < s_.size()) {
173 const char c = s_[pos_++];
174 if (c == '"') return true;
175 if (c != '\\') {
176 if (static_cast<unsigned char>(c) < 0x20) return false;
177 out += c;
178 continue;
179 }
180 if (pos_ >= s_.size()) return false;
181 const char esc = s_[pos_++];
182 switch (esc) {
183 case '"': out += '"'; break;
184 case '\\': out += '\\'; break;
185 case '/': out += '/'; break;
186 case 'b': out += '\b'; break;
187 case 'f': out += '\f'; break;
188 case 'n': out += '\n'; break;
189 case 'r': out += '\r'; break;
190 case 't': out += '\t'; break;
191 case 'u': {
192 if (pos_ + 4 > s_.size()) return false;
193 const char hex[5] = {s_[pos_], s_[pos_ + 1], s_[pos_ + 2], s_[pos_ + 3], '\0'};
194 pos_ += 4;
195 char* end = nullptr;
196 const unsigned cp = static_cast<unsigned>(std::strtoul(hex, &end, 16));
197 if (!end || *end != '\0') return false;
198 // A high surrogate followed by "\uXXXX" forms one code point.
199 if (cp >= 0xD800 && cp <= 0xDBFF && pos_ + 6 <= s_.size() && s_[pos_] == '\\' &&
200 s_[pos_ + 1] == 'u') {
201 const char lohex[5] = {s_[pos_ + 2], s_[pos_ + 3], s_[pos_ + 4], s_[pos_ + 5], '\0'};
202 pos_ += 6;
203 char* loEnd = nullptr;
204 const unsigned lo = static_cast<unsigned>(std::strtoul(lohex, &loEnd, 16));
205 if (loEnd && *loEnd == '\0' && lo >= 0xDC00 && lo <= 0xDFFF) {
206 appendUtf8(out, 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00));
207 } else {
208 return false;
209 }
210 } else if (cp >= 0xD800 && cp <= 0xDFFF) {
211 return false;
212 } else {
213 appendUtf8(out, cp);
214 }
215 break;
216 }
217 default: return false;
218 }
219 }
220 return false; // unterminated
221 }
222
223 static void appendUtf8(std::string& out, unsigned cp) {
224 if (cp < 0x80) {
225 out += static_cast<char>(cp);
226 } else if (cp < 0x800) {
227 out += static_cast<char>(0xC0 | (cp >> 6));
228 out += static_cast<char>(0x80 | (cp & 0x3F));
229 } else if (cp < 0x10000) {
230 out += static_cast<char>(0xE0 | (cp >> 12));
231 out += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
232 out += static_cast<char>(0x80 | (cp & 0x3F));
233 } else {
234 out += static_cast<char>(0xF0 | (cp >> 18));
235 out += static_cast<char>(0x80 | ((cp >> 12) & 0x3F));
236 out += static_cast<char>(0x80 | ((cp >> 6) & 0x3F));
237 out += static_cast<char>(0x80 | (cp & 0x3F));
238 }
239 }
240
241 bool parseObject(Node& out, size_t depth) {
242 ++pos_; // '{'
243 skipWs();
244 if (peek('}')) {
245 ++pos_;
246 out.kind = Node::Kind::Object;
247 return true;
248 }
249 while (true) {
250 skipWs();
251 std::string key;
252 if (!parseString(key)) return false;
253 for (const std::string& existing : out.memberNames)
254 if (existing == key) return false;
255 skipWs();
256 if (!peek(':')) return false;
257 ++pos_;
258 skipWs();
259 Node val;
260 if (!parseValue(val, depth + 1)) return false;
261 out.memberNames.emplace_back(std::move(key));
262 out.memberValues.emplace_back(std::move(val));
263 skipWs();
264 if (peek('}')) {
265 ++pos_;
266 out.kind = Node::Kind::Object;
267 return true;
268 }
269 if (!peek(',')) return false;
270 ++pos_;
271 }
272 }
273
274 bool parseArray(Node& out, size_t depth) {
275 ++pos_; // '['
276 skipWs();
277 if (peek(']')) {
278 ++pos_;
279 out.kind = Node::Kind::Array;
280 return true;
281 }
282 while (true) {
283 skipWs();
284 Node val;
285 if (!parseValue(val, depth + 1)) return false;
286 out.elements.push_back(std::move(val));
287 skipWs();
288 if (peek(']')) {
289 ++pos_;
290 out.kind = Node::Kind::Array;
291 return true;
292 }
293 if (!peek(',')) return false;
294 ++pos_;
295 }
296 }
297};
298
300std::string numberToString(const Node& n) {
301 if (n.hasInt64) return std::to_string(n.intVal);
302 std::ostringstream os;
303 os << n.numberVal;
304 return os.str();
305}
306
307bool stringToDouble(const std::string& s, double& out) {
308 try {
309 size_t used = 0;
310 const double v = std::stod(s, &used);
311 while (used < s.size() && (s[used] == ' ' || s[used] == '\t')) ++used;
312 if (used != s.size()) return false;
313 out = v;
314 return true;
315 } catch (...) {
316 return false;
317 }
318}
319
320} // namespace
321
322// ---------------------------------------------------------------------------
323// Value
324// ---------------------------------------------------------------------------
325
326bool Value::isNull() const { return !node_ || node_->kind == Node::Kind::Null; }
327bool Value::isBool() const { return node_ && node_->kind == Node::Kind::Bool; }
328bool Value::isNumber() const { return node_ && node_->kind == Node::Kind::Number; }
329bool Value::isInt64() const { return node_ && node_->kind == Node::Kind::Number && node_->hasInt64; }
330bool Value::isIntegerLiteral() const { return node_ && node_->kind == Node::Kind::Number && node_->integerLiteral; }
331bool Value::isString() const { return node_ && node_->kind == Node::Kind::String; }
332bool Value::isObject() const { return node_ && node_->kind == Node::Kind::Object; }
333bool Value::isArray() const { return node_ && node_->kind == Node::Kind::Array; }
334
335bool Value::has(const char* key) const { return static_cast<bool>(get(key)); }
336
337Value Value::get(const char* key) const {
338 if (!node_ || node_->kind != Node::Kind::Object || !key) return Value();
339 for (size_t i = 0; i < node_->memberNames.size(); ++i)
340 if (node_->memberNames[i] == key) return Value(&node_->memberValues[i]);
341 return Value();
342}
343
344std::vector<std::string> Value::keys() const {
345 std::vector<std::string> out;
346 if (!node_ || node_->kind != Node::Kind::Object) return out;
347 out.reserve(node_->memberNames.size());
348 for (const auto& name : node_->memberNames) out.push_back(name);
349 return out;
350}
351
352size_t Value::size() const {
353 if (!node_) return 0;
354 if (node_->kind == Node::Kind::Array) return node_->elements.size();
355 if (node_->kind == Node::Kind::Object) return node_->memberValues.size();
356 return 0;
357}
358
359Value Value::at(size_t index) const {
360 if (!node_ || node_->kind != Node::Kind::Array || index >= node_->elements.size())
361 return Value();
362 return Value(&node_->elements[index]);
363}
364
365bool Value::asBool(bool fallback) const {
366 if (!node_) return fallback;
367 switch (node_->kind) {
368 case Node::Kind::Bool: return node_->boolVal;
369 case Node::Kind::Number: return node_->numberVal != 0.0;
371 if (node_->stringVal == "true") return true;
372 if (node_->stringVal == "false") return false;
373 return fallback;
374 default: return fallback;
375 }
376}
377
378double Value::asDouble(double fallback) const {
379 if (!node_) return fallback;
380 switch (node_->kind) {
381 case Node::Kind::Number: return node_->numberVal;
382 case Node::Kind::Bool: return node_->boolVal ? 1.0 : 0.0;
383 case Node::Kind::String: {
384 double v = 0.0;
385 return stringToDouble(node_->stringVal, v) ? v : fallback;
386 }
387 default: return fallback;
388 }
389}
390
391std::int64_t Value::asInt64(std::int64_t fallback) const { return isInt64() ? node_->intVal : fallback; }
392
393int Value::asInt(int fallback) const {
394 if (!node_) return fallback;
395 if (node_->kind == Node::Kind::Number && node_->hasInt64) {
396 if (node_->intVal < std::numeric_limits<int>::min() ||
397 node_->intVal > std::numeric_limits<int>::max())
398 return fallback;
399 return static_cast<int>(node_->intVal);
400 }
401 const double d = asDouble(static_cast<double>(fallback));
402 if (!std::isfinite(d) || d < static_cast<double>(std::numeric_limits<int>::min()) ||
403 d > static_cast<double>(std::numeric_limits<int>::max()))
404 return fallback;
405 return static_cast<int>(d);
406}
407
408float Value::asFloat(float fallback) const {
409 return static_cast<float>(asDouble(static_cast<double>(fallback)));
410}
411
412std::string Value::asString(const std::string& fallback) const {
413 if (!node_) return fallback;
414 switch (node_->kind) {
415 case Node::Kind::String: return node_->stringVal;
416 case Node::Kind::Number: return numberToString(*node_);
417 case Node::Kind::Bool: return node_->boolVal ? "true" : "false";
418 default: return fallback;
419 }
420}
421
422bool Value::getBool(const char* key, bool fallback) const { return get(key).asBool(fallback); }
423int Value::getInt(const char* key, int fallback) const { return get(key).asInt(fallback); }
424float Value::getFloat(const char* key, float fallback) const { return get(key).asFloat(fallback); }
425double Value::getDouble(const char* key, double fallback) const {
426 return get(key).asDouble(fallback);
427}
428std::string Value::getString(const char* key, const std::string& fallback) const {
429 return get(key).asString(fallback);
430}
431
432std::vector<std::string> Value::toStringArray() const {
433 std::vector<std::string> out;
434 if (!node_ || node_->kind != Node::Kind::Array) return out;
435 out.reserve(node_->elements.size());
436 for (const auto& e : node_->elements) out.push_back(Value(&e).asString());
437 return out;
438}
439
440std::vector<std::string> Value::getStringArray(const char* key) const {
441 return get(key).toStringArray();
442}
443
444std::vector<int> Value::getIntArray(const char* key) const {
445 std::vector<int> out;
446 const Value arr = get(key);
447 const size_t n = arr.isArray() ? arr.size() : 0;
448 out.reserve(n);
449 for (size_t i = 0; i < n; ++i) out.push_back(arr.at(i).asInt(0));
450 return out;
451}
452
453std::vector<float> Value::getFloatArray(const char* key) const {
454 std::vector<float> out;
455 const Value arr = get(key);
456 const size_t n = arr.isArray() ? arr.size() : 0;
457 out.reserve(n);
458 for (size_t i = 0; i < n; ++i) out.push_back(arr.at(i).asFloat(0.f));
459 return out;
460}
461
462std::unordered_map<std::string, std::string> Value::getStringMap(const char* key) const {
463 std::unordered_map<std::string, std::string> out;
464 const Value obj = get(key);
465 if (!obj.isObject()) return out;
466 for (const auto& name : obj.keys()) out[name] = obj.getString(name.c_str());
467 return out;
468}
469
470std::unordered_map<std::string, int> Value::getIntMap(const char* key) const {
471 std::unordered_map<std::string, int> out;
472 const Value obj = get(key);
473 if (!obj.isObject()) return out;
474 for (const auto& name : obj.keys()) out[name] = obj.getInt(name.c_str(), 0);
475 return out;
476}
477
478namespace {
479
480void appendEscapedString(std::string_view value, std::string& output,
481 StringEscapeStyle style = StringEscapeStyle::Canonical) {
482 constexpr char hex[] = "0123456789abcdef";
483 output.push_back('"');
484 for (const unsigned char byte : value) {
485 switch (byte) {
486 case '"': output += "\\\""; break;
487 case '\\': output += "\\\\"; break;
488 case '\b': output += style == StringEscapeStyle::Canonical ? "\\b" : "\\u0008"; break;
489 case '\f': output += style == StringEscapeStyle::Canonical ? "\\f" : "\\u000c"; break;
490 case '\n': output += "\\n"; break;
491 case '\r': output += "\\r"; break;
492 case '\t': output += "\\t"; break;
493 default:
494 if (byte < 0x20) {
495 output += "\\u00";
496 output.push_back(hex[byte >> 4]);
497 output.push_back(hex[byte & 0x0f]);
498 } else {
499 output.push_back(static_cast<char>(byte));
500 }
501 break;
502 }
503 }
504 output.push_back('"');
505}
506
507bool appendCanonicalJson(const eve::Value& value, std::string& output) {
508 return std::visit(
509 [&output](const auto& current) -> bool {
510 using T = std::decay_t<decltype(current)>;
511 if constexpr (std::is_same_v<T, std::monostate>) {
512 output += "null";
513 return true;
514 } else if constexpr (std::is_same_v<T, bool>) {
515 output += current ? "true" : "false";
516 return true;
517 } else if constexpr (std::is_same_v<T, std::int64_t>) {
518 char buffer[32];
519 const auto [end, error] = std::to_chars(buffer, buffer + sizeof(buffer), current);
520 if (error != std::errc()) return false;
521 output.append(buffer, end);
522 return true;
523 } else if constexpr (std::is_same_v<T, double>) {
524 if (!std::isfinite(current)) return false;
525#if defined(_MSC_VER)
526 char buffer[64];
527 const auto [end, error] =
528 std::to_chars(buffer, buffer + sizeof(buffer), current, std::chars_format::general,
529 std::numeric_limits<double>::max_digits10);
530 if (error != std::errc()) return false;
531 const std::string_view number(buffer, static_cast<std::size_t>(end - buffer));
532 output.append(buffer, end);
533 if (number.find_first_of(".eE") == std::string_view::npos) output += ".0";
534#else
535 std::ostringstream stream;
536 stream.imbue(std::locale::classic());
537 stream.precision(std::numeric_limits<double>::max_digits10);
538 stream << current;
539 const std::string number = stream.str();
540 output += number;
541 if (number.find_first_of(".eE") == std::string_view::npos) output += ".0";
542#endif
543 return true;
544 } else if constexpr (std::is_same_v<T, std::string>) {
545 appendEscapedString(current, output);
546 return true;
547 } else if constexpr (std::is_same_v<T, eve::Value::Array>) {
548 output.push_back('[');
549 bool first = true;
550 for (const eve::Value& element : current) {
551 if (!first) output.push_back(',');
552 first = false;
553 if (!appendCanonicalJson(element, output)) return false;
554 }
555 output.push_back(']');
556 return true;
557 } else if constexpr (std::is_same_v<T, eve::Value::Object>) {
558 output.push_back('{');
559 bool first = true;
560 for (const auto& [key, element] : current) {
561 if (!first) output.push_back(',');
562 first = false;
563 appendEscapedString(key, output);
564 output.push_back(':');
565 if (!appendCanonicalJson(element, output)) return false;
566 }
567 output.push_back('}');
568 return true;
569 }
570 },
571 value.storage());
572}
573
574} // namespace
575
576std::string quoteString(std::string_view value, StringEscapeStyle style) {
577 std::string output;
578 output.reserve(value.size() + 2);
579 appendEscapedString(value, output, style);
580 return output;
581}
582
583Result<UnsignedIntegerScan> scanUnsignedInteger(std::string_view text, UnsignedIntegerSyntax syntax) {
584 if (!text.empty()) {
585 if (syntax == UnsignedIntegerSyntax::LegacyStoull) {
586 try {
587 std::size_t consumed = 0;
588 const auto value = std::stoull(std::string(text), &consumed);
590 } catch (...) {
591 // Compatibility with the existing persistence readers' exception boundary.
592 }
593 } else {
594 std::uint64_t value = 0;
595 const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value);
596 if (error == std::errc{})
598 {value, static_cast<std::size_t>(end - text.data()), text.size()});
599 }
600 }
602 Diagnostic::error(DiagnosticCode::ParseError, "unsigned integer has no valid in-range prefix"));
603}
604
606 std::string output;
607 if (!appendCanonicalJson(value, output)) {
609 DiagnosticCode::SerializationError, "Value contains a non-finite Double; JSON requires finite numbers"));
610 }
611 return Result<std::string>::success(std::move(output));
612}
613
614// ---------------------------------------------------------------------------
615// Document
616// ---------------------------------------------------------------------------
617
618Document::Document() = default;
619Document::~Document() = default;
620Document::Document(Document&&) noexcept = default;
621Document& Document::operator=(Document&&) noexcept = default;
622
623Document Document::parse(const std::string& text, std::string* error) {
624 Document doc;
625 auto node = std::make_unique<Node>();
626 Parser parser(text);
627 if (!parser.parse(*node, error)) return doc;
628 doc.root_ = std::move(node);
629 return doc;
630}
631
632} // namespace eve::json
Duration start
std::string output
const std::string & s
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
glm::vec3 n
Definition Grass.cpp:63
float v
std::int32_t c
std::int32_t first
uint64_t used
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
std::string error
Definition Package.cpp:174
const eve::script::ScriptFunctionSyntax * syntax
Object::Ptr value
float d
const RoadNode * node
double number
std::string text
ScriptStructure out
size_t end
double current
std::string element
Share string attribute map operations through the Windows Foundation DLL.
std::uint32_t depth
uint32_t index
Owning, renderer-independent dynamic values.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
Definition Value.cpp:207
double asDouble() const
Return the Double payload; the caller must have checked the kind.
Definition Value.cpp:195
std::int64_t asInt() const
Return the Int64 payload; the caller must have checked the kind.
Definition Value.cpp:189
bool asBool() const
Return the boolean payload; the caller must have checked the kind.
Definition Value.cpp:201
EVENGINE_API_FOUNDATION public API.
Definition Json.h:137
~Document()
Document.
EVENGINE_API_FOUNDATION public API.
Definition Json.h:39
float asFloat(float fallback=0.f) const
As float.
Definition Json.cpp:408
bool getBool(const char *key, bool fallback=false) const
Returns the bool.
Definition Json.cpp:422
bool isObject() const
True when object.
Definition Json.cpp:332
bool has(const char *key) const
True when active.
Definition Json.cpp:335
double asDouble(double fallback=0.0) const
As double.
Definition Json.cpp:378
bool isIntegerLiteral() const
True when the JSON number had no fraction or exponent.
Definition Json.cpp:330
Value get(const char *key) const
Returns the value.
Definition Json.cpp:337
size_t size() const
Returns the size of .
Definition Json.cpp:352
bool isArray() const
True when array.
Definition Json.cpp:333
Value at(size_t index) const
At.
Definition Json.cpp:359
bool isString() const
True when string.
Definition Json.cpp:331
float getFloat(const char *key, float fallback=0.f) const
Returns the float.
Definition Json.cpp:424
int getInt(const char *key, int fallback=0) const
Returns the int.
Definition Json.cpp:423
Value()=default
Value.
bool isNull() const
True when null.
Definition Json.cpp:326
bool isNumber() const
True when number.
Definition Json.cpp:328
std::string getString(const char *key, const std::string &fallback={}) const
Returns the string.
Definition Json.cpp:428
int asInt(int fallback=0) const
As int.
Definition Json.cpp:393
std::vector< std::string > getStringArray(const char *key) const
Returns the string array.
Definition Json.cpp:440
bool asBool(bool fallback=false) const
As bool.
Definition Json.cpp:365
double getDouble(const char *key, double fallback=0.0) const
Returns the double.
Definition Json.cpp:425
bool isBool() const
True when bool.
Definition Json.cpp:327
std::vector< std::string > keys() const
Keys.
Definition Json.cpp:344
std::string asString(const std::string &fallback={}) const
As string.
Definition Json.cpp:412
bool isInt64() const
True when the JSON number was an integer literal that fits Int64.
Definition Json.cpp:329
std::unordered_map< std::string, int > getIntMap(const char *key) const
Definition Json.cpp:470
std::int64_t asInt64(std::int64_t fallback=0) const
Read an exact Int64 literal, or return the caller-supplied default for other numbers.
Definition Json.cpp:391
std::unordered_map< std::string, std::string > getStringMap(const char *key) const
Definition Json.cpp:462
std::vector< int > getIntArray(const char *key) const
Returns the int array.
Definition Json.cpp:444
std::vector< std::string > toStringArray() const
To string array.
Definition Json.cpp:432
std::vector< float > getFloatArray(const char *key) const
Returns the float array.
Definition Json.cpp:453
std::string quoteString(std::string_view value, StringEscapeStyle style)
Encode an owning JSON string using the canonical serializer's escaping implementation.
Definition Json.cpp:576
Result< std::string > stringify(const eve::Value &value)
Serialize the canonical owning Value as deterministic compact JSON.
Definition Json.cpp:605
Result< UnsignedIntegerScan > scanUnsignedInteger(std::string_view text, UnsignedIntegerSyntax syntax)
Scan an unsigned integer prefix without mutating caller state.
Definition Json.cpp:583
std::int64_t intVal
Definition Json.cpp:32
std::string stringVal
Definition Json.cpp:33
std::vector< Node > elements
Definition Json.cpp:36
double numberVal
Definition Json.cpp:27
std::vector< std::string > memberNames
Definition Json.cpp:34
bool hasInt64
Definition Json.cpp:31
std::vector< Node > memberValues
Definition Json.cpp:35
bool integerLiteral
Definition Json.cpp:29
bool consumed
Definition Graphics.cpp:201