2021-03-12 05:59:23 +00:00
|
|
|
#include "json-patch.hpp"
|
|
|
|
|
|
|
|
#include <nlohmann/json-schema.hpp>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// originally from http://jsonpatch.com/, http://json.schemastore.org/json-patch
|
|
|
|
// with fixes
|
|
|
|
const nlohmann::json patch_schema = R"patch({
|
|
|
|
"title": "JSON schema for JSONPatch files",
|
|
|
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
|
|
|
"type": "array",
|
|
|
|
|
|
|
|
"items": {
|
|
|
|
"oneOf": [
|
|
|
|
{
|
|
|
|
"additionalProperties": false,
|
|
|
|
"required": [ "value", "op", "path"],
|
|
|
|
"properties": {
|
|
|
|
"path" : { "$ref": "#/definitions/path" },
|
|
|
|
"op": {
|
|
|
|
"description": "The operation to perform.",
|
|
|
|
"type": "string",
|
|
|
|
"enum": [ "add", "replace", "test" ]
|
|
|
|
},
|
|
|
|
"value": {
|
|
|
|
"description": "The value to add, replace or test."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"additionalProperties": false,
|
|
|
|
"required": [ "op", "path"],
|
|
|
|
"properties": {
|
|
|
|
"path" : { "$ref": "#/definitions/path" },
|
|
|
|
"op": {
|
|
|
|
"description": "The operation to perform.",
|
|
|
|
"type": "string",
|
|
|
|
"enum": [ "remove" ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"additionalProperties": false,
|
|
|
|
"required": [ "from", "op", "path" ],
|
|
|
|
"properties": {
|
|
|
|
"path" : { "$ref": "#/definitions/path" },
|
|
|
|
"op": {
|
|
|
|
"description": "The operation to perform.",
|
|
|
|
"type": "string",
|
|
|
|
"enum": [ "move", "copy" ]
|
|
|
|
},
|
|
|
|
"from": {
|
|
|
|
"$ref": "#/definitions/path",
|
|
|
|
"description": "A JSON Pointer path pointing to the location to move/copy from."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"definitions": {
|
|
|
|
"path": {
|
|
|
|
"description": "A JSON Pointer path.",
|
|
|
|
"type": "string"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})patch"_json;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
json_patch::json_patch( json&& patch ) : j_( std::move( patch ) )
|
2021-03-12 05:59:23 +00:00
|
|
|
{
|
2024-04-13 00:00:12 +00:00
|
|
|
validateJsonPatch( j_ );
|
2021-03-12 05:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
json_patch::json_patch( const json& patch ) : j_( std::move( patch ) )
|
2021-03-12 05:59:23 +00:00
|
|
|
{
|
2024-04-13 00:00:12 +00:00
|
|
|
validateJsonPatch( j_ );
|
2021-03-12 05:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
json_patch& json_patch::add( const json::json_pointer& ptr, json value )
|
2021-03-12 05:59:23 +00:00
|
|
|
{
|
2024-04-13 00:00:12 +00:00
|
|
|
j_.push_back(
|
|
|
|
json{ { "op", "add" }, { "path", ptr.to_string() }, { "value", std::move( value ) } } );
|
|
|
|
return *this;
|
2021-03-12 05:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
json_patch& json_patch::replace( const json::json_pointer& ptr, json value )
|
2021-03-12 05:59:23 +00:00
|
|
|
{
|
2024-04-13 00:00:12 +00:00
|
|
|
j_.push_back( json{
|
|
|
|
{ "op", "replace" }, { "path", ptr.to_string() }, { "value", std::move( value ) } } );
|
|
|
|
return *this;
|
2021-03-12 05:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
json_patch& json_patch::remove( const json::json_pointer& ptr )
|
2021-03-12 05:59:23 +00:00
|
|
|
{
|
2024-04-13 00:00:12 +00:00
|
|
|
j_.push_back( json{ { "op", "remove" }, { "path", ptr.to_string() } } );
|
|
|
|
return *this;
|
2021-03-12 05:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
void json_patch::validateJsonPatch( json const& patch )
|
2021-03-12 05:59:23 +00:00
|
|
|
{
|
2024-04-13 00:00:12 +00:00
|
|
|
// static put here to have it created at the first usage of validateJsonPatch
|
|
|
|
static nlohmann::json_schema::json_validator patch_validator( patch_schema );
|
2021-03-12 05:59:23 +00:00
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
patch_validator.validate( patch );
|
2021-03-12 05:59:23 +00:00
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
for( auto const& op : patch )
|
|
|
|
json::json_pointer( op["path"].get<std::string>() );
|
2021-03-12 05:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-04-13 00:00:12 +00:00
|
|
|
} // namespace nlohmann
|