Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
JsonPatchParser.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  * JsonPatchParser.h
17  * Create a JsonPatchSet from a string.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
29 #ifndef INCLUDE_OLA_WEB_JSONPATCHPARSER_H_
30 #define INCLUDE_OLA_WEB_JSONPATCHPARSER_H_
31 
32 #include <ola/base/Macro.h>
33 #include <ola/web/JsonLexer.h>
34 #include <ola/web/JsonPatch.h>
35 #include <ola/web/JsonParser.h>
36 #include <ola/web/OptionalItem.h>
37 #include <memory>
38 #include <stack>
39 #include <string>
40 
41 namespace ola {
42 namespace web {
43 
53  public:
54  explicit JsonPatchParser(JsonPatchSet *patch_set)
56  m_patch_set(patch_set),
57  m_parser_depth(0),
58  m_state(TOP) {
59  }
60 
61  void Begin();
62  void End();
63 
64  void String(const std::string &value);
65  void Number(uint32_t value);
66  void Number(int32_t value);
67  void Number(uint64_t value);
68  void Number(int64_t value);
70  void Number(double value);
71  void Bool(bool value);
72  void Null();
73  void OpenArray();
74  void CloseArray();
75  void OpenObject();
76  void ObjectKey(const std::string &key);
77  void CloseObject();
78 
79  void SetError(const std::string &error);
80 
84  std::string GetError() const;
85 
89  bool IsValid() const;
90 
97  static bool Parse(const std::string &input,
98  JsonPatchSet *patch_set,
99  std::string *error);
100 
101  private:
102  enum State {
103  TOP,
104  PATCH_LIST,
105  PATCH,
106  VALUE,
107  };
108 
109  std::string m_error;
110  JsonPatchSet *m_patch_set;
111  std::string m_key;
112 
113  JsonParser m_parser;
114  unsigned int m_parser_depth;
115  State m_state;
116 
117  // Patch members;
118  std::string m_op;
121  std::auto_ptr<JsonValue> m_value;
122 
123  template <typename T>
124  void HandleNumber(const T &value);
125 
126  void HandlePatchString(const std::string &value);
127  void HandlePatch();
128 
129  static const char kFromKey[];
130  static const char kMissingFrom[];
131  static const char kMissingPath[];
132  static const char kMissingValue[];
133  static const char kOpKey[];
134  static const char kPatchElementError[];
135  static const char kPatchListError[];
136  static const char kPathKey[];
137  static const char kValueKey[];
138  static const char kAddOp[];
139  static const char kRemoveOp[];
140  static const char kReplaceOp[];
141  static const char kMoveOp[];
142  static const char kCopyOp[];
143  static const char kTestOp[];
144 
145  DISALLOW_COPY_AND_ASSIGN(JsonPatchParser);
146 };
148 } // namespace web
149 } // namespace ola
150 #endif // INCLUDE_OLA_WEB_JSONPATCHPARSER_H_
void Bool(bool value)
Called when a bool is encounted.
Definition: JsonPatchParser.cpp:113
Represents a JSON double value broken down as separate components.
Definition: Json.h:661
A JsonParserInterface implementation that builds a parse tree.
The interface used to handle tokens during JSON parsing.
Definition: JsonLexer.h:71
The class used to parse JSON data.
static bool Parse(const std::string &input, JsonPatchSet *patch_set, std::string *error)
Build a JsonPatchSet from a JSON document.
Definition: JsonPatchParser.cpp:343
void End()
Called when parsing completes.
Definition: JsonPatchParser.cpp:66
void CloseObject()
Called when an object completes.
Definition: JsonPatchParser.cpp:224
void Begin()
Called when parsing begins.
Definition: JsonPatchParser.cpp:58
void OpenArray()
Called when an array starts.
Definition: JsonPatchParser.cpp:151
Implementation of JSON Patch (RFC 6902).
void ObjectKey(const std::string &key)
Called when a new key is encounted.
Definition: JsonPatchParser.cpp:216
A JsonParserInterface implementation that builds a tree of JsonValues.
Definition: JsonParser.h:57
std::string GetError() const
Check if parsing was successful.
Definition: JsonPatchParser.cpp:253
void Number(uint32_t value)
Called when a uint32_t is encounted.
Definition: JsonPatchParser.cpp:89
void SetError(const std::string &error)
Can be called at any time to indicate an error with the input data.
Definition: JsonPatchParser.cpp:247
void CloseArray()
Called when an array completes.
Definition: JsonPatchParser.cpp:171
An ordered collection of JsonPatchOps.
Definition: JsonPatch.h:205
Helper macros.
void String(const std::string &value)
Called when a string is encounted.
Definition: JsonPatchParser.cpp:72
void Null()
Called when a null token is encounted.
Definition: JsonPatchParser.cpp:132
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool IsValid() const
Check if this patch document was valid.
Definition: JsonPatchParser.cpp:257
Parse a JSON Patch document (RFC 6902).
Definition: JsonPatchParser.h:52
void OpenObject()
Called when an object starts.
Definition: JsonPatchParser.cpp:192