Open Lighting Architecture  0.9.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 <memory>
37 #include <stack>
38 #include <string>
39 
40 #include "common/web/OptionalItem.h"
41 
42 namespace ola {
43 namespace web {
44 
54  public:
55  explicit JsonPatchParser(JsonPatchSet *patch_set)
57  m_patch_set(patch_set),
58  m_parser_depth(0),
59  m_state(TOP) {
60  }
61 
62  void Begin();
63  void End();
64 
65  void String(const std::string &value);
66  void Number(uint32_t value);
67  void Number(int32_t value);
68  void Number(uint64_t value);
69  void Number(int64_t value);
71  void Number(double value);
72  void Bool(bool value);
73  void Null();
74  void OpenArray();
75  void CloseArray();
76  void OpenObject();
77  void ObjectKey(const std::string &key);
78  void CloseObject();
79 
80  void SetError(const std::string &error);
81 
85  std::string GetError() const;
86 
90  bool IsValid() const;
91 
98  static bool Parse(const std::string &input,
99  JsonPatchSet *patch_set,
100  std::string *error);
101 
102  private:
103  enum State {
104  TOP,
105  PATCH_LIST,
106  PATCH,
107  VALUE,
108  };
109 
110  std::string m_error;
111  JsonPatchSet *m_patch_set;
112  std::string m_key;
113 
114  JsonParser m_parser;
115  unsigned int m_parser_depth;
116  State m_state;
117 
118  // Patch members;
119  std::string m_op;
122  std::auto_ptr<JsonValue> m_value;
123 
124  template <typename T>
125  void HandleNumber(const T &value);
126 
127  void HandlePatchString(const std::string &value);
128  void HandlePatch();
129 
130  static const char kFromKey[];
131  static const char kMissingFrom[];
132  static const char kMissingPath[];
133  static const char kMissingValue[];
134  static const char kOpKey[];
135  static const char kPatchElementError[];
136  static const char kPatchListError[];
137  static const char kPathKey[];
138  static const char kValueKey[];
139  static const char kAddOp[];
140  static const char kRemoveOp[];
141  static const char kReplaceOp[];
142  static const char kMoveOp[];
143  static const char kCopyOp[];
144  static const char kTestOp[];
145 
146  DISALLOW_COPY_AND_ASSIGN(JsonPatchParser);
147 };
149 } // namespace web
150 } // namespace ola
151 #endif // INCLUDE_OLA_WEB_JSONPATCHPARSER_H_