Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
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 
36 namespace ola {
37 namespace 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 
102  m_index++;
103  return *this;
104  }
105 
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_
unsigned int TokenCount() const
The number of tokens in the Json Pointer.
Definition: JsonPointer.h:162
Iterator begin() const
Return an iterator pointing to the first token in the JsonPointer.
Definition: JsonPointer.h:152
void Push(const std::string &token)
Append a token to the pointer path.
Definition: JsonPointer.cpp:64
An iterator for traversing a JsonPointer.
Definition: JsonPointer.h:74
bool IsValid() const
Returns true if this pointer is valid.
Definition: JsonPointer.h:147
void Pop()
Pop the last token from the pointer.
Definition: JsonPointer.cpp:68
bool AtEnd() const
Check if the iterator is pointing to the last token.
Definition: JsonPointer.h:94
Iterator & operator++()
Move the iterator to the next token in the pointer.
Definition: JsonPointer.h:101
A JSON pointer (RFC 6901) refers to a possible element in a JSON data structure.
Definition: JsonPointer.h:66
bool IsValid() const
Check if this iterator is valid.
Definition: JsonPointer.h:87
Helper macros.
bool IsPrefixOf(const JsonPointer &other) const
Check if this pointer is a prefix of another.
Definition: JsonPointer.cpp:89
Iterator & operator++(int)
Move the iterator to the next token in the pointer.
Definition: JsonPointer.h:109
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
std::string TokenAt(unsigned int i) const
Return the token at the specifed index.
Definition: JsonPointer.h:171
bool operator==(const JsonPointer &other) const
Equality operator.
Definition: JsonPointer.cpp:60
std::string ToString() const
Returns the string representation of the pointer.
Definition: JsonPointer.cpp:74
std::string operator*() const
Return the current token.
Definition: JsonPointer.h:117