Open Lighting Architecture: include/ola/web/JsonPatch.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
JsonPatch.h
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * JsonPatch.h
17 * Implementation of RFC 6902.
18 * Copyright (C) 2014 Simon Newton
19 */
20
28
29#ifndef INCLUDE_OLA_WEB_JSONPATCH_H_
30#define INCLUDE_OLA_WEB_JSONPATCH_H_
31
32#include <ola/base/Macro.h>
33#include <ola/web/Json.h>
34#include <ola/web/JsonPointer.h>
35#include <memory>
36#include <string>
37#include <vector>
38
39namespace ola {
40namespace web {
41
46
47class JsonPatchSet;
48
52 class JsonPatchOp {
53 public:
54 virtual ~JsonPatchOp() {}
55
62 virtual bool Apply(JsonValue **value) const = 0;
63};
64
68 class JsonPatchAddOp : public JsonPatchOp {
69 public:
75 JsonPatchAddOp(const JsonPointer &path, const JsonValue *value)
76 : m_pointer(path),
77 m_value(value) {
78 }
79
80 bool Apply(JsonValue **value) const;
81
82 private:
83 JsonPointer m_pointer;
84 std::auto_ptr<const JsonValue> m_value;
85
86 JsonPatchAddOp(const JsonPatchAddOp &) = delete;
87 const JsonPatchAddOp &operator=(const JsonPatchAddOp &) = delete;
88};
89
93 class JsonPatchRemoveOp : public JsonPatchOp {
94 public:
99 explicit JsonPatchRemoveOp(const JsonPointer &path)
100 : m_pointer(path) {
101 }
102
103 bool Apply(JsonValue **value) const;
104
105 private:
106 const JsonPointer m_pointer;
107
108 JsonPatchRemoveOp(const JsonPatchRemoveOp &) = delete;
109 const JsonPatchRemoveOp &operator=(const JsonPatchRemoveOp &) = delete;
110};
111
115 class JsonPatchReplaceOp : public JsonPatchOp {
116 public:
122 JsonPatchReplaceOp(const JsonPointer &path, const JsonValue *value)
123 : m_pointer(path),
124 m_value(value) {
125 }
126
127 bool Apply(JsonValue **value) const;
128
129 private:
130 const JsonPointer m_pointer;
131 std::auto_ptr<const JsonValue> m_value;
132
133 JsonPatchReplaceOp(const JsonPatchReplaceOp &) = delete;
134 const JsonPatchReplaceOp &operator=(const JsonPatchReplaceOp &) = delete;
135};
136
140 class JsonPatchMoveOp : public JsonPatchOp {
141 public:
147 JsonPatchMoveOp(const JsonPointer &from, const JsonPointer &to)
148 : m_from(from),
149 m_to(to) {
150 }
151
152 bool Apply(JsonValue **value) const;
153
154 private:
155 JsonPointer m_from;
156 JsonPointer m_to;
157
158 JsonPatchMoveOp(const JsonPatchMoveOp &) = delete;
159 const JsonPatchMoveOp &operator=(const JsonPatchMoveOp &) = delete;
160};
161
165 class JsonPatchCopyOp : public JsonPatchOp {
166 public:
172 JsonPatchCopyOp(const JsonPointer &from, const JsonPointer &to)
173 : m_from(from),
174 m_to(to) {
175 }
176
177 bool Apply(JsonValue **value) const;
178
179 private:
180 JsonPointer m_from;
181 JsonPointer m_to;
182
183 JsonPatchCopyOp(const JsonPatchCopyOp &) = delete;
184 const JsonPatchCopyOp &operator=(const JsonPatchCopyOp &) = delete;
185};
186
190 class JsonPatchTestOp : public JsonPatchOp {
191 public:
192 JsonPatchTestOp(const JsonPointer &path, const JsonValue *value)
193 : m_pointer(path),
194 m_value(value) {
195 }
196
197 bool Apply(JsonValue **value) const;
198
199 private:
200 JsonPointer m_pointer;
201 std::auto_ptr<const JsonValue> m_value;
202
203 JsonPatchTestOp(const JsonPatchTestOp &) = delete;
204 const JsonPatchTestOp &operator=(const JsonPatchTestOp &) = delete;
205};
206
207
211 class JsonPatchSet {
212 public:
213 JsonPatchSet() {}
214 ~JsonPatchSet();
215
220 void AddOp(JsonPatchOp *op);
221
227 bool Apply(JsonValue **value) const;
228
229 bool Empty() const { return m_patch_ops.empty(); }
230
231 private:
232 typedef std::vector<JsonPatchOp*> PatchOps;
233
234 PatchOps m_patch_ops;
235};
236
237} // namespace web
238} // namespace ola
239#endif // INCLUDE_OLA_WEB_JSONPATCH_H_
Json.h
Basic data types used to represent elements in a JSON document.
JsonPointer.h
An implementation of Json Pointers (RFC 6901).
Macro.h
Helper macros.
ola::web::JsonPatchAddOp
Add a JsonValue.
Definition JsonPatch.h:68
ola::web::JsonPatchAddOp::JsonPatchAddOp
JsonPatchAddOp(const JsonPointer &path, const JsonValue *value)
Add the JsonValue to the specified path.
Definition JsonPatch.h:75
ola::web::JsonPatchAddOp::Apply
bool Apply(JsonValue **value) const
Apply the patch operation to the value.
Definition JsonPatch.cpp:189
ola::web::JsonPatchCopyOp
Copy a value from one location to another.
Definition JsonPatch.h:165
ola::web::JsonPatchCopyOp::Apply
bool Apply(JsonValue **value) const
Apply the patch operation to the value.
Definition JsonPatch.cpp:273
ola::web::JsonPatchCopyOp::JsonPatchCopyOp
JsonPatchCopyOp(const JsonPointer &from, const JsonPointer &to)
Copy a value from one location to another.
Definition JsonPatch.h:172
ola::web::JsonPatchMoveOp
Move a value from one location to another.
Definition JsonPatch.h:140
ola::web::JsonPatchMoveOp::Apply
bool Apply(JsonValue **value) const
Apply the patch operation to the value.
Definition JsonPatch.cpp:232
ola::web::JsonPatchMoveOp::JsonPatchMoveOp
JsonPatchMoveOp(const JsonPointer &from, const JsonPointer &to)
Move a value from one location to another.
Definition JsonPatch.h:147
ola::web::JsonPatchOp
A class to serialize a JSONValue to text.
Definition JsonPatch.h:52
ola::web::JsonPatchOp::Apply
virtual bool Apply(JsonValue **value) const =0
Apply the patch operation to the value.
ola::web::JsonPatchRemoveOp
Remove the value at the specified path.
Definition JsonPatch.h:93
ola::web::JsonPatchRemoveOp::Apply
bool Apply(JsonValue **value) const
Apply the patch operation to the value.
Definition JsonPatch.cpp:193
ola::web::JsonPatchRemoveOp::JsonPatchRemoveOp
JsonPatchRemoveOp(const JsonPointer &path)
Add the JsonValue to the specified path.
Definition JsonPatch.h:99
ola::web::JsonPatchReplaceOp
Replace the value at the specified path.
Definition JsonPatch.h:115
ola::web::JsonPatchReplaceOp::JsonPatchReplaceOp
JsonPatchReplaceOp(const JsonPointer &path, const JsonValue *value)
Replace the JsonValue at the specified path.
Definition JsonPatch.h:122
ola::web::JsonPatchReplaceOp::Apply
bool Apply(JsonValue **value) const
Apply the patch operation to the value.
Definition JsonPatch.cpp:212
ola::web::JsonPatchSet
An ordered collection of JsonPatchOps.
Definition JsonPatch.h:211
ola::web::JsonPatchSet::AddOp
void AddOp(JsonPatchOp *op)
Add a patch operation to the set.
Definition JsonPatch.cpp:321
ola::web::JsonPatchSet::Apply
bool Apply(JsonValue **value) const
Apply this patch set to a value.
Definition JsonPatch.cpp:325
ola::web::JsonPatchTestOp::Apply
bool Apply(JsonValue **value) const
Apply the patch operation to the value.
Definition JsonPatch.cpp:301
ola::web::JsonPointer
A JSON pointer (RFC 6901) refers to a possible element in a JSON data structure.
Definition JsonPointer.h:66
ola::web::JsonValue
The base class for JSON values.
Definition Json.h:119
ola::web
Classes to deal with web services.
Definition Json.cpp:29
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44