parse_json() * * Parse a JSON object. */
| 639 | * Parse a JSON object. |
| 640 | */ |
| 641 | Json parse_json(int depth) { |
| 642 | if (depth > max_depth) { |
| 643 | return fail("Exceeded maximum nesting depth"); |
| 644 | } |
| 645 | |
| 646 | char ch = get_next_token(); |
| 647 | if (failed) |
| 648 | return Json(); |
| 649 | |
| 650 | if (ch == '-' || (ch >= '0' && ch <= '9')) { |
| 651 | i--; |
| 652 | return parse_number(); |
| 653 | } |
| 654 | |
| 655 | if (ch == 't') |
| 656 | return expect("true", true); |
| 657 | |
| 658 | if (ch == 'f') |
| 659 | return expect("false", false); |
| 660 | |
| 661 | if (ch == 'n') |
| 662 | return expect("null", Json()); |
| 663 | |
| 664 | if (ch == '"') |
| 665 | return parse_string(); |
| 666 | |
| 667 | if (ch == '{') { |
| 668 | map<string, Json> data; |
| 669 | ch = get_next_token(); |
| 670 | if (ch == '}') |
| 671 | return data; |
| 672 | |
| 673 | while (1) { |
| 674 | if (ch != '"') |
| 675 | return fail("Expected '\"' in object, got " + esc(ch)); |
| 676 | |
| 677 | string key = parse_string(); |
| 678 | if (failed) |
| 679 | return Json(); |
| 680 | |
| 681 | ch = get_next_token(); |
| 682 | if (ch != ':') |
| 683 | return fail("Expected ':' in object, got " + esc(ch)); |
| 684 | |
| 685 | data[std::move(key)] = parse_json(depth + 1); |
| 686 | if (failed) |
| 687 | return Json(); |
| 688 | |
| 689 | ch = get_next_token(); |
| 690 | if (ch == '}') |
| 691 | break; |
| 692 | if (ch != ',') |
| 693 | return fail("Expected ',' in object, got " + esc(ch)); |
| 694 | |
| 695 | ch = get_next_token(); |
| 696 | } |
| 697 | return data; |
| 698 | } |
no test coverage detected