Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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_
JsonValue * ClaimRoot()
Get the root of the parse tree, or NULL if parsing failed.
Definition: JsonParser.cpp:174
The base class for JSON values.
Definition: Json.h:119
Represents a JSON double value broken down as separate components.
Definition: Json.h:661
The interface used to handle tokens during JSON parsing.
Definition: JsonLexer.h:71
The class used to parse JSON data.
void ObjectKey(const std::string &key)
Called when a new key is encountered.
Definition: JsonParser.cpp:143
void String(const std::string &value)
Called when a string is encountered.
Definition: JsonParser.cpp:63
void Bool(bool value)
Called when a bool is encountered.
Definition: JsonParser.cpp:91
JsonValue * GetRoot()
Get the root of the parse tree, or NULL if parsing failed.
Definition: JsonParser.cpp:170
A JsonParserInterface implementation that builds a tree of JsonValues.
Definition: JsonParser.h:57
void CloseObject()
Called when an object completes.
Definition: JsonParser.cpp:150
void OpenObject()
Called when an object starts.
Definition: JsonParser.cpp:127
void Null()
Called when a null token is encountered.
Definition: JsonParser.cpp:95
void Begin()
Called when parsing begins.
Definition: JsonParser.cpp:36
void Number(uint32_t value)
Called when a uint32_t is encountered.
Definition: JsonParser.cpp:67
void SetError(const std::string &error)
Can be called at any time to indicate an error with the input data.
Definition: JsonParser.cpp:162
Helper macros.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
void CloseArray()
Called when an array completes.
Definition: JsonParser.cpp:115
void End()
Called when parsing completes.
Definition: JsonParser.cpp:46
static JsonValue * Parse(const std::string &input, std::string *error)
Parse text and return a JsonValue representation.
Definition: JsonParser.cpp:210
void OpenArray()
Called when an array starts.
Definition: JsonParser.cpp:99
std::string GetError() const
Check if parsing was successful.
Definition: JsonParser.cpp:166