|
1 | 1 | #include "json-patch.hpp"
|
2 | 2 |
|
| 3 | +#include <nlohmann/json-schema.hpp> |
| 4 | + |
| 5 | +namespace |
| 6 | +{ |
| 7 | + |
| 8 | +// originally from http://jsonpatch.com/, http://json.schemastore.org/json-patch |
| 9 | +// with fixes |
| 10 | +const 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 | + |
3 | 71 | namespace nlohmann
|
4 | 72 | {
|
5 | 73 |
|
6 | 74 | json_patch::json_patch(json &&patch)
|
7 |
| - : j_{std::move(patch)} |
| 75 | + : j_(std::move(patch)) |
8 | 76 | {
|
9 | 77 | validateJsonPatch(j_);
|
10 | 78 | }
|
11 | 79 |
|
12 | 80 | json_patch::json_patch(const json &patch)
|
13 |
| - : j_{std::move(patch)} |
| 81 | + : j_(std::move(patch)) |
14 | 82 | {
|
15 | 83 | validateJsonPatch(j_);
|
16 | 84 | }
|
17 | 85 |
|
18 |
| -json_patch &json_patch::add(std::string path, json value) |
| 86 | +json_patch &json_patch::add(const json::json_pointer &ptr, json value) |
19 | 87 | {
|
20 |
| - j_.push_back(json{{"op", "add"}, {"path", std::move(path)}, {"value", std::move(value)}}); |
| 88 | + j_.push_back(json{{"op", "add"}, {"path", ptr}, {"value", std::move(value)}}); |
21 | 89 | return *this;
|
22 | 90 | }
|
23 | 91 |
|
24 |
| -json_patch &json_patch::replace(std::string path, json value) |
| 92 | +json_patch &json_patch::replace(const json::json_pointer &ptr, json value) |
25 | 93 | {
|
26 |
| - j_.push_back(json{{"op", "replace"}, {"path", std::move(path)}, {"value", std::move(value)}}); |
| 94 | + j_.push_back(json{{"op", "replace"}, {"path", ptr}, {"value", std::move(value)}}); |
27 | 95 | return *this;
|
28 | 96 | }
|
29 | 97 |
|
30 |
| -json_patch &json_patch::remove(std::string path) |
| 98 | +json_patch &json_patch::remove(const json::json_pointer &ptr) |
31 | 99 | {
|
32 |
| - j_.push_back(json{{"op", "remove"}, {"path", std::move(path)}}); |
| 100 | + j_.push_back(json{{"op", "remove"}, {"path", ptr}}); |
33 | 101 | return *this;
|
34 | 102 | }
|
35 | 103 |
|
36 | 104 | void json_patch::validateJsonPatch(json const &patch)
|
37 | 105 | {
|
38 |
| - if (!patch.is_array()) { |
39 |
| - throw JsonPatchFormatException{"Json is not an array"}; |
40 |
| - } |
41 |
| - |
42 |
| - for (auto const &op : patch) { |
43 |
| - if (!op.is_object()) { |
44 |
| - throw JsonPatchFormatException{"Each json patch entry needs to be an op object"}; |
45 |
| - } |
46 |
| - |
47 |
| - if (!op.contains("op")) { |
48 |
| - throw JsonPatchFormatException{"Each json patch entry needs op element"}; |
49 |
| - } |
50 |
| - |
51 |
| - const auto opType = op["op"].get<std::string>(); |
52 |
| - if ((opType != "add") && (opType != "remove") && (opType != "replace")) { |
53 |
| - throw JsonPatchFormatException{std::string{"Operation "} + opType + std::string{"is invalid"}}; |
54 |
| - } |
55 |
| - |
56 |
| - if (!op.contains("path")) { |
57 |
| - throw JsonPatchFormatException{"Each json patch entry needs path element"}; |
58 |
| - } |
| 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); |
59 | 108 |
|
60 |
| - try { |
61 |
| - // try parse to path |
62 |
| - [[maybe_unused]] const auto p = json::json_pointer{op["path"].get<std::string>()}; |
63 |
| - } catch (json::exception &e) { |
64 |
| - throw JsonPatchFormatException{e.what()}; |
65 |
| - } |
| 109 | + patch_validator.validate(patch); |
66 | 110 |
|
67 |
| - if (opType != "remove") { |
68 |
| - if (!op.contains("value")) { |
69 |
| - throw JsonPatchFormatException{"Remove and replace needs value element"}; |
70 |
| - } |
71 |
| - } |
72 |
| - } |
| 111 | + for (auto const &op : patch) |
| 112 | + json::json_pointer(op["path"].get<std::string>()); |
73 | 113 | }
|
74 | 114 |
|
75 | 115 | } // namespace nlohmann
|
0 commit comments