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

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
JsonPointer.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 * JsonPointer.h
17 * An implementation of RFC 6901.
18 * Copyright (C) 2014 Simon Newton
19 */
20
27
28#ifndef INCLUDE_OLA_WEB_JSONPOINTER_H_
29#define INCLUDE_OLA_WEB_JSONPOINTER_H_
30
31#include <ola/base/Macro.h>
32#include <sstream>
33#include <string>
34#include <vector>
35
36namespace ola {
37namespace web {
38
66 class JsonPointer {
67 public:
74 class Iterator {
75 public:
76 explicit Iterator(const JsonPointer *pointer)
77 : m_pointer(pointer),
78 m_index(0) {
79 }
80
87 bool IsValid() const {
88 return m_index < m_pointer->TokenCount();
89 }
90
94 bool AtEnd() const {
95 return m_index + 1 == m_pointer->TokenCount();
96 }
97
101 Iterator& operator++() {
102 m_index++;
103 return *this;
104 }
105
109 Iterator& operator++(int) {
110 m_index++;
111 return *this;
112 }
113
117 std::string operator*() const {
118 return m_pointer->TokenAt(m_index);
119 }
120
121 private:
122 const JsonPointer *m_pointer;
123 unsigned int m_index;
124 };
125
126 JsonPointer();
127 JsonPointer(const JsonPointer &other);
128
134 explicit JsonPointer(const std::string &path);
135
139 bool operator==(const JsonPointer &other) const;
140
147 bool IsValid() const { return m_is_valid; }
148
152 Iterator begin() const {
153 return Iterator(this);
154 }
155
162 unsigned int TokenCount() const {
163 return m_tokens.size() + 1;
164 }
165
171 std::string TokenAt(unsigned int i) const {
172 if (i >= m_tokens.size()) {
173 return "";
174 } else {
175 return m_tokens[i];
176 }
177 }
178
183 void Push(const std::string &token);
184
188 void Pop();
189
193 std::string ToString() const;
194
200 bool IsPrefixOf(const JsonPointer &other) const;
201
202 private:
203 typedef std::vector<std::string> Tokens;
204
205 bool m_is_valid;
206 Tokens m_tokens;
207
208 static std::string EscapeString(const std::string &input);
209 static std::string UnEscapeString(const std::string &input);
210
211 void operator=(const JsonPointer&);
212};
213} // namespace web
214} // namespace ola
216#endif // INCLUDE_OLA_WEB_JSONPOINTER_H_
Macro.h
Helper macros.
ola::web::JsonPointer::Iterator
An iterator for traversing a JsonPointer.
Definition JsonPointer.h:74
ola::web::JsonPointer::Iterator::AtEnd
bool AtEnd() const
Check if the iterator is pointing to the last token.
Definition JsonPointer.h:94
ola::web::JsonPointer::Iterator::operator++
Iterator & operator++()
Move the iterator to the next token in the pointer.
Definition JsonPointer.h:101
ola::web::JsonPointer::Iterator::operator++
Iterator & operator++(int)
Move the iterator to the next token in the pointer.
Definition JsonPointer.h:109
ola::web::JsonPointer::Iterator::IsValid
bool IsValid() const
Check if this iterator is valid.
Definition JsonPointer.h:87
ola::web::JsonPointer::Iterator::operator*
std::string operator*() const
Return the current token.
Definition JsonPointer.h:117
ola::web::JsonPointer
A JSON pointer (RFC 6901) refers to a possible element in a JSON data structure.
Definition JsonPointer.h:66
ola::web::JsonPointer::begin
Iterator begin() const
Return an iterator pointing to the first token in the JsonPointer.
Definition JsonPointer.h:152
ola::web::JsonPointer::IsValid
bool IsValid() const
Returns true if this pointer is valid.
Definition JsonPointer.h:147
ola::web::JsonPointer::Pop
void Pop()
Pop the last token from the pointer.
Definition JsonPointer.cpp:68
ola::web::JsonPointer::TokenAt
std::string TokenAt(unsigned int i) const
Return the token at the specified index.
Definition JsonPointer.h:171
ola::web::JsonPointer::Push
void Push(const std::string &token)
Append a token to the pointer path.
Definition JsonPointer.cpp:64
ola::web::JsonPointer::ToString
std::string ToString() const
Returns the string representation of the pointer.
Definition JsonPointer.cpp:74
ola::web::JsonPointer::TokenCount
unsigned int TokenCount() const
The number of tokens in the Json Pointer.
Definition JsonPointer.h:162
ola::web::JsonPointer::operator==
bool operator==(const JsonPointer &other) const
Equality operator.
Definition JsonPointer.cpp:60
ola::web::JsonPointer::IsPrefixOf
bool IsPrefixOf(const JsonPointer &other) const
Check if this pointer is a prefix of another.
Definition JsonPointer.cpp:89
ola::web
Classes to deal with web services.
Definition Json.cpp:29
ola::web::EscapeString
string EscapeString(const std::string &original)
Escape a string, returning a copy.
Definition StringUtils.cpp:296
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44