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

Function replace

include/jsoncons_ext/jsonpointer/jsonpointer.hpp:1119–1196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1117
1118 template <typename Json,typename T>
1119 void replace(Json& root,
1120 const basic_json_pointer<typename Json::char_type>& location,
1121 T&& value,
1122 bool create_if_missing,
1123 std::error_code& ec)
1124 {
1125 if (location.empty())
1126 {
1127 root = std::forward<T>(value);
1128 return;
1129 }
1130 Json* current = std::addressof(root);
1131
1132 std::basic_string<typename Json::char_type> buffer;
1133 auto it = location.begin();
1134 auto end = location.end();
1135
1136 while (it != end)
1137 {
1138 buffer = *it;
1139 ++it;
1140 if (it != end)
1141 {
1142 current = jsoncons::jsonpointer::detail::resolve(current, buffer, create_if_missing, ec);
1143 if (JSONCONS_UNLIKELY(ec))
1144 return;
1145 }
1146 }
1147 if (current->is_array())
1148 {
1149 if (buffer.size() == 1 && buffer[0] == '-')
1150 {
1151 ec = jsonpointer_errc::index_exceeds_array_size;
1152 return;
1153 }
1154 else
1155 {
1156 std::size_t index{};
1157 auto result = jsoncons::dec_to_integer(buffer.data(), buffer.length(), index);
1158 if (!result)
1159 {
1160 ec = jsonpointer_errc::invalid_index;
1161 return;
1162 }
1163 if (index >= current->size())
1164 {
1165 ec = jsonpointer_errc::index_exceeds_array_size;
1166 return;
1167 }
1168 current->at(index) = std::forward<T>(value);
1169 }
1170 }
1171 else if (current->is_object())
1172 {
1173 if (!current->contains(buffer))
1174 {
1175 if (create_if_missing)
1176 {

Callers

nothing calls this directly

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

no test coverage detected