Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonParser.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  * JsonParser.h
17  * A JsonParserInterface implementation that builds a parse tree.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
29 #ifndef INCLUDE_OLA_WEB_JSONPARSER_H_
30 #define INCLUDE_OLA_WEB_JSONPARSER_H_
31 
32 #include <ola/base/Macro.h>
33 #include <ola/web/JsonLexer.h>
34 #include <memory>
35 #include <stack>
36 #include <string>
37 
38 namespace ola {
39 namespace web {
40 
41 class JsonArray;
42 class JsonObject;
43 class JsonValue;
44 
58  public:
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 
90  JsonValue *GetRoot();
91 
97 
101  static JsonValue* Parse(const std::string &input,
102  std::string *error);
103 
104  private:
105  // The container type identifies the type of container (object or array)
106  // we're currently operating in. As we descend the parse tree we push types
107  // onto m_container_stack so we can back track up the tree later.
108  enum ContainerType {
109  ARRAY,
110  OBJECT,
111  };
112 
113  std::string m_error;
114  std::auto_ptr<JsonValue> m_root;
115  std::string m_key;
116  std::stack<ContainerType> m_container_stack;
117 
118  // The stacks below don't own the objects they point to.
119  std::stack<JsonArray*> m_array_stack;
120  std::stack<JsonObject*> m_object_stack;
121 
122  void AddValue(JsonValue *value);
123  DISALLOW_COPY_AND_ASSIGN(JsonParser);
124 };
126 } // namespace web
127 } // namespace ola
128 #endif // INCLUDE_OLA_WEB_JSONPARSER_H_