| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | * THE SOFTWARE. |
| 20 | */ |
| 21 |
| 22 | #include "json11.hpp" |
| 23 | #include <cassert> |
| 24 | #include <cmath> |
| 25 | #include <cstdio> |
| 26 | #include <cstdlib> |
| 27 | #include <limits> |
| 28 | #include <locale> |
| 29 | #include <sstream> |
| 30 |
| 31 | namespace json11 |
| 32 | { |
| 33 |
| 34 | static const int max_depth = 200; |
| 35 |
| 36 | using std::initializer_list; |
| 37 | using std::make_shared; |
| 38 | using std::map; |
| 39 | using std::move; |
| 40 | using std::string; |
| 41 | using std::vector; |
| 42 |
| 43 | /* Helper for representing null - just a do-nothing struct, plus comparison |
| 44 | * operators so the helpers in JsonValue work. We can't use nullptr_t because |
| 45 | * it may not be orderable. |
| 46 | */ |
| 47 | struct NullStruct |
| 48 | { |
| 49 | bool operator==(NullStruct) const { return true; } |
| 50 | bool operator<(NullStruct) const { return false; } |
| 51 | }; |
| 52 |
| 53 | /* * * * * * * * * * * * * * * * * * * * |
| 54 | * Serialization |
| 55 | */ |
| 56 |
| 57 | static void dump(NullStruct, string& out) |
| 58 | { |
| 59 | out += "null"; |
| 60 | } |
| 61 |
| 62 | static void dump(double value, string& out) |
| 63 | { |
| 64 | if (std::isfinite(value)) |
| 65 | { |
| 66 | std::ostringstream stm; |
| 67 | stm.imbue(std::locale("C")); |
| 68 | stm << value; |
| 69 | out += stm.str(); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | out += "null"; |
| 74 | } |
| 75 | } |
| 76 |
| 77 | static void dump(int value, string& out) |
| 78 | { |
| 79 | char buf[32]; |
| 80 | snprintf(buf, sizeof buf, "%d", value); |
| 81 | out += buf; |
| 82 | } |
| 83 |
| 84 | static void dump(bool value, string& out) |
| 85 | { |
| 86 | out += value ? "true" : "false"; |
| 87 | } |
| 88 |
| 89 | static void dump(const string& value, string& out) |
| 90 | { |
| 91 | out += '"'; |
| 92 | for (std::size_t i = 0; i < value.length(); i++) |
| 93 | { |
| 94 | const char ch = value[i]; |
| 95 | if (ch == '\\') |
| 96 | { |
| 97 | out += "\\\\"; |
| 98 | } |
| 99 | else if (ch == '"') |
| 100 | { |
| 101 | out += "\\\""; |
| 102 | } |
| 103 | else if (ch == '\b') |
| 104 | { |
| 105 | out += "\\b"; |
| 106 | } |
| 107 | else if (ch == '\f') |
| 108 | { |
| 109 | out += "\\f"; |
| 110 | } |
| 111 | else if (ch == '\n') |
| 112 | { |
| 113 | out += "\\n"; |
| 114 | } |
| 115 | else if (ch == '\r') |
| 116 | { |
| 117 | out += "\\r"; |
| 118 | } |
| 119 | else if (ch == '\t') |
| 120 | { |
| 121 | out += "\\t"; |
| 122 | } |
| 123 | else if (static_cast<std::uint8_t>(ch) <= 0x1f) |
| 124 | { |
| 125 | char buf[8]; |
| 126 | snprintf(buf, sizeof buf, "\\u%04x", ch); |
| 127 | out += buf; |
| 128 | } |
| 129 | else if (static_cast<std::uint8_t>(ch) == 0xe2 && static_cast<std::uint8_t>(value[i + 1]) == 0x80 |
| 130 | && static_cast<std::uint8_t>(value[i + 2]) == 0xa8) |
| 131 | { |
| 132 | out += "\\u2028"; |
| 133 | i += 2; |
| 134 | } |
| 135 | else if (static_cast<std::uint8_t>(ch) == 0xe2 && static_cast<std::uint8_t>(value[i + 1]) == 0x80 |
| 136 | && static_cast<std::uint8_t>(value[i + 2]) == 0xa9) |
| 137 | { |
| 138 | out += "\\u2029"; |
| 139 | i += 2; |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | out += ch; |
| 144 | } |
| 145 | } |
| 146 | out += '"'; |
| 147 | } |
| 148 |
| 149 | static void dump(const Json::array& values, string& out) |
| 150 | { |
| 151 | bool first = true; |
| 152 | out += "["; |
| 153 | for (const auto& value : values) |
| 154 | { |
| 155 | if (!first) |
| 156 | out += ", "; |
| 157 | value.dump(out); |
| 158 | first = false; |
| 159 | } |
| 160 | out += "]"; |
| 161 | } |
| 162 |
| 163 | static void dump(const Json::object& values, string& out) |
| 164 | { |
| 165 | bool first = true; |
| 166 | out += "{"; |
| 167 | for (const auto& kv : values) |
| 168 | { |
| 169 | if (!first) |
| 170 | out += ", "; |
| 171 | dump(kv.first, out); |
| 172 | out += ": "; |
| 173 | kv.second.dump(out); |
| 174 | first = false; |
| 175 | } |
| 176 | out += "}"; |
| 177 | } |
| 178 |
| 179 | void Json::dump(string& out) const |
| 180 | { |
| 181 | m_ptr->dump(out); |
| 182 | } |
| 183 |
| 184 | /* * * * * * * * * * * * * * * * * * * * |
| 185 | * Value wrappers |
| 186 | */ |
| 187 |
| 188 | template <Json::Type tag, typename T> |
| 189 | class Value : public JsonValue |
| 190 | { |
| 191 | protected: |
| 192 | // Constructors |
| 193 | explicit Value(const T& value) |
| 194 | : m_value(value) |
| 195 | { |
| 196 | } |
| 197 | explicit Value(T&& value) |
| 198 | : m_value(move(value)) |
| 199 | { |
| 200 | } |
| 201 |
| 202 | // Get type tag |
| 203 | Json::Type type() const override |
| 204 | { |
| 205 | return tag; |
| 206 | } |
| 207 |
| 208 | // Comparisons |
| 209 | bool equals(const JsonValue* other) const override |
| 210 | { |
| 211 | return m_value == static_cast<const Value<tag, T>*>(other)->m_value; |
| 212 | } |
| 213 | bool less(const JsonValue* other) const override |
| 214 | { |
| 215 | return m_value < static_cast<const Value<tag, T>*>(other)->m_value; |
| 216 | } |
| 217 |
| 218 | const T m_value; |
| 219 | void dump(string& out) const override { json11::dump(m_value, out); } |
| 220 | }; |
| 221 |
| 222 | class JsonDouble final : public Value<Json::NUMBER, double> |
| 223 | { |
| 224 | double number_value() const override { return m_value; } |
| 225 | int int_value() const override { return static_cast<int>(m_value); } |
| 226 | bool equals(const JsonValue* other) const override { return m_value == other->number_value(); } |
| 227 | bool less(const JsonValue* other) const override { return m_value < other->number_value(); } |
| 228 |
| 229 | public: |
| 230 | explicit JsonDouble(double value) |
| 231 | : Value(value) |
| 232 | { |
| 233 | } |
| 234 | }; |
| 235 |
| 236 | class JsonInt final : public Value<Json::NUMBER, int> |
| 237 | { |
| 238 | double number_value() const override { return m_value; } |
| 239 | int int_value() const override { return m_value; } |
| 240 | bool equals(const JsonValue* other) const override { return m_value == other->number_value(); } |
| 241 | bool less(const JsonValue* other) const override { return m_value < other->number_value(); } |
| 242 |
| 243 | public: |
| 244 | explicit JsonInt(int value) |
| 245 | : Value(value) |
| 246 | { |
| 247 | } |
| 248 | }; |
| 249 |
| 250 | class JsonBoolean final : public Value<Json::BOOL, bool> |
| 251 | { |
| 252 | bool bool_value() const override { return m_value; } |
| 253 |
| 254 | public: |
| 255 | explicit JsonBoolean(bool value) |
| 256 | : Value(value) |
| 257 | { |
| 258 | } |
| 259 | }; |
| 260 |
| 261 | class JsonString final : public Value<Json::STRING, string> |
| 262 | { |
| 263 | const string& string_value() const override { return m_value; } |
| 264 |
| 265 | public: |
| 266 | explicit JsonString(const string& value) |
| 267 | : Value(value) |
| 268 | { |
| 269 | } |
| 270 | explicit JsonString(string&& value) |
| 271 | : Value(move(value)) |
| 272 | { |
| 273 | } |
| 274 | }; |
| 275 |
| 276 | class JsonArray final : public Value<Json::ARRAY, Json::array> |
| 277 | { |
| 278 | const Json::array& array_items() const override { return m_value; } |
| 279 | const Json& operator[](std::size_t i) const override; |
| 280 |
| 281 | public: |
| 282 | explicit JsonArray(const Json::array& value) |
| 283 | : Value(value) |
| 284 | { |
| 285 | } |
| 286 | explicit JsonArray(Json::array&& value) |
| 287 | : Value(move(value)) |
| 288 | { |
| 289 | } |
| 290 | }; |
| 291 |
| 292 | class JsonObject final : public Value<Json::OBJECT, Json::object> |
| 293 | { |
| 294 | const Json::object& object_items() const override { return m_value; } |
| 295 | const Json& operator[](const string& key) const override; |
| 296 |
| 297 | public: |
| 298 | explicit JsonObject(const Json::object& value) |
| 299 | : Value(value) |
| 300 | { |
| 301 | } |
| 302 | explicit JsonObject(Json::object&& value) |
| 303 | : Value(move(value)) |
| 304 | { |
| 305 | } |
| 306 | }; |
| 307 |
| 308 | class JsonNull final : public Value<Json::NUL, NullStruct> |
| 309 | { |
| 310 | public: |
| 311 | JsonNull() |
| 312 | : Value({}) |
| 313 | { |
| 314 | } |
| 315 | }; |
| 316 |
| 317 | /* * * * * * * * * * * * * * * * * * * * |
| 318 | * Static globals - static-init-safe |
| 319 | */ |
| 320 | struct Statics |
| 321 | { |
| 322 | const std::shared_ptr<JsonValue> null = make_shared<JsonNull>(); |
| 323 | const std::shared_ptr<JsonValue> t = make_shared<JsonBoolean>(true); |
| 324 | const std::shared_ptr<JsonValue> f = make_shared<JsonBoolean>(false); |
| 325 | const string empty_string; |
| 326 | const vector<Json> empty_vector; |
| 327 | const map<string, Json> empty_map; |
| 328 | Statics() {} |
| 329 | }; |
| 330 |
| 331 | static const Statics& statics() |
| 332 | { |
| 333 | static const Statics s {}; |
| 334 | return s; |
| 335 | } |
| 336 |
| 337 | static const Json& static_null() |
| 338 | { |
| 339 | // This has to be separate, not in Statics, because Json() accesses statics().null. |
| 340 | static const Json json_null; |
| 341 | return json_null; |
| 342 | } |
| 343 |
| 344 | /* * * * * * * * * * * * * * * * * * * * |
| 345 | * Constructors |
| 346 | */ |
| 347 |
| 348 | Json::Json() noexcept |
| 349 | : m_ptr(statics().null) |
| 350 | { |
| 351 | } |
| 352 | Json::Json(std::nullptr_t) noexcept |
| 353 | : m_ptr(statics().null) |
| 354 | { |
| 355 | } |
| 356 | Json::Json(double value) |
| 357 | : m_ptr(make_shared<JsonDouble>(value)) |
| 358 | { |
| 359 | } |
| 360 | Json::Json(int value) |
| 361 | : m_ptr(make_shared<JsonInt>(value)) |
| 362 | { |
| 363 | } |
| 364 | Json::Json(bool value) |
| 365 | : m_ptr(value ? statics().t : statics().f) |
| 366 | { |
| 367 | } |
| 368 | Json::Json(const string& value) |
| 369 | : m_ptr(make_shared<JsonString>(value)) |
| 370 | { |
| 371 | } |
| 372 | Json::Json(string&& value) |
| 373 | : m_ptr(make_shared<JsonString>(move(value))) |
| 374 | { |
| 375 | } |
| 376 | Json::Json(const char* value) |
| 377 | : m_ptr(make_shared<JsonString>(value)) |
| 378 | { |
| 379 | } |
| 380 | Json::Json(const Json::array& values) |
| 381 | : m_ptr(make_shared<JsonArray>(values)) |
| 382 | { |
| 383 | } |
| 384 | Json::Json(Json::array&& values) |
| 385 | : m_ptr(make_shared<JsonArray>(move(values))) |
| 386 | { |
| 387 | } |
| 388 | Json::Json(const Json::object& values) |
| 389 | : m_ptr(make_shared<JsonObject>(values)) |
| 390 | { |
| 391 | } |
| 392 | Json::Json(Json::object&& values) |
| 393 | : m_ptr(make_shared<JsonObject>(move(values))) |
| 394 | { |
| 395 | } |
| 396 |
| 397 | /* * * * * * * * * * * * * * * * * * * * |
| 398 | * Accessors |
| 399 | */ |
| 400 |
| 401 | Json::Type Json::type() const { return m_ptr->type(); } |
| 402 | double Json::number_value() const { return m_ptr->number_value(); } |
| 403 | int Json::int_value() const { return m_ptr->int_value(); } |
| 404 | bool Json::bool_value() const { return m_ptr->bool_value(); } |
| 405 | const string& Json::string_value() const { return m_ptr->string_value(); } |
| 406 | const vector<Json>& Json::array_items() const { return m_ptr->array_items(); } |
| 407 | const map<string, Json>& Json::object_items() const { return m_ptr->object_items(); } |
| 408 | const Json& Json::operator[](std::size_t i) const { return (*m_ptr)[i]; } |
| 409 | const Json& Json::operator[](const string& key) const { return (*m_ptr)[key]; } |
| 410 |
| 411 | double JsonValue::number_value() const { return 0; } |
| 412 | int JsonValue::int_value() const { return 0; } |
| 413 | bool JsonValue::bool_value() const { return false; } |
| 414 | const string& JsonValue::string_value() const { return statics().empty_string; } |
| 415 | const vector<Json>& JsonValue::array_items() const { return statics().empty_vector; } |
| 416 | const map<string, Json>& JsonValue::object_items() const { return statics().empty_map; } |
| 417 | const Json& JsonValue::operator[](std::size_t) const { return static_null(); } |
| 418 | const Json& JsonValue::operator[](const string&) const { return static_null(); } |
| 419 |
| 420 | const Json& JsonObject::operator[](const string& key) const |
| 421 | { |
| 422 | auto iter = m_value.find(key); |
| 423 | return (iter == m_value.end()) ? static_null() : iter->second; |
| 424 | } |
| 425 | const Json& JsonArray::operator[](std::size_t i) const |
| 426 | { |
| 427 | if (i >= m_value.size()) |
| 428 | return static_null(); |
| 429 | else |
| 430 | return m_value[i]; |
| 431 | } |
| 432 |
| 433 | /* * * * * * * * * * * * * * * * * * * * |
| 434 | * Comparison |
| 435 | */ |
| 436 |
| 437 | bool Json::operator==(const Json& other) const |
| 438 | { |
| 439 | if (m_ptr == other.m_ptr) |
| 440 | return true; |
| 441 | if (m_ptr->type() != other.m_ptr->type()) |
| 442 | return false; |
| 443 |
| 444 | return m_ptr->equals(other.m_ptr.get()); |
| 445 | } |
| 446 |
| 447 | bool Json::operator<(const Json& other) const |
| 448 | { |
| 449 | if (m_ptr == other.m_ptr) |
| 450 | return false; |
| 451 | if (m_ptr->type() != other.m_ptr->type()) |
| 452 | return m_ptr->type() < other.m_ptr->type(); |
| 453 |
| 454 | return m_ptr->less(other.m_ptr.get()); |
| 455 | } |
| 456 |
| 457 | /* * * * * * * * * * * * * * * * * * * * |
| 458 | * Parsing |
| 459 | */ |
| 460 |
| 461 | /* esc(c) |
| 462 | * |
| 463 | * Format char c suitable for printing in an error message. |
| 464 | */ |
| 465 | static inline string esc(char c) |
| 466 | { |
| 467 | char buf[12]; |
| 468 | if (static_cast<std::uint8_t>(c) >= 0x20 && static_cast<std::uint8_t>(c) <= 0x7f) |
| 469 | { |
| 470 | snprintf(buf, sizeof buf, "'%c' (%d)", c, c); |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | snprintf(buf, sizeof buf, "(%d)", c); |
| 475 | } |
| 476 | return string(buf); |
| 477 | } |
| 478 |
| 479 | static inline bool in_range(long x, long lower, long upper) |
| 480 | { |
| 481 | return (x >= lower && x <= upper); |
| 482 | } |
| 483 |
| 484 | namespace |
| 485 | { |
| 486 | /* JsonParser |
| 487 | * |
| 488 | * Object that tracks all state of an in-progress parse. |
| 489 | */ |
| 490 | struct JsonParser final |
| 491 | { |
| 492 |
| 493 | /* State |
| 494 | */ |
| 495 | const string& str; |
| 496 | std::size_t i; |
| 497 | string& err; |
| 498 | bool failed; |
| 499 | const JsonParse strategy; |
| 500 |
| 501 | /* fail(msg, err_ret = Json()) |
| 502 | * |
| 503 | * Mark this parse as failed. |
| 504 | */ |
| 505 | Json fail(string&& msg) |
| 506 | { |
| 507 | return fail(move(msg), Json()); |
| 508 | } |
| 509 |
| 510 | template <typename T> |
| 511 | T fail(string&& msg, const T err_ret) |
| 512 | { |
| 513 | if (!failed) |
| 514 | err = std::move(msg); |
| 515 | failed = true; |
| 516 | return err_ret; |
| 517 | } |
| 518 |
| 519 | /* consume_whitespace() |
| 520 | * |
| 521 | * Advance until the current character is non-whitespace. |
| 522 | */ |
| 523 | void consume_whitespace() |
| 524 | { |
| 525 | while (str[i] == ' ' || str[i] == '\r' || str[i] == '\n' || str[i] == '\t') |
| 526 | i++; |
| 527 | } |
| 528 |
| 529 | /* consume_comment() |
| 530 | * |
| 531 | * Advance comments (c-style inline and multiline). |
| 532 | */ |
| 533 | bool consume_comment() |
| 534 | { |
| 535 | bool comment_found = false; |
| 536 | if (str[i] == '/') |
| 537 | { |
| 538 | i++; |
| 539 | if (i == str.size()) |
| 540 | return fail("unexpected end of input after start of comment", false); |
| 541 | if (str[i] == '/') |
| 542 | { // inline comment |
| 543 | i++; |
| 544 | // advance until next line, or end of input |
| 545 | while (i < str.size() && str[i] != '\n') |
| 546 | { |
| 547 | i++; |
| 548 | } |
| 549 | comment_found = true; |
| 550 | } |
| 551 | else if (str[i] == '*') |
| 552 | { // multiline comment |
| 553 | i++; |
| 554 | if (i > str.size() - 2) |
| 555 | return fail("unexpected end of input inside multi-line comment", false); |
| 556 | // advance until closing tokens |
| 557 | while (!(str[i] == '*' && str[i + 1] == '/')) |
| 558 | { |
| 559 | i++; |
| 560 | if (i > str.size() - 2) |
| 561 | return fail( |
| 562 | "unexpected end of input inside multi-line comment", false); |
| 563 | } |
| 564 | i += 2; |
| 565 | comment_found = true; |
| 566 | } |
| 567 | else |
| 568 | return fail("malformed comment", false); |
| 569 | } |
| 570 | return comment_found; |
| 571 | } |
| 572 |
| 573 | /* consume_garbage() |
| 574 | * |
| 575 | * Advance until the current character is non-whitespace and non-comment. |
| 576 | */ |
| 577 | void consume_garbage() |
| 578 | { |
| 579 | consume_whitespace(); |
| 580 | if (strategy == JsonParse::COMMENTS) |
| 581 | { |
| 582 | bool comment_found = false; |
| 583 | do |
| 584 | { |
| 585 | comment_found = consume_comment(); |
| 586 | if (failed) |
| 587 | return; |
| 588 | consume_whitespace(); |
| 589 | } while (comment_found); |
| 590 | } |
| 591 | } |
| 592 |
| 593 | /* get_next_token() |
| 594 | * |
| 595 | * Return the next non-whitespace character. If the end of the input is reached, |
| 596 | * flag an error and return 0. |
| 597 | */ |
| 598 | char get_next_token() |
| 599 | { |
| 600 | consume_garbage(); |
| 601 | if (failed) |
| 602 | return (char)0; |
| 603 | if (i == str.size()) |
| 604 | return fail("unexpected end of input", (char)0); |
| 605 |
| 606 | return str[i++]; |
| 607 | } |
| 608 |
| 609 | /* encode_utf8(pt, out) |
| 610 | * |
| 611 | * Encode pt as UTF-8 and add it to out. |
| 612 | */ |
| 613 | void encode_utf8(long pt, string& out) |
| 614 | { |
| 615 | if (pt < 0) |
| 616 | return; |
| 617 |
| 618 | if (pt < 0x80) |
| 619 | { |
| 620 | out += static_cast<char>(pt); |
| 621 | } |
| 622 | else if (pt < 0x800) |
| 623 | { |
| 624 | out += static_cast<char>((pt >> 6) | 0xC0); |
| 625 | out += static_cast<char>((pt & 0x3F) | 0x80); |
| 626 | } |
| 627 | else if (pt < 0x10000) |
| 628 | { |
| 629 | out += static_cast<char>((pt >> 12) | 0xE0); |
| 630 | out += static_cast<char>(((pt >> 6) & 0x3F) | 0x80); |
| 631 | out += static_cast<char>((pt & 0x3F) | 0x80); |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | out += static_cast<char>((pt >> 18) | 0xF0); |
| 636 | out += static_cast<char>(((pt >> 12) & 0x3F) | 0x80); |
| 637 | out += static_cast<char>(((pt >> 6) & 0x3F) | 0x80); |
| 638 | out += static_cast<char>((pt & 0x3F) | 0x80); |
| 639 | } |
| 640 | } |
| 641 |
| 642 | /* parse_string() |
| 643 | * |
| 644 | * Parse a string, starting at the current position. |
| 645 | */ |
| 646 | string parse_string() |
| 647 | { |
| 648 | string out; |
| 649 | long last_escaped_codepoint = -1; |
| 650 | while (true) |
| 651 | { |
| 652 | if (i == str.size()) |
| 653 | return fail("unexpected end of input in string", ""); |
| 654 |
| 655 | char ch = str[i++]; |
| 656 |
| 657 | if (ch == '"') |
| 658 | { |
| 659 | encode_utf8(last_escaped_codepoint, out); |
| 660 | return out; |
| 661 | } |
| 662 |
| 663 | if (in_range(ch, 0, 0x1f)) |
| 664 | return fail("unescaped " + esc(ch) + " in string", ""); |
| 665 |
| 666 | // The usual case: non-escaped characters |
| 667 | if (ch != '\\') |
| 668 | { |
| 669 | encode_utf8(last_escaped_codepoint, out); |
| 670 | last_escaped_codepoint = -1; |
| 671 | out += ch; |
| 672 | continue; |
| 673 | } |
| 674 |
| 675 | // Handle escapes |
| 676 | if (i == str.size()) |
| 677 | return fail("unexpected end of input in string", ""); |
| 678 |
| 679 | ch = str[i++]; |
| 680 |
| 681 | if (ch == 'u') |
| 682 | { |
| 683 | // Extract 4-byte escape sequence |
| 684 | string esc = str.substr(i, 4); |
| 685 | // Explicitly check length of the substring. The following loop |
| 686 | // relies on std::string returning the terminating NUL when |
| 687 | // accessing str[length]. Checking here reduces brittleness. |
| 688 | if (esc.length() < 4) |
| 689 | { |
| 690 | return fail("bad \\u escape: " + esc, ""); |
| 691 | } |
| 692 | for (std::size_t j = 0; j < 4; j++) |
| 693 | { |
| 694 | if (!in_range(esc[j], 'a', 'f') && !in_range(esc[j], 'A', 'F') |
| 695 | && !in_range(esc[j], '0', '9')) |
| 696 | return fail("bad \\u escape: " + esc, ""); |
| 697 | } |
| 698 |
| 699 | long codepoint = strtol(esc.data(), nullptr, 16); |
| 700 |
| 701 | // JSON specifies that characters outside the BMP shall be encoded as a pair |
| 702 | // of 4-hex-digit \u escapes encoding their surrogate pair components. Check |
| 703 | // whether we're in the middle of such a beast: the previous codepoint was an |
| 704 | // escaped lead (high) surrogate, and this is a trail (low) surrogate. |
| 705 | if (in_range(last_escaped_codepoint, 0xD800, 0xDBFF) |
| 706 | && in_range(codepoint, 0xDC00, 0xDFFF)) |
| 707 | { |
| 708 | // Reassemble the two surrogate pairs into one astral-plane character, per |
| 709 | // the UTF-16 algorithm. |
| 710 | encode_utf8((((last_escaped_codepoint - 0xD800) << 10) |
| 711 | | (codepoint - 0xDC00)) |
| 712 | + 0x10000, |
| 713 | out); |
| 714 | last_escaped_codepoint = -1; |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | encode_utf8(last_escaped_codepoint, out); |
| 719 | last_escaped_codepoint = codepoint; |
| 720 | } |
| 721 |
| 722 | i += 4; |
| 723 | continue; |
| 724 | } |
| 725 |
| 726 | encode_utf8(last_escaped_codepoint, out); |
| 727 | last_escaped_codepoint = -1; |
| 728 |
| 729 | if (ch == 'b') |
| 730 | { |
| 731 | out += '\b'; |
| 732 | } |
| 733 | else if (ch == 'f') |
| 734 | { |
| 735 | out += '\f'; |
| 736 | } |
| 737 | else if (ch == 'n') |
| 738 | { |
| 739 | out += '\n'; |
| 740 | } |
| 741 | else if (ch == 'r') |
| 742 | { |
| 743 | out += '\r'; |
| 744 | } |
| 745 | else if (ch == 't') |
| 746 | { |
| 747 | out += '\t'; |
| 748 | } |
| 749 | else if (ch == '"' || ch == '\\' || ch == '/') |
| 750 | { |
| 751 | out += ch; |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | return fail("invalid escape character " + esc(ch), ""); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 |
| 760 | /* parse_number() |
| 761 | * |
| 762 | * Parse a double. |
| 763 | */ |
| 764 | Json parse_number() |
| 765 | { |
| 766 | std::size_t start_pos = i; |
| 767 |
| 768 | if (str[i] == '-') |
| 769 | i++; |
| 770 |
| 771 | // Integer part |
| 772 | if (str[i] == '0') |
| 773 | { |
| 774 | i++; |
| 775 | if (in_range(str[i], '0', '9')) |
| 776 | return fail("leading 0s not permitted in numbers"); |
| 777 | } |
| 778 | else if (in_range(str[i], '1', '9')) |
| 779 | { |
| 780 | i++; |
| 781 | while (in_range(str[i], '0', '9')) |
| 782 | i++; |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | return fail("invalid " + esc(str[i]) + " in number"); |
| 787 | } |
| 788 |
| 789 | if (str[i] != '.' && str[i] != 'e' && str[i] != 'E' |
| 790 | && (i - start_pos) <= static_cast<std::size_t>(std::numeric_limits<int>::digits10)) |
| 791 | { |
| 792 | return std::atoi(str.c_str() + start_pos); |
| 793 | } |
| 794 |
| 795 | // Decimal part |
| 796 | if (str[i] == '.') |
| 797 | { |
| 798 | i++; |
| 799 | if (!in_range(str[i], '0', '9')) |
| 800 | return fail("at least one digit required in fractional part"); |
| 801 |
| 802 | while (in_range(str[i], '0', '9')) |
| 803 | i++; |
| 804 | } |
| 805 |
| 806 | // Exponent part |
| 807 | if (str[i] == 'e' || str[i] == 'E') |
| 808 | { |
| 809 | i++; |
| 810 |
| 811 | if (str[i] == '+' || str[i] == '-') |
| 812 | i++; |
| 813 |
| 814 | if (!in_range(str[i], '0', '9')) |
| 815 | return fail("at least one digit required in exponent"); |
| 816 |
| 817 | while (in_range(str[i], '0', '9')) |
| 818 | i++; |
| 819 | } |
| 820 |
| 821 | std::istringstream stm(std::string(str.begin() + start_pos, str.end())); |
| 822 | stm.imbue(std::locale("C")); |
| 823 | double result; |
| 824 | stm >> result; |
| 825 | return result; |
| 826 | } |
| 827 |
| 828 | /* expect(str, res) |
| 829 | * |
| 830 | * Expect that 'str' starts at the character that was just read. If it does, advance |
| 831 | * the input and return res. If not, flag an error. |
| 832 | */ |
| 833 | Json expect(const string& expected, Json res) |
| 834 | { |
| 835 | assert(i != 0); |
| 836 | i--; |
| 837 | if (str.compare(i, expected.length(), expected) == 0) |
| 838 | { |
| 839 | i += expected.length(); |
| 840 | return res; |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | return fail("parse error: expected " + expected + ", got " + str.substr(i, expected.length())); |
| 845 | } |
| 846 | } |
| 847 |
| 848 | /* parse_json() |
| 849 | * |
| 850 | * Parse a JSON object. |
| 851 | */ |
| 852 | Json parse_json(int depth) |
| 853 | { |
| 854 | if (depth > max_depth) |
| 855 | { |
| 856 | return fail("exceeded maximum nesting depth"); |
| 857 | } |
| 858 |
| 859 | char ch = get_next_token(); |
| 860 | if (failed) |
| 861 | return Json(); |
| 862 |
| 863 | if (ch == '-' || (ch >= '0' && ch <= '9')) |
| 864 | { |
| 865 | i--; |
| 866 | return parse_number(); |
| 867 | } |
| 868 |
| 869 | if (ch == 't') |
| 870 | return expect("true", true); |
| 871 |
| 872 | if (ch == 'f') |
| 873 | return expect("false", false); |
| 874 |
| 875 | if (ch == 'n') |
| 876 | return expect("null", Json()); |
| 877 |
| 878 | if (ch == '"') |
| 879 | return parse_string(); |
| 880 |
| 881 | if (ch == '{') |
| 882 | { |
| 883 | map<string, Json> data; |
| 884 | ch = get_next_token(); |
| 885 | if (ch == '}') |
| 886 | return data; |
| 887 |
| 888 | while (1) |
| 889 | { |
| 890 | if (ch != '"') |
| 891 | return fail("expected '\"' in object, got " + esc(ch)); |
| 892 |
| 893 | string key = parse_string(); |
| 894 | if (failed) |
| 895 | return Json(); |
| 896 |
| 897 | ch = get_next_token(); |
| 898 | if (ch != ':') |
| 899 | return fail("expected ':' in object, got " + esc(ch)); |
| 900 |
| 901 | data[std::move(key)] = parse_json(depth + 1); |
| 902 | if (failed) |
| 903 | return Json(); |
| 904 |
| 905 | ch = get_next_token(); |
| 906 | if (ch == '}') |
| 907 | break; |
| 908 | if (ch != ',') |
| 909 | return fail("expected ',' in object, got " + esc(ch)); |
| 910 |
| 911 | ch = get_next_token(); |
| 912 | } |
| 913 | return data; |
| 914 | } |
| 915 |
| 916 | if (ch == '[') |
| 917 | { |
| 918 | vector<Json> data; |
| 919 | ch = get_next_token(); |
| 920 | if (ch == ']') |
| 921 | return data; |
| 922 |
| 923 | while (1) |
| 924 | { |
| 925 | i--; |
| 926 | data.push_back(parse_json(depth + 1)); |
| 927 | if (failed) |
| 928 | return Json(); |
| 929 |
| 930 | ch = get_next_token(); |
| 931 | if (ch == ']') |
| 932 | break; |
| 933 | if (ch != ',') |
| 934 | return fail("expected ',' in list, got " + esc(ch)); |
| 935 |
| 936 | ch = get_next_token(); |
| 937 | (void)ch; |
| 938 | } |
| 939 | return data; |
| 940 | } |
| 941 |
| 942 | return fail("expected value, got " + esc(ch)); |
| 943 | } |
| 944 | }; |
| 945 | } //namespace { |
| 946 |
| 947 | Json Json::parse(const string& in, string& err, JsonParse strategy) |
| 948 | { |
| 949 | JsonParser parser {in, 0, err, false, strategy}; |
| 950 | Json result = parser.parse_json(0); |
| 951 |
| 952 | // Check for any trailing garbage |
| 953 | parser.consume_garbage(); |
| 954 | if (parser.failed) |
| 955 | return Json(); |
| 956 | if (parser.i != in.size()) |
| 957 | return parser.fail("unexpected trailing " + esc(in[parser.i])); |
| 958 |
| 959 | return result; |
| 960 | } |
| 961 |
| 962 | // Documented in json11.hpp |
| 963 | vector<Json> Json::parse_multi(const string& in, |
| 964 | std::string::size_type& parser_stop_pos, |
| 965 | string& err, |
| 966 | JsonParse strategy) |
| 967 | { |
| 968 | JsonParser parser {in, 0, err, false, strategy}; |
| 969 | parser_stop_pos = 0; |
| 970 | vector<Json> json_vec; |
| 971 | while (parser.i != in.size() && !parser.failed) |
| 972 | { |
| 973 | json_vec.push_back(parser.parse_json(0)); |
| 974 | if (parser.failed) |
| 975 | break; |
| 976 |
| 977 | // Check for another object |
| 978 | parser.consume_garbage(); |
| 979 | if (parser.failed) |
| 980 | break; |
| 981 | parser_stop_pos = parser.i; |
| 982 | } |
| 983 | return json_vec; |
| 984 | } |
| 985 |
| 986 | /* * * * * * * * * * * * * * * * * * * * |
| 987 | * Shape-checking |
| 988 | */ |
| 989 |
| 990 | bool Json::has_shape(const shape& types, string& err) const |
| 991 | { |
| 992 | if (!is_object()) |
| 993 | { |
| 994 | err = "expected JSON object, got " + dump(); |
| 995 | return false; |
| 996 | } |
| 997 |
| 998 | for (auto& item : types) |
| 999 | { |
| 1000 | if ((*this)[item.first].type() != item.second) |
| 1001 | { |
| 1002 | err = "bad type for " + item.first + " in " + dump(); |
| 1003 | return false; |
| 1004 | } |
| 1005 | } |
| 1006 |
| 1007 | return true; |
| 1008 | } |
| 1009 |
| 1010 | } // namespace json11 |
| 1011 |