47 explicit Parser(
const std::string&
text) : s_(
text) {}
51 if (!parseValue(
out)) {
52 if (
error) *
error =
"invalid JSON near offset " + std::to_string(pos_);
56 if (pos_ != s_.size()) {
57 if (
error) *
error =
"trailing data at offset " + std::to_string(pos_);
64 static constexpr size_t kMaxDepth = 256;
65 const std::string& s_;
69 while (pos_ < s_.size() && (s_[pos_] ==
' ' || s_[pos_] ==
'\t' || s_[pos_] ==
'\n' || s_[pos_] ==
'\r'))
73 bool peek(
char c)
const {
return pos_ < s_.size() && s_[pos_] ==
c; }
76 if (
depth > kMaxDepth)
return false;
77 if (pos_ >= s_.size())
return false;
79 case '{':
return parseObject(
out,
depth);
80 case '[':
return parseArray(
out,
depth);
82 if (!parseString(
out.stringVal))
return false;
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);
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;
102 if (s_.compare(pos_, 4,
"null") != 0)
return false;
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') {
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_;
121 bool integral =
true;
122 if (pos_ < s_.size() && s_[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;
129 if (pos_ < s_.size() && (s_[pos_] ==
'e' || s_[pos_] ==
'E')) {
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;
137 const std::string num = s_.substr(
start, pos_ -
start);
140 out.numberVal = std::stod(num, &
used);
141 if (
used != num.size() || !std::isfinite(
out.numberVal))
return false;
142 out.integerLiteral = integral;
145 out.intVal = std::stoll(num);
151 out.hasInt64 =
false;
157 if (!integral)
return false;
159 out.numberVal = std::stod(num);
168 bool parseString(std::string&
out) {
169 if (!peek(
'"'))
return false;
172 while (pos_ < s_.size()) {
173 const char c = s_[pos_++];
174 if (
c ==
'"')
return true;
176 if (
static_cast<unsigned char>(
c) < 0x20)
return false;
180 if (pos_ >= s_.size())
return false;
181 const char esc = s_[pos_++];
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;
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'};
196 const unsigned cp =
static_cast<unsigned>(std::strtoul(hex, &
end, 16));
197 if (!
end || *
end !=
'\0')
return false;
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'};
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));
210 }
else if (cp >= 0xD800 && cp <= 0xDFFF) {
217 default:
return false;
223 static void appendUtf8(std::string&
out,
unsigned cp) {
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));
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));
252 if (!parseString(
key))
return false;
253 for (
const std::string& existing :
out.memberNames)
254 if (existing ==
key)
return false;
256 if (!peek(
':'))
return false;
260 if (!parseValue(val,
depth + 1))
return false;
262 out.memberValues.emplace_back(std::move(val));
269 if (!peek(
','))
return false;
285 if (!parseValue(val,
depth + 1))
return false;
293 if (!peek(
','))
return false;
300std::string numberToString(
const Node&
n) {
301 if (
n.hasInt64)
return std::to_string(
n.intVal);
302 std::ostringstream os;
307bool stringToDouble(
const std::string&
s,
double&
out) {
310 const double v = std::stod(
s, &
used);
312 if (
used !=
s.size())
return false;
339 for (
size_t i = 0; i < node_->
memberNames.size(); ++i)
345 std::vector<std::string>
out;
353 if (!node_)
return 0;
366 if (!node_)
return fallback;
367 switch (node_->
kind) {
371 if (node_->
stringVal ==
"true")
return true;
372 if (node_->
stringVal ==
"false")
return false;
374 default:
return fallback;
379 if (!node_)
return fallback;
380 switch (node_->
kind) {
385 return stringToDouble(node_->
stringVal,
v) ?
v : fallback;
387 default:
return fallback;
394 if (!node_)
return fallback;
396 if (node_->
intVal < std::numeric_limits<int>::min() ||
397 node_->
intVal > std::numeric_limits<int>::max())
399 return static_cast<int>(node_->
intVal);
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()))
405 return static_cast<int>(
d);
409 return static_cast<float>(
asDouble(
static_cast<double>(fallback)));
413 if (!node_)
return fallback;
414 switch (node_->
kind) {
418 default:
return fallback;
433 std::vector<std::string>
out;
445 std::vector<int>
out;
449 for (
size_t i = 0; i <
n; ++i)
out.push_back(arr.
at(i).
asInt(0));
454 std::vector<float>
out;
458 for (
size_t i = 0; i <
n; ++i)
out.push_back(arr.
at(i).
asFloat(0.f));
463 std::unordered_map<std::string, std::string>
out;
471 std::unordered_map<std::string, int>
out;
480void appendEscapedString(std::string_view
value, std::string&
output,
481 StringEscapeStyle style = StringEscapeStyle::Canonical) {
482 constexpr char hex[] =
"0123456789abcdef";
484 for (
const unsigned char byte :
value) {
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;
496 output.push_back(hex[
byte >> 4]);
497 output.push_back(hex[
byte & 0x0f]);
499 output.push_back(
static_cast<char>(
byte));
510 using T = std::decay_t<
decltype(
current)>;
511 if constexpr (std::is_same_v<T, std::monostate>) {
514 }
else if constexpr (std::is_same_v<T, bool>) {
517 }
else if constexpr (std::is_same_v<T, std::int64_t>) {
520 if (
error != std::errc())
return false;
523 }
else if constexpr (std::is_same_v<T, double>) {
524 if (!std::isfinite(
current))
return false;
529 std::numeric_limits<double>::max_digits10);
530 if (
error != std::errc())
return false;
533 if (
number.find_first_of(
".eE") == std::string_view::npos)
output +=
".0";
535 std::ostringstream stream;
536 stream.imbue(std::locale::classic());
537 stream.precision(std::numeric_limits<double>::max_digits10);
539 const std::string
number = stream.str();
541 if (
number.find_first_of(
".eE") == std::string_view::npos)
output +=
".0";
544 }
else if constexpr (std::is_same_v<T, std::string>) {
547 }
else if constexpr (std::is_same_v<T, eve::Value::Array>) {
557 }
else if constexpr (std::is_same_v<T, eve::Value::Object>) {
585 if (
syntax == UnsignedIntegerSyntax::LegacyStoull) {
594 std::uint64_t
value = 0;
596 if (
error == std::errc{})
621Document& Document::operator=(Document&&) noexcept = default;
625 auto node = std::make_unique<Node>();
627 if (!parser.parse(*
node,
error))
return doc;
628 doc.root_ = std::move(
node);
std::unique_ptr< gpgpu::GpuBuffer > buffer
const eve::script::ScriptFunctionSyntax * syntax
Share string attribute map operations through the Windows Foundation DLL.
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.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
The canonical owning dynamic value used by data-facing protocols.
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
double asDouble() const
Return the Double payload; the caller must have checked the kind.
std::int64_t asInt() const
Return the Int64 payload; the caller must have checked the kind.
bool asBool() const
Return the boolean payload; the caller must have checked the kind.
EVENGINE_API_FOUNDATION public API.
EVENGINE_API_FOUNDATION public API.
float asFloat(float fallback=0.f) const
As float.
bool getBool(const char *key, bool fallback=false) const
Returns the bool.
bool isObject() const
True when object.
bool has(const char *key) const
True when active.
double asDouble(double fallback=0.0) const
As double.
bool isIntegerLiteral() const
True when the JSON number had no fraction or exponent.
Value get(const char *key) const
Returns the value.
size_t size() const
Returns the size of .
bool isArray() const
True when array.
Value at(size_t index) const
At.
bool isString() const
True when string.
float getFloat(const char *key, float fallback=0.f) const
Returns the float.
int getInt(const char *key, int fallback=0) const
Returns the int.
bool isNull() const
True when null.
bool isNumber() const
True when number.
std::string getString(const char *key, const std::string &fallback={}) const
Returns the string.
int asInt(int fallback=0) const
As int.
std::vector< std::string > getStringArray(const char *key) const
Returns the string array.
bool asBool(bool fallback=false) const
As bool.
double getDouble(const char *key, double fallback=0.0) const
Returns the double.
bool isBool() const
True when bool.
std::vector< std::string > keys() const
Keys.
std::string asString(const std::string &fallback={}) const
As string.
bool isInt64() const
True when the JSON number was an integer literal that fits Int64.
std::unordered_map< std::string, int > getIntMap(const char *key) const
std::int64_t asInt64(std::int64_t fallback=0) const
Read an exact Int64 literal, or return the caller-supplied default for other numbers.
std::unordered_map< std::string, std::string > getStringMap(const char *key) const
std::vector< int > getIntArray(const char *key) const
Returns the int array.
std::vector< std::string > toStringArray() const
To string array.
std::vector< float > getFloatArray(const char *key) const
Returns the float array.
std::string quoteString(std::string_view value, StringEscapeStyle style)
Encode an owning JSON string using the canonical serializer's escaping implementation.
Result< std::string > stringify(const eve::Value &value)
Serialize the canonical owning Value as deterministic compact JSON.
Result< UnsignedIntegerScan > scanUnsignedInteger(std::string_view text, UnsignedIntegerSyntax syntax)
Scan an unsigned integer prefix without mutating caller state.
std::vector< Node > elements
std::vector< std::string > memberNames
std::vector< Node > memberValues