MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / add

Function add

include/jsoncons_ext/jsonpointer/jsonpointer.hpp:724–795  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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);

Calls 15

resolveFunction · 0.85
dec_to_integerFunction · 0.85
parseFunction · 0.85
jsonpointer_errorClass · 0.85
atMethod · 0.80
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
is_arrayMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45
lengthMethod · 0.45

Tested by 1

check_insert_or_assignFunction · 0.68