Open Lighting Architecture  0.9.3
 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 
59  void Begin();
60  void End();
61 
62  void String(const std::string &value);
63  void Number(uint32_t value);
64  void Number(int32_t value);
65  void Number(uint64_t value);
66  void Number(int64_t value);
68  void Number(double value);
69  void Bool(bool value);
70  void Null();
71  void OpenArray();
72  void CloseArray();
73  void OpenObject();
74  void ObjectKey(const std::string &key);
75  void CloseObject();
76 
77  void SetError(const std::string &error);
78 
82  std::string GetError() const;
83 
87  bool IsValid() const;
88 
95  static bool Parse(const std::string &input,
96  JsonPatchSet *patch_set,
97  std::string *error);
98 
99  private:
100  enum State {
101  TOP,
102  PATCH_LIST,
103  PATCH,
104  VALUE,
105  };
106 
107  std::string m_error;
108  JsonPatchSet *m_patch_set;
109  std::string m_key;
110 
111  JsonParser m_parser;
112  unsigned int m_parser_depth;
113  State m_state;
114 
115  // Patch members;
116  std::string m_op;
119  std::auto_ptr<JsonValue> m_value;
120 
121  template <typename T>
122  void HandleNumber(const T &value);
123 
124  void HandlePatchString(const std::string &value);
125  void HandlePatch();
126 
127  static const char kFromKey[];
128  static const char kMissingFrom[];
129  static const char kMissingPath[];
130  static const char kMissingValue[];
131  static const char kOpKey[];
132  static const char kPatchElementError[];
133  static const char kPatchListError[];
134  static const char kPathKey[];
135  static const char kValueKey[];
136  static const char kAddOp[];
137  static const char kRemoveOp[];
138  static const char kReplaceOp[];
139  static const char kMoveOp[];
140  static const char kCopyOp[];
141  static const char kTestOp[];
142 
143  DISALLOW_COPY_AND_ASSIGN(JsonPatchParser);
144 };
146 } // namespace web
147 } // namespace ola
148 #endif // INCLUDE_OLA_WEB_JSONPATCHPARSER_H_