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

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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
28
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 <ola/web/OptionalItem.h>
37#include <memory>
38#include <stack>
39#include <string>
40
41namespace ola {
42namespace web {
43
48
52 class JsonPatchParser : public JsonParserInterface {
53 public:
54 explicit JsonPatchParser(JsonPatchSet *patch_set)
55 : JsonParserInterface(),
56 m_patch_set(patch_set),
57 m_parser_depth(0),
58 m_state(TOP) {
59 }
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
89 bool IsValid() const;
90
97 static bool Parse(const std::string &input,
98 JsonPatchSet *patch_set,
99 std::string *error);
100
101 private:
102 enum State {
103 TOP,
104 PATCH_LIST,
105 PATCH,
106 VALUE,
107 };
108
109 std::string m_error;
110 JsonPatchSet *m_patch_set;
111 std::string m_key;
112
113 JsonParser m_parser;
114 unsigned int m_parser_depth;
115 State m_state;
116
117 // Patch members;
118 std::string m_op;
119 OptionalItem<std::string> m_path;
120 OptionalItem<std::string> m_from;
121 std::auto_ptr<JsonValue> m_value;
122
123 template <typename T>
124 void HandleNumber(const T &value);
125
126 void HandlePatchString(const std::string &value);
127 void HandlePatch();
128
129 static const char kFromKey[];
130 static const char kMissingFrom[];
131 static const char kMissingPath[];
132 static const char kMissingValue[];
133 static const char kOpKey[];
134 static const char kPatchElementError[];
135 static const char kPatchListError[];
136 static const char kPathKey[];
137 static const char kValueKey[];
138 static const char kAddOp[];
139 static const char kRemoveOp[];
140 static const char kReplaceOp[];
141 static const char kMoveOp[];
142 static const char kCopyOp[];
143 static const char kTestOp[];
144
145 JsonPatchParser(const JsonPatchParser &) = delete;
146 const JsonPatchParser &operator=(const JsonPatchParser &) = delete;
147};
148
149} // namespace web
150} // namespace ola
151#endif // INCLUDE_OLA_WEB_JSONPATCHPARSER_H_
JsonLexer.h
The class used to parse JSON data.
JsonParser.h
A JsonParserInterface implementation that builds a parse tree.
JsonPatch.h
Implementation of JSON Patch (RFC 6902).
Macro.h
Helper macros.
ola::web::JsonParser
A JsonParserInterface implementation that builds a tree of JsonValues.
Definition JsonParser.h:57
ola::web::JsonPatchParser::GetError
std::string GetError() const
Check if parsing was successful.
Definition JsonPatchParser.cpp:253
ola::web::JsonPatchParser::String
void String(const std::string &value)
Called when a string is encountered.
Definition JsonPatchParser.cpp:72
ola::web::JsonPatchParser::Bool
void Bool(bool value)
Called when a bool is encountered.
Definition JsonPatchParser.cpp:113
ola::web::JsonPatchParser::OpenArray
void OpenArray()
Called when an array starts.
Definition JsonPatchParser.cpp:151
ola::web::JsonPatchParser::OpenObject
void OpenObject()
Called when an object starts.
Definition JsonPatchParser.cpp:192
ola::web::JsonPatchParser::Number
void Number(uint32_t value)
Called when a uint32_t is encountered.
Definition JsonPatchParser.cpp:89
ola::web::JsonPatchParser::End
void End()
Called when parsing completes.
Definition JsonPatchParser.cpp:66
ola::web::JsonPatchParser::CloseObject
void CloseObject()
Called when an object completes.
Definition JsonPatchParser.cpp:224
ola::web::JsonPatchParser::IsValid
bool IsValid() const
Check if this patch document was valid.
Definition JsonPatchParser.cpp:257
ola::web::JsonPatchParser::Begin
void Begin()
Called when parsing begins.
Definition JsonPatchParser.cpp:58
ola::web::JsonPatchParser::Null
void Null()
Called when a null token is encountered.
Definition JsonPatchParser.cpp:132
ola::web::JsonPatchParser::Parse
static bool Parse(const std::string &input, JsonPatchSet *patch_set, std::string *error)
Build a JsonPatchSet from a JSON document.
Definition JsonPatchParser.cpp:343
ola::web::JsonPatchParser::SetError
void SetError(const std::string &error)
Can be called at any time to indicate an error with the input data.
Definition JsonPatchParser.cpp:247
ola::web::JsonPatchParser::CloseArray
void CloseArray()
Called when an array completes.
Definition JsonPatchParser.cpp:171
ola::web::JsonPatchParser::ObjectKey
void ObjectKey(const std::string &key)
Called when a new key is encountered.
Definition JsonPatchParser.cpp:216
ola::web::JsonPatchSet
An ordered collection of JsonPatchOps.
Definition JsonPatch.h:211
ola::web::OptionalItem
Definition OptionalItem.h:31
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