| 722 | |
| 723 | template <typename Json,typename T> |
| 724 | void add(Json& root, |
| 725 | const basic_json_pointer<typename Json::char_type>& location, |
| 726 | T&& value, |
| 727 | bool create_if_missing, |
| 728 | std::error_code& ec) |
| 729 | { |
| 730 | if (location.empty()) |
| 731 | { |
| 732 | root = std::forward<T>(value); |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | Json* current = std::addressof(root); |
| 737 | |
| 738 | std::basic_string<typename Json::char_type> buffer; |
| 739 | auto it = location.begin(); |
| 740 | auto end = location.end(); |
| 741 | while (it != end) |
| 742 | { |
| 743 | buffer = *it; |
| 744 | ++it; |
| 745 | if (it != end) |
| 746 | { |
| 747 | current = jsoncons::jsonpointer::detail::resolve(current, buffer, create_if_missing, ec); |
| 748 | if (JSONCONS_UNLIKELY(ec)) |
| 749 | return; |
| 750 | } |
| 751 | } |
| 752 | if (current->is_array()) |
| 753 | { |
| 754 | if (buffer.size() == 1 && buffer[0] == '-') |
| 755 | { |
| 756 | current->emplace_back(std::forward<T>(value)); |
| 757 | current = std::addressof(current->at(current->size()-1)); |
| 758 | } |
| 759 | else |
| 760 | { |
| 761 | std::size_t index{0}; |
| 762 | auto result = jsoncons::dec_to_integer(buffer.data(), buffer.length(), index); |
| 763 | if (!result) |
| 764 | { |
| 765 | ec = jsonpointer_errc::invalid_index; |
| 766 | return; |
| 767 | } |
| 768 | if (index > current->size()) |
| 769 | { |
| 770 | ec = jsonpointer_errc::index_exceeds_array_size; |
| 771 | return; |
| 772 | } |
| 773 | if (index == current->size()) |
| 774 | { |
| 775 | current->emplace_back(std::forward<T>(value)); |
| 776 | current = std::addressof(current->at(current->size()-1)); |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | auto it2 = current->insert(current->array_range().begin()+index,std::forward<T>(value)); |
| 781 | current = std::addressof(*it2); |