Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Json.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Json.h
17  * A simple set of classes for generating JSON.
18  * See http://www.json.org/
19  * Copyright (C) 2012 Simon Newton
20  */
21 
48 #ifndef INCLUDE_OLA_WEB_JSON_H_
49 #define INCLUDE_OLA_WEB_JSON_H_
50 
51 #include <ola/StringUtils.h>
52 #include <ola/base/Macro.h>
53 #include <map>
54 #include <ostream>
55 #include <sstream>
56 #include <string>
57 #include <vector>
58 
59 namespace ola {
60 namespace web {
61 
70 class JsonValue {
71  public:
72  virtual ~JsonValue() {}
73 
79  virtual void ToString(std::ostream *output, unsigned int indent) const = 0;
80 
81  protected:
87  void Indent(std::ostream *output, unsigned int indent) const {
88  *output << std::string(indent, ' ');
89  }
90 
94  static const unsigned int DEFAULT_INDENT = 2;
95 };
96 
97 
101 class JsonStringValue: public JsonValue {
102  public:
107  explicit JsonStringValue(const std::string &value)
108  : m_value(value) {
109  }
110 
111  void ToString(std::ostream *output, unsigned int) const {
112  *output << '"' << EscapeString(EncodeString(m_value)) << '"';
113  }
114 
115  private:
116  const std::string m_value;
117 };
118 
119 
123 class JsonUIntValue: public JsonValue {
124  public:
129  explicit JsonUIntValue(unsigned int value)
130  : m_value(value) {
131  }
132 
133  void ToString(std::ostream *output, unsigned int) const {
134  *output << m_value;
135  }
136 
137  private:
138  const unsigned int m_value;
139 };
140 
141 
145 class JsonIntValue: public JsonValue {
146  public:
151  explicit JsonIntValue(int value)
152  : m_value(value) {
153  }
154 
155  void ToString(std::ostream *output, unsigned int) const {
156  *output << m_value;
157  }
158 
159  private:
160  const int m_value;
161 };
162 
163 
167 class JsonBoolValue: public JsonValue {
168  public:
173  explicit JsonBoolValue(bool value)
174  : m_value(value) {
175  }
176 
177  void ToString(std::ostream *output, unsigned int) const {
178  *output << (m_value ? "true" : "false");
179  }
180 
181  private:
182  const bool m_value;
183 };
184 
185 
189 class JsonNullValue: public JsonValue {
190  public:
194  explicit JsonNullValue() {}
195 
196  void ToString(std::ostream *output, unsigned int) const {
197  *output << "null";
198  }
199 };
200 
201 
205 class JsonRawValue: public JsonValue {
206  public:
211  explicit JsonRawValue(const std::string &value)
212  : m_value(value) {
213  }
214 
215  void ToString(std::ostream *output, unsigned int) const {
216  *output << m_value;
217  }
218 
219  private:
220  const std::string m_value;
221 };
222 
223 
233 class JsonObject: public JsonValue {
234  public:
239  ~JsonObject();
240 
246  void Add(const std::string &key, const std::string &value);
247 
253  void Add(const std::string &key, const char *value);
254 
260  void Add(const std::string &key, unsigned int i);
261 
267  void Add(const std::string &key, int i);
268 
274  void Add(const std::string &key, bool value);
275 
280  void Add(const std::string &key);
281 
287  JsonObject* AddObject(const std::string &key);
288 
294  class JsonArray* AddArray(const std::string &key);
295 
301  void AddRaw(const std::string &key, const std::string &value);
302 
303  void ToString(std::ostream *output, unsigned int indent) const;
304 
305  private:
306  typedef std::map<std::string, JsonValue*> MemberMap;
307  MemberMap m_members;
308 
310 };
311 
312 
317 class JsonArray: public JsonValue {
318  public:
319  JsonArray() : m_complex_type(false) {}
320  ~JsonArray();
321 
326  void Append(const std::string &value) {
327  m_values.push_back(new JsonStringValue(value));
328  }
329 
334  void Append(const char *value) {
335  m_values.push_back(new JsonStringValue(value));
336  }
337 
342  void Append(unsigned int i) {
343  m_values.push_back(new JsonUIntValue(i));
344  }
345 
350  void Append(int i) {
351  m_values.push_back(new JsonIntValue(i));
352  }
353 
358  void Append(bool value) {
359  m_values.push_back(new JsonBoolValue(value));
360  }
361 
365  void Append() {
366  m_values.push_back(new JsonNullValue());
367  }
368 
375  JsonObject *obj = new JsonObject();
376  m_values.push_back(obj);
377  m_complex_type = true;
378  return obj;
379  }
380 
387  JsonArray *array = new JsonArray();
388  m_values.push_back(array);
389  m_complex_type = true;
390  return array;
391  }
392 
396  void AppendRaw(const std::string &value) {
397  m_values.push_back(new JsonRawValue(value));
398  }
399 
400  void ToString(std::ostream *output, unsigned int indent) const;
401 
402  private:
403  typedef std::vector<JsonValue*> ValuesVector;
404  ValuesVector m_values;
405  // true if this array contains a nested object or array
406  bool m_complex_type;
407 
408  DISALLOW_COPY_AND_ASSIGN(JsonArray);
409 };
410 
414 class JsonWriter {
415  public:
421  static void Write(std::ostream *output, const JsonValue &value);
422 
427  static std::string AsString(const JsonValue &value);
428 };
430 } // namespace web
431 } // namespace ola
432 #endif // INCLUDE_OLA_WEB_JSON_H_