MQTTSuite
Loading...
Searching...
No Matches
json-patch.cpp
Go to the documentation of this file.
1#include "json-patch.hpp"
2
3#include <nlohmann/json-schema.hpp>
4
5namespace
6{
7
8// originally from http://jsonpatch.com/, http://json.schemastore.org/json-patch
9// with fixes
10const nlohmann::json patch_schema = R"patch({
11 "title": "JSON schema for JSONPatch files",
12 "$schema": "http://json-schema.org/draft-04/schema#",
13 "type": "array",
14
15 "items": {
16 "oneOf": [
17 {
18 "additionalProperties": false,
19 "required": [ "value", "op", "path"],
20 "properties": {
21 "path" : { "$ref": "#/definitions/path" },
22 "op": {
23 "description": "The operation to perform.",
24 "type": "string",
25 "enum": [ "add", "replace", "test" ]
26 },
27 "value": {
28 "description": "The value to add, replace or test."
29 }
30 }
31 },
32 {
33 "additionalProperties": false,
34 "required": [ "op", "path"],
35 "properties": {
36 "path" : { "$ref": "#/definitions/path" },
37 "op": {
38 "description": "The operation to perform.",
39 "type": "string",
40 "enum": [ "remove" ]
41 }
42 }
43 },
44 {
45 "additionalProperties": false,
46 "required": [ "from", "op", "path" ],
47 "properties": {
48 "path" : { "$ref": "#/definitions/path" },
49 "op": {
50 "description": "The operation to perform.",
51 "type": "string",
52 "enum": [ "move", "copy" ]
53 },
54 "from": {
55 "$ref": "#/definitions/path",
56 "description": "A JSON Pointer path pointing to the location to move/copy from."
57 }
58 }
59 }
60 ]
61 },
62 "definitions": {
63 "path": {
64 "description": "A JSON Pointer path.",
65 "type": "string"
66 }
67 }
68})patch"_json;
69} // namespace
70
71namespace nlohmann
72{
73
74json_patch::json_patch(json &&patch)
75 : j_(std::move(patch))
76{
78}
79
80json_patch::json_patch(const json &patch)
81 : j_(std::move(patch))
82{
84}
85
86json_patch &json_patch::add(const json::json_pointer &ptr, json value)
87{
88 j_.push_back(json{{"op", "add"}, {"path", ptr.to_string()}, {"value", std::move(value)}});
89 return *this;
90}
91
92json_patch &json_patch::replace(const json::json_pointer &ptr, json value)
93{
94 j_.push_back(json{{"op", "replace"}, {"path", ptr.to_string()}, {"value", std::move(value)}});
95 return *this;
96}
97
98json_patch &json_patch::remove(const json::json_pointer &ptr)
99{
100 j_.push_back(json{{"op", "remove"}, {"path", ptr.to_string()}});
101 return *this;
102}
103
104void json_patch::validateJsonPatch(json const &patch)
105{
106 // static put here to have it created at the first usage of validateJsonPatch
107 static nlohmann::json_schema::json_validator patch_validator(patch_schema);
108
109 patch_validator.validate(patch);
110
111 for (auto const &op : patch)
112 json::json_pointer(op["path"].get<std::string>());
113}
114
115} // namespace nlohmann
json_patch & add(const json::json_pointer &, json value)
json_patch & remove(const json::json_pointer &)
json_patch & replace(const json::json_pointer &, json value)
json_patch(json &&patch)
static void validateJsonPatch(json const &patch)
json_patch(const json &patch)
json_validator(const json &, schema_loader=nullptr, format_checker=nullptr, content_checker=nullptr)
const nlohmann::json patch_schema