Open Lighting Architecture  Latest Git
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 
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 
39 namespace ola {
40 namespace web {
41 
47 class 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 
87 };
88 
93  public:
98  explicit JsonPatchRemoveOp(const JsonPointer &path)
99  : m_pointer(path) {
100  }
101 
102  bool Apply(JsonValue **value) const;
103 
104  private:
105  const JsonPointer m_pointer;
106 
108 };
109 
114  public:
120  JsonPatchReplaceOp(const JsonPointer &path, const JsonValue *value)
121  : m_pointer(path),
122  m_value(value) {
123  }
124 
125  bool Apply(JsonValue **value) const;
126 
127  private:
128  const JsonPointer m_pointer;
129  std::auto_ptr<const JsonValue> m_value;
130 
132 };
133 
137 class JsonPatchMoveOp : public JsonPatchOp {
138  public:
144  JsonPatchMoveOp(const JsonPointer &from, const JsonPointer &to)
145  : m_from(from),
146  m_to(to) {
147  }
148 
149  bool Apply(JsonValue **value) const;
150 
151  private:
152  JsonPointer m_from;
153  JsonPointer m_to;
154 
156 };
157 
161 class JsonPatchCopyOp : public JsonPatchOp {
162  public:
168  JsonPatchCopyOp(const JsonPointer &from, const JsonPointer &to)
169  : m_from(from),
170  m_to(to) {
171  }
172 
173  bool Apply(JsonValue **value) const;
174 
175  private:
176  JsonPointer m_from;
177  JsonPointer m_to;
178 
180 };
181 
185 class JsonPatchTestOp : public JsonPatchOp {
186  public:
187  JsonPatchTestOp(const JsonPointer &path, const JsonValue *value)
188  : m_pointer(path),
189  m_value(value) {
190  }
191 
192  bool Apply(JsonValue **value) const;
193 
194  private:
195  JsonPointer m_pointer;
196  std::auto_ptr<const JsonValue> m_value;
197 
199 };
200 
201 
206  public:
207  JsonPatchSet() {}
208  ~JsonPatchSet();
209 
214  void AddOp(JsonPatchOp *op);
215 
221  bool Apply(JsonValue **value) const;
222 
223  bool Empty() const { return m_patch_ops.empty(); }
224 
225  private:
226  typedef std::vector<JsonPatchOp*> PatchOps;
227 
228  PatchOps m_patch_ops;
229 };
231 } // namespace web
232 } // namespace ola
233 #endif // INCLUDE_OLA_WEB_JSONPATCH_H_
The base class for JSON values.
Definition: Json.h:119
A class to serialize a JSONValue to text.
Definition: JsonPatch.h:52
Add a JsonValue.
Definition: JsonPatch.h:68
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Remove the value at the specified path.
Definition: JsonPatch.h:92
Move a value from one location to another.
Definition: JsonPatch.h:137
JsonPatchAddOp(const JsonPointer &path, const JsonValue *value)
Add the JsonValue to the specified path.
Definition: JsonPatch.h:75
JsonPatchReplaceOp(const JsonPointer &path, const JsonValue *value)
Replace the JsonValue at the specified path.
Definition: JsonPatch.h:120
JsonPatchMoveOp(const JsonPointer &from, const JsonPointer &to)
Move a value from one location to another.
Definition: JsonPatch.h:144
Replace the value at the specified path.
Definition: JsonPatch.h:113
Copy a value from one location to another.
Definition: JsonPatch.h:161
JsonPatchRemoveOp(const JsonPointer &path)
Add the JsonValue to the specified path.
Definition: JsonPatch.h:98
virtual bool Apply(JsonValue **value) const =0
Apply the patch operation to the value.
JsonPatchCopyOp(const JsonPointer &from, const JsonPointer &to)
Copy a value from one location to another.
Definition: JsonPatch.h:168
An ordered collection of JsonPatchOps.
Definition: JsonPatch.h:205
A JSON pointer (RFC 6901) refers to a possible element in a JSON data structure.
Definition: JsonPointer.h:66
Helper macros.
An implementation of Json Pointers (RFC 6901).
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Basic data types used to represent elements in a JSON document.
Test a path matches the specified value.
Definition: JsonPatch.h:185