Open Lighting Architecture: include/ola/web/JsonParser.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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
28
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
38namespace ola {
39namespace web {
40
41class JsonArray;
42class JsonObject;
43class JsonValue;
44
49
57 class JsonParser : public JsonParserInterface {
58 public:
59 JsonParser() : JsonParserInterface() {}
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);
69 void Number(const JsonDouble::DoubleRepresentation &rep);
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
96 JsonValue *ClaimRoot();
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 JsonParser(const JsonParser &) = delete;
124 const JsonParser &operator=(const JsonParser &) = delete;
125};
126
127} // namespace web
128} // namespace ola
129#endif // INCLUDE_OLA_WEB_JSONPARSER_H_
JsonLexer.h
The class used to parse JSON data.
Macro.h
Helper macros.
ola::web::JsonArray
An array of JSON values. Arrays in JSON can contain values of different types.
Definition Json.h:1043
ola::web::JsonObject
A JSON object. JSON Objects are key : value mappings, similar to dictionaries in Python.
Definition Json.h:899
ola::web::JsonParser::CloseArray
void CloseArray()
Called when an array completes.
Definition JsonParser.cpp:115
ola::web::JsonParser::ClaimRoot
JsonValue * ClaimRoot()
Get the root of the parse tree, or NULL if parsing failed.
Definition JsonParser.cpp:174
ola::web::JsonParser::ObjectKey
void ObjectKey(const std::string &key)
Called when a new key is encountered.
Definition JsonParser.cpp:143
ola::web::JsonParser::CloseObject
void CloseObject()
Called when an object completes.
Definition JsonParser.cpp:150
ola::web::JsonParser::GetRoot
JsonValue * GetRoot()
Get the root of the parse tree, or NULL if parsing failed.
Definition JsonParser.cpp:170
ola::web::JsonParser::Begin
void Begin()
Called when parsing begins.
Definition JsonParser.cpp:36
ola::web::JsonParser::OpenArray
void OpenArray()
Called when an array starts.
Definition JsonParser.cpp:99
ola::web::JsonParser::SetError
void SetError(const std::string &error)
Can be called at any time to indicate an error with the input data.
Definition JsonParser.cpp:162
ola::web::JsonParser::OpenObject
void OpenObject()
Called when an object starts.
Definition JsonParser.cpp:127
ola::web::JsonParser::End
void End()
Called when parsing completes.
Definition JsonParser.cpp:46
ola::web::JsonParser::Bool
void Bool(bool value)
Called when a bool is encountered.
Definition JsonParser.cpp:91
ola::web::JsonParser::Parse
static JsonValue * Parse(const std::string &input, std::string *error)
Parse text and return a JsonValue representation.
Definition JsonParser.cpp:210
ola::web::JsonParser::String
void String(const std::string &value)
Called when a string is encountered.
Definition JsonParser.cpp:63
ola::web::JsonParser::Null
void Null()
Called when a null token is encountered.
Definition JsonParser.cpp:95
ola::web::JsonParser::Number
void Number(uint32_t value)
Called when a uint32_t is encountered.
Definition JsonParser.cpp:67
ola::web::JsonParser::GetError
std::string GetError() const
Check if parsing was successful.
Definition JsonParser.cpp:166
ola::web::JsonValue
The base class for JSON values.
Definition Json.h:119
ola::web
Classes to deal with web services.
Definition Json.cpp:29
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
ola::web::JsonDouble::DoubleRepresentation
Represents a JSON double value broken down as separate components.
Definition Json.h:666