1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-13 20:21:48 +03:00

implemented "move"

This commit is contained in:
Niels
2016-04-24 17:43:27 +02:00
parent 855cf2307b
commit 09e9f6dcd4
3 changed files with 152 additions and 60 deletions

View File

@ -8836,6 +8836,44 @@ class basic_json
throw std::domain_error("JSON patch must be an array of objects");
}
const auto operation_add = [&result](json_pointer & ptr,
basic_json & value)
{
const auto last_path = ptr.pop_back();
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent[last_path] = value;
}
else if (parent.is_array())
{
if (last_path == "-")
{
parent.push_back(value);
}
else
{
parent.insert(parent.begin() + std::stoi(last_path),
value);
}
}
};
const auto operation_remove = [&result](json_pointer & ptr)
{
const auto last_path = ptr.pop_back();
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent.erase(parent.find(last_path));
}
else if (parent.is_array())
{
parent.erase(parent.begin() + std::stoi(last_path));
}
};
for (const auto& val : patch)
{
if (not val.is_object())
@ -8870,38 +8908,11 @@ class basic_json
throw std::domain_error("'add' operation must have member 'value'");
}
const auto last_path = ptr.pop_back();
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent[last_path] = it_value->second;
}
else if (parent.is_array())
{
if (last_path == "-")
{
parent.push_back(it_value->second);
}
else
{
parent.insert(parent.begin() + std::stoi(last_path),
it_value->second);
}
}
operation_add(ptr, it_value->second);
}
else if (op == "remove")
{
const auto last_path = ptr.pop_back();
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent.erase(parent.find(last_path));
}
else if (parent.is_array())
{
parent.erase(parent.begin() + std::stoi(last_path));
}
operation_remove(ptr);
}
else if (op == "replace")
{
@ -8920,7 +8931,11 @@ class basic_json
}
const std::string from_path = it_from->second;
const json_pointer from_ptr(from_path);
json_pointer from_ptr(from_path);
basic_json v = result[from_ptr];
operation_remove(from_ptr);
operation_add(ptr, v);
}
else if (op == "copy")
{