Open Lighting Architecture  Latest Git
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 
30 #ifndef INCLUDE_OLA_WEB_JSON_H_
31 #define INCLUDE_OLA_WEB_JSON_H_
32 
33 #include <ola/StringUtils.h>
34 #include <ola/base/Macro.h>
35 #include <ola/web/JsonPointer.h>
36 #include <stdint.h>
37 #include <map>
38 #include <ostream>
39 #include <sstream>
40 #include <string>
41 #include <vector>
42 
43 namespace ola {
44 namespace web {
45 
51 class JsonObjectPropertyVisitor;
52 class JsonValueConstVisitorInterface;
53 class JsonValueVisitorInterface;
54 
55 class JsonArray;
56 class JsonBool;
57 class JsonDouble;
58 class JsonInt64;
59 class JsonInt;
60 class JsonNull;
61 class JsonNumber;
62 class JsonObject;
63 class JsonRawValue;
64 class JsonString;
65 class JsonUInt64;
66 class JsonUInt;
67 
76  public:
77  virtual ~JsonValueVisitorInterface() {}
78 
79  virtual void Visit(JsonString *value) = 0;
80  virtual void Visit(JsonBool *value) = 0;
81  virtual void Visit(JsonNull *value) = 0;
82  virtual void Visit(JsonRawValue *value) = 0;
83  virtual void Visit(JsonObject *value) = 0;
84  virtual void Visit(JsonArray *value) = 0;
85  virtual void Visit(JsonUInt *value) = 0;
86  virtual void Visit(JsonUInt64 *value) = 0;
87  virtual void Visit(JsonInt *value) = 0;
88  virtual void Visit(JsonInt64 *value) = 0;
89  virtual void Visit(JsonDouble *value) = 0;
90 };
91 
100  public:
101  virtual ~JsonValueConstVisitorInterface() {}
102 
103  virtual void Visit(const JsonString &value) = 0;
104  virtual void Visit(const JsonBool &value) = 0;
105  virtual void Visit(const JsonNull &value) = 0;
106  virtual void Visit(const JsonRawValue &value) = 0;
107  virtual void Visit(const JsonObject &value) = 0;
108  virtual void Visit(const JsonArray &value) = 0;
109  virtual void Visit(const JsonUInt &value) = 0;
110  virtual void Visit(const JsonUInt64 &value) = 0;
111  virtual void Visit(const JsonInt &value) = 0;
112  virtual void Visit(const JsonInt64 &value) = 0;
113  virtual void Visit(const JsonDouble &value) = 0;
114 };
115 
119 class JsonValue {
120  public:
121  virtual ~JsonValue() {}
122 
126  virtual JsonValue* LookupElement(const JsonPointer &pointer);
127 
134  virtual bool operator==(const JsonValue &other) const = 0;
135 
139  virtual bool operator!=(const JsonValue &other) const {
140  return !(*this == other);
141  }
142 
148  virtual void Accept(JsonValueVisitorInterface *visitor) = 0;
149 
155  virtual void Accept(JsonValueConstVisitorInterface *visitor) const = 0;
156 
160  virtual JsonValue* Clone() const = 0;
161 
172  virtual JsonValue* LookupElementWithIter(JsonPointer::Iterator *iterator) = 0;
173 
178  virtual bool Equals(const JsonString &) const { return false; }
179 
184  virtual bool Equals(const JsonUInt &) const { return false; }
185 
190  virtual bool Equals(const JsonInt &) const { return false; }
191 
196  virtual bool Equals(const JsonUInt64 &) const { return false; }
197 
202  virtual bool Equals(const JsonInt64 &) const { return false; }
203 
208  virtual bool Equals(const JsonBool &) const { return false; }
209 
214  virtual bool Equals(const JsonNull &) const { return false; }
215 
220  virtual bool Equals(const JsonDouble &) const { return false; }
221 
226  virtual bool Equals(const JsonRawValue &) const { return false; }
227 
232  virtual bool Equals(const JsonObject &) const { return false; }
233 
238  virtual bool Equals(const JsonArray &) const { return false; }
239 
247  template <typename T>
248  static JsonValue* NewValue(const T &value);
249 
254  template <typename T>
255  static JsonNumber* NewNumberValue(const T &value);
256 };
257 
258 
265 class JsonLeafValue : public JsonValue {
266  public:
270  JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
274 };
275 
279 class JsonString: public JsonLeafValue {
280  public:
285  explicit JsonString(const std::string &value) : m_value(value) {}
286 
287  bool operator==(const JsonValue &other) const { return other.Equals(*this); }
288 
292  const std::string& Value() const { return m_value; }
293 
294  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
295  void Accept(JsonValueConstVisitorInterface *visitor) const {
296  visitor->Visit(*this);
297  }
298 
299  JsonValue* Clone() const { return new JsonString(m_value); }
300 
304  bool Equals(const JsonString &other) const {
305  return m_value == other.m_value;
306  }
311  private:
312  const std::string m_value;
313 
315 };
316 
317 
324 class JsonNumber : public JsonLeafValue {
325  public:
329  virtual bool MultipleOf(const JsonNumber &other) const = 0;
330 
334  virtual bool operator<(const JsonNumber &other) const = 0;
335 
339  bool operator<=(const JsonNumber &other) const {
340  return *this == other || *this < other;
341  }
342 
346  bool operator>(const JsonNumber &other) const {
347  return !(*this <= other);
348  }
349 
353  bool operator>=(const JsonNumber &other) const {
354  return !(*this < other);
355  }
356 
360  virtual int Compare(const JsonUInt &value) const = 0;
361  virtual int Compare(const JsonInt &value) const = 0;
362  virtual int Compare(const JsonUInt64 &value) const = 0;
363  virtual int Compare(const JsonInt64 &value) const = 0;
364  virtual int Compare(const JsonDouble &value) const = 0;
365 
366  virtual bool FactorOf(const JsonUInt &value) const = 0;
367  virtual bool FactorOf(const JsonInt &value) const = 0;
368  virtual bool FactorOf(const JsonUInt64 &value) const = 0;
369  virtual bool FactorOf(const JsonInt64 &value) const = 0;
370  virtual bool FactorOf(const JsonDouble &value) const = 0;
374 };
375 
379 class JsonUInt: public JsonNumber {
380  public:
385  explicit JsonUInt(unsigned int value) : m_value(value) {}
386 
387  bool operator==(const JsonValue &other) const { return other.Equals(*this); }
388 
389  bool operator<(const JsonNumber &other) const {
390  return other.Compare(*this) == 1;
391  }
392 
393  bool MultipleOf(const JsonNumber &other) const {
394  return other.FactorOf(*this);
395  }
396 
397  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
398  void Accept(JsonValueConstVisitorInterface *visitor) const {
399  visitor->Visit(*this);
400  }
401 
402  JsonValue* Clone() const { return new JsonUInt(m_value); }
403 
407  unsigned int Value() const { return m_value; }
408 
412  bool Equals(const JsonUInt &other) const {
413  return m_value == other.m_value;
414  }
415 
416  bool Equals(const JsonInt &other) const;
417  bool Equals(const JsonUInt64 &other) const;
418  bool Equals(const JsonInt64 &other) const;
419 
420  int Compare(const JsonUInt &value) const;
421  int Compare(const JsonInt &value) const;
422  int Compare(const JsonUInt64 &value) const;
423  int Compare(const JsonInt64 &value) const;
424  int Compare(const JsonDouble &value) const;
425 
426  bool FactorOf(const JsonUInt &value) const;
427  bool FactorOf(const JsonInt &value) const;
428  bool FactorOf(const JsonUInt64 &value) const;
429  bool FactorOf(const JsonInt64 &value) const;
430  bool FactorOf(const JsonDouble &value) const;
435  private:
436  const unsigned int m_value;
437 
439 };
440 
441 
445 class JsonInt: public JsonNumber {
446  public:
451  explicit JsonInt(int value) : m_value(value) {}
452 
453  bool operator==(const JsonValue &other) const { return other.Equals(*this); }
454 
455  bool operator<(const JsonNumber &other) const {
456  return other.Compare(*this) == 1;
457  }
458 
459  bool MultipleOf(const JsonNumber &other) const {
460  return other.FactorOf(*this);
461  }
462 
463  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
464  void Accept(JsonValueConstVisitorInterface *visitor) const {
465  visitor->Visit(*this);
466  }
467 
468  JsonValue* Clone() const { return new JsonInt(m_value); }
469 
473  int Value() const { return m_value; }
474 
478  bool Equals(const JsonInt &other) const {
479  return m_value == other.m_value;
480  }
481 
482  bool Equals(const JsonUInt &other) const;
483  bool Equals(const JsonUInt64 &other) const;
484  bool Equals(const JsonInt64 &other) const;
485 
486  int Compare(const JsonUInt &value) const;
487  int Compare(const JsonInt &value) const;
488  int Compare(const JsonUInt64 &value) const;
489  int Compare(const JsonInt64 &value) const;
490  int Compare(const JsonDouble &value) const;
491 
492  bool FactorOf(const JsonUInt &value) const;
493  bool FactorOf(const JsonInt &value) const;
494  bool FactorOf(const JsonUInt64 &value) const;
495  bool FactorOf(const JsonInt64 &value) const;
496  bool FactorOf(const JsonDouble &value) const;
500  private:
501  const int m_value;
502 
504 };
505 
506 
510 class JsonUInt64: public JsonNumber {
511  public:
516  explicit JsonUInt64(uint64_t value) : m_value(value) {}
517 
518  bool operator==(const JsonValue &other) const {
519  return other.Equals(*this);
520  }
521 
522  bool operator<(const JsonNumber &other) const {
523  return other.Compare(*this) == 1;
524  }
525 
526  bool MultipleOf(const JsonNumber &other) const {
527  return other.FactorOf(*this);
528  }
529 
530  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
531  void Accept(JsonValueConstVisitorInterface *visitor) const {
532  visitor->Visit(*this);
533  }
534 
535  JsonValue* Clone() const { return new JsonUInt64(m_value); }
536 
540  uint64_t Value() const { return m_value; }
541 
545  bool Equals(const JsonUInt64 &other) const {
546  return m_value == other.m_value;
547  }
548 
549  bool Equals(const JsonUInt &other) const;
550  bool Equals(const JsonInt &other) const;
551  bool Equals(const JsonInt64 &other) const;
552 
553  int Compare(const JsonUInt &value) const;
554  int Compare(const JsonInt &value) const;
555  int Compare(const JsonUInt64 &value) const;
556  int Compare(const JsonInt64 &value) const;
557  int Compare(const JsonDouble &value) const;
558 
559  bool FactorOf(const JsonUInt &value) const;
560  bool FactorOf(const JsonInt &value) const;
561  bool FactorOf(const JsonUInt64 &value) const;
562  bool FactorOf(const JsonInt64 &value) const;
563  bool FactorOf(const JsonDouble &value) const;
567  private:
568  const uint64_t m_value;
569 
571 };
572 
573 
577 class JsonInt64: public JsonNumber {
578  public:
583  explicit JsonInt64(int64_t value) : m_value(value) {}
584 
585  bool operator==(const JsonValue &other) const {
586  return other.Equals(*this);
587  }
588 
589  bool operator<(const JsonNumber &other) const {
590  return other.Compare(*this) == 1;
591  }
592 
593  bool MultipleOf(const JsonNumber &other) const {
594  return other.FactorOf(*this);
595  }
596 
597  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
598  void Accept(JsonValueConstVisitorInterface *visitor) const {
599  visitor->Visit(*this);
600  }
601 
602  JsonValue* Clone() const { return new JsonInt64(m_value); }
603 
607  int64_t Value() const { return m_value; }
608 
612  bool Equals(const JsonInt64 &other) const {
613  return m_value == other.m_value;
614  }
615 
616  bool Equals(const JsonUInt &other) const;
617  bool Equals(const JsonInt &other) const;
618  bool Equals(const JsonUInt64 &other) const;
619 
620  int Compare(const JsonUInt &value) const;
621  int Compare(const JsonInt &value) const;
622  int Compare(const JsonUInt64 &value) const;
623  int Compare(const JsonInt64 &value) const;
624  int Compare(const JsonDouble &value) const;
625 
626  bool FactorOf(const JsonUInt &value) const;
627  bool FactorOf(const JsonInt &value) const;
628  bool FactorOf(const JsonUInt64 &value) const;
629  bool FactorOf(const JsonInt64 &value) const;
630  bool FactorOf(const JsonDouble &value) const;
634  private:
635  const int64_t m_value;
636 
638 };
639 
640 
649 class JsonDouble: public JsonNumber {
650  public:
665  uint64_t full;
669  uint64_t fractional;
671  int32_t exponent;
672  };
673 
678  explicit JsonDouble(double value);
679 
683  explicit JsonDouble(const DoubleRepresentation &rep);
684 
685  bool operator==(const JsonValue &other) const {
686  return other.Equals(*this);
687  }
688 
689  bool operator<(const JsonNumber &other) const {
690  return other.Compare(*this) == 1;
691  }
692 
693  bool MultipleOf(const JsonNumber &other) const {
694  return other.FactorOf(*this);
695  }
696 
697  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
698  void Accept(JsonValueConstVisitorInterface *visitor) const {
699  visitor->Visit(*this);
700  }
701 
705  const std::string& ToString() const { return m_as_string; }
706 
712  double Value() const { return m_value; }
713 
720  static bool AsDouble(const DoubleRepresentation &rep, double *out);
721 
727  static std::string AsString(const DoubleRepresentation &rep);
728 
729  JsonValue* Clone() const {
730  // This loses precision, maybe we should fix it?
731  return new JsonDouble(m_value);
732  }
733 
737  bool Equals(const JsonDouble &other) const {
738  // This is sketchy. The standard says "have the same mathematical value"
739  // TODO(simon): Think about this some more.
740  return m_value == other.m_value;
741  }
742 
743  int Compare(const JsonUInt &value) const;
744  int Compare(const JsonInt &value) const;
745  int Compare(const JsonUInt64 &value) const;
746  int Compare(const JsonInt64 &value) const;
747  int Compare(const JsonDouble &value) const;
748 
749  bool FactorOf(const JsonUInt &value) const;
750  bool FactorOf(const JsonInt &value) const;
751  bool FactorOf(const JsonUInt64 &value) const;
752  bool FactorOf(const JsonInt64 &value) const;
753  bool FactorOf(const JsonDouble &value) const;
757  private:
758  double m_value;
759  std::string m_as_string;
760 
762 };
763 
764 
768 class JsonBool: public JsonLeafValue {
769  public:
774  explicit JsonBool(bool value) : m_value(value) {}
775 
776  bool operator==(const JsonValue &other) const {
777  return other.Equals(*this);
778  }
779 
780  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
781  void Accept(JsonValueConstVisitorInterface *visitor) const {
782  visitor->Visit(*this);
783  }
784 
785  JsonValue* Clone() const { return new JsonBool(m_value); }
786 
790  bool Value() const { return m_value; }
791 
795  bool Equals(const JsonBool &other) const {
796  return m_value == other.m_value;
797  }
801  private:
802  const bool m_value;
803 
805 };
806 
807 
811 class JsonNull: public JsonLeafValue {
812  public:
816  JsonNull() {}
817 
818  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
819  void Accept(JsonValueConstVisitorInterface *visitor) const {
820  visitor->Visit(*this);
821  }
822 
823  JsonValue* Clone() const { return new JsonNull(); }
824 
825  bool operator==(const JsonValue &other) const { return other.Equals(*this); }
826 
830  bool Equals(const JsonNull &) const { return true; }
834  private:
836 };
837 
838 
843  public:
848  explicit JsonRawValue(const std::string &value) : m_value(value) {}
849 
850  bool operator==(const JsonValue &other) const { return other.Equals(*this); }
851 
852  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
853  void Accept(JsonValueConstVisitorInterface *visitor) const {
854  visitor->Visit(*this);
855  }
856 
857  JsonValue* Clone() const { return new JsonRawValue(m_value); }
858 
862  const std::string& Value() const { return m_value; }
863 
867  bool Equals(const JsonRawValue &other) const {
868  return m_value == other.m_value;
869  }
874  private:
875  const std::string m_value;
876 
878 };
879 
880 
890 class JsonObject: public JsonValue {
891  public:
896  ~JsonObject();
897 
898  JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
899 
900  bool operator==(const JsonValue &other) const {
901  return other.Equals(*this);
902  }
903 
904  bool Equals(const JsonObject &other) const;
905 
911  void Add(const std::string &key, const std::string &value);
912 
918  void Add(const std::string &key, const char *value);
919 
925  void Add(const std::string &key, unsigned int i);
926 
932  void Add(const std::string &key, int i);
933 
939  void Add(const std::string &key, double d);
940 
946  void Add(const std::string &key, bool value);
947 
952  void Add(const std::string &key);
953 
959  JsonObject* AddObject(const std::string &key);
960 
966  class JsonArray* AddArray(const std::string &key);
967 
973  void AddValue(const std::string &key, JsonValue *value);
974 
980  void AddRaw(const std::string &key, const std::string &value);
981 
987  bool Remove(const std::string &key);
988 
997  bool ReplaceValue(const std::string &key, JsonValue *value);
998 
999  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
1001  visitor->Visit(*this);
1002  }
1003 
1004  JsonValue* Clone() const;
1005 
1010  bool IsEmpty() const { return m_members.empty(); }
1011 
1012  unsigned int Size() const { return m_members.size(); }
1013 
1019  void VisitProperties(JsonObjectPropertyVisitor *visitor) const;
1020 
1021  private:
1022  typedef std::map<std::string, JsonValue*> MemberMap;
1023  MemberMap m_members;
1024 
1026 };
1027 
1028 
1033 class JsonArray: public JsonValue {
1034  public:
1035  JsonArray() : m_complex_type(false) {}
1036  ~JsonArray();
1037 
1038  JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
1039 
1040  bool operator==(const JsonValue &other) const {
1041  return other.Equals(*this);
1042  }
1043 
1044  bool Equals(const JsonArray &other) const;
1045 
1050  void Append(const std::string &value) {
1051  m_values.push_back(new JsonString(value));
1052  }
1053 
1058  void Append(const char *value) {
1059  m_values.push_back(new JsonString(value));
1060  }
1061 
1066  void Append(unsigned int i) {
1067  m_values.push_back(new JsonUInt(i));
1068  }
1069 
1074  void Append(int i) {
1075  m_values.push_back(new JsonInt(i));
1076  }
1077 
1082  void Append(bool value) {
1083  m_values.push_back(new JsonBool(value));
1084  }
1085 
1089  void Append() {
1090  m_values.push_back(new JsonNull());
1091  }
1092 
1096  void Append(JsonValue *value) {
1097  m_values.push_back(value);
1098  }
1099 
1103  void Append(JsonObject *value) {
1104  m_values.push_back(value);
1105  m_complex_type |= !value->IsEmpty();
1106  }
1107 
1114  JsonObject *obj = new JsonObject();
1115  m_values.push_back(obj);
1116  m_complex_type = true;
1117  return obj;
1118  }
1119 
1126  JsonArray *array = new JsonArray();
1127  m_values.push_back(array);
1128  m_complex_type = true;
1129  return array;
1130  }
1131 
1136  void AppendValue(JsonValue *value) {
1137  m_values.push_back(value);
1138  }
1139 
1143  void AppendRaw(const std::string &value) {
1144  m_values.push_back(new JsonRawValue(value));
1145  }
1146 
1152  bool RemoveElementAt(uint32_t index);
1153 
1160  bool ReplaceElementAt(uint32_t index, JsonValue *value);
1161 
1170  bool InsertElementAt(uint32_t index, JsonValue *value);
1171 
1172  void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
1174  visitor->Visit(*this);
1175  }
1176 
1177  JsonValue* Clone() const;
1178 
1183  bool IsEmpty() const { return m_values.empty(); }
1184 
1188  unsigned int Size() const { return m_values.size(); }
1189 
1194  const JsonValue *ElementAt(unsigned int i) const;
1195 
1199  bool IsComplexType() const { return m_complex_type; }
1200 
1201  private:
1202  typedef std::vector<JsonValue*> ValuesVector;
1203  ValuesVector m_values;
1204 
1205  // true if this array contains a nested object or array
1206  bool m_complex_type;
1207 
1209 };
1210 
1211 
1216  public:
1217  virtual ~JsonObjectPropertyVisitor() {}
1218 
1222  virtual void VisitProperty(const std::string &property,
1223  const JsonValue &value) = 0;
1224 };
1225 
1233 
1240 JsonArray* ArrayCast(JsonValue *value);
1241 
1244 // operator<<
1245 std::ostream& operator<<(std::ostream &os, const JsonString &value);
1246 std::ostream& operator<<(std::ostream &os, const JsonUInt &value);
1247 std::ostream& operator<<(std::ostream &os, const JsonInt &value);
1248 std::ostream& operator<<(std::ostream &os, const JsonUInt64 &value);
1249 std::ostream& operator<<(std::ostream &os, const JsonInt64 &value);
1250 std::ostream& operator<<(std::ostream &os, const JsonDouble &value);
1251 std::ostream& operator<<(std::ostream &os, const JsonBool &value);
1252 std::ostream& operator<<(std::ostream &os, const JsonNull &value);
1253 std::ostream& operator<<(std::ostream &os, const JsonRawValue &value);
1254 
1255 // JsonValue::NewNumberValue implementations.
1256 template <>
1257 inline JsonNumber* JsonValue::NewNumberValue<uint32_t>(
1258  const uint32_t &value) {
1259  return new JsonUInt(value);
1260 }
1261 
1262 template <>
1263 inline JsonNumber* JsonValue::NewNumberValue<int32_t>(
1264  const int32_t &value) {
1265  return new JsonInt(value);
1266 }
1267 
1268 template <>
1269 inline JsonNumber* JsonValue::NewNumberValue<uint64_t>(
1270  const uint64_t &value) {
1271  return new JsonUInt64(value);
1272 }
1273 
1274 template <>
1275 inline JsonNumber* JsonValue::NewNumberValue<int64_t>(
1276  const int64_t &value) {
1277  return new JsonInt64(value);
1278 }
1279 
1280 template <>
1281 inline JsonNumber* JsonValue::NewNumberValue<double>(
1282  const double &value) {
1283  return new JsonDouble(value);
1284 }
1285 
1286 template <>
1287 inline JsonNumber* JsonValue::NewNumberValue<
1289  const JsonDouble::DoubleRepresentation &value) {
1290  return new JsonDouble(value);
1291 }
1292 
1293 // JsonValue::NewValue implementations.
1294 template <>
1295 inline JsonValue* JsonValue::NewValue<std::string>(const std::string &value) {
1296  return new JsonString(value);
1297 }
1298 
1299 template <>
1300 inline JsonValue* JsonValue::NewValue<uint32_t>(const uint32_t &value) {
1301  return NewNumberValue(value);
1302 }
1303 
1304 template <>
1305 inline JsonValue* JsonValue::NewValue<int32_t>(const int32_t &value) {
1306  return NewNumberValue(value);
1307 }
1308 
1309 template <>
1310 inline JsonValue* JsonValue::NewValue<uint64_t>(const uint64_t &value) {
1311  return NewNumberValue(value);
1312 }
1313 
1314 template <>
1315 inline JsonValue* JsonValue::NewValue<int64_t>(const int64_t &value) {
1316  return NewNumberValue(value);
1317 }
1318 
1319 template <>
1320 inline JsonValue* JsonValue::NewValue<double>(const double &value) {
1321  return NewNumberValue(value);
1322 }
1323 
1324 template <>
1325 inline JsonValue* JsonValue::NewValue<JsonDouble::DoubleRepresentation>(
1326  const JsonDouble::DoubleRepresentation &value) {
1327  return NewNumberValue(value);
1328 }
1329 
1330 template <>
1331 inline JsonValue* JsonValue::NewValue<bool>(const bool &value) {
1332  return new JsonBool(value);
1333 }
1335 } // namespace web
1336 } // namespace ola
1337 #endif // INCLUDE_OLA_WEB_JSON_H_
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:818
The base class for JSON values.
Definition: Json.h:119
A base class used to describe values which are leafs of a JSON tree.
Definition: Json.h:265
const std::string & ToString() const
Return the double value as a string.
Definition: Json.h:705
Represents a JSON double value broken down as separate components.
Definition: Json.h:661
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition: Json.h:593
const std::string & Value() const
Return the string value.
Definition: Json.h:292
JsonArray * AppendArray()
Append a JsonArray to the array.
Definition: Json.h:1125
bool operator<=(const JsonNumber &other) const
Less than or equals operator.
Definition: Json.h:339
bool operator<(const JsonNumber &other) const
Less than operator.
Definition: Json.h:389
A signed 64bit int value.
Definition: Json.h:577
An unsigned 32bit int value.
Definition: Json.h:379
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
A raw value, useful if you want to cheat.
Definition: Json.h:842
bool Value() const
Return the bool value.
Definition: Json.h:790
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:585
The interface for the JsonValueVisitor class.
Definition: Json.h:75
uint64_t fractional
Definition: Json.h:669
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:463
JsonRawValue(const std::string &value)
Create a new JsonRawValue.
Definition: Json.h:848
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:1173
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:729
bool operator<(const JsonNumber &other) const
Less than operator.
Definition: Json.h:522
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:1040
void Append(JsonObject *value)
Append a JsonValue. Takes ownership of the pointer.
Definition: Json.h:1103
bool IsComplexType() const
Return true if this array contains nested arrays or objects.
Definition: Json.h:1199
JsonUInt64(uint64_t value)
Create a new JsonUInt64.
Definition: Json.h:516
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:518
virtual bool operator!=(const JsonValue &other) const
Not-equals operator.
Definition: Json.h:139
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition: Json.h:693
JsonInt64(int64_t value)
Create a new JsonInt64.
Definition: Json.h:583
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:530
JsonObject * AppendObject()
Append a JsonObject to the array.
Definition: Json.h:1113
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:295
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:602
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:598
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition: Json.h:393
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:402
A class used to visit Json values within a JsonObject.
Definition: Json.h:1215
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition: Json.h:526
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:453
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:825
The null value.
Definition: Json.h:811
bool is_negative
Definition: Json.h:663
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:857
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:776
JsonObject * ObjectCast(JsonValue *value)
Downcast a JsonValue to a JsonObject.
Definition: Json.cpp:780
A JSON object. JSON Objects are key : value mappings, similar to dictionaries in Python.
Definition: Json.h:890
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:387
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:597
An iterator for traversing a JsonPointer.
Definition: JsonPointer.h:74
double Value() const
Returns the value as a double.
Definition: Json.h:712
A Bool value.
Definition: Json.h:768
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition: Json.h:459
void Append(const char *value)
Append a string value to the array.
Definition: Json.h:1058
An array of JSON values. Arrays in JSON can contain values of different types.
Definition: Json.h:1033
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:294
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:397
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:535
JsonArray * ArrayCast(JsonValue *value)
Downcast a JsonValue to a JsonArray.
Definition: Json.cpp:795
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:685
int32_t exponent
Definition: Json.h:671
void Append(int i)
Append an int value to the array.
Definition: Json.h:1074
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:698
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:697
A string value.
Definition: Json.h:279
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:999
void Append(unsigned int i)
Append an unsigned int value to the array.
Definition: Json.h:1066
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:819
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:900
JsonInt(int value)
Create a new JsonInt.
Definition: Json.h:451
An unsigned 64bit int value.
Definition: Json.h:510
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:1172
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:468
void Append(JsonValue *value)
Append a JsonValue. Takes ownership of the pointer.
Definition: Json.h:1096
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:299
Various string utility functions.
bool operator>=(const JsonNumber &other) const
Greater than or equals operator.
Definition: Json.h:353
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:850
A signed 32bit int value.
Definition: Json.h:445
void AppendValue(JsonValue *value)
Append a JsonValue to the array.
Definition: Json.h:1136
const std::string & Value() const
Return the raw value as a string.
Definition: Json.h:862
A JSON pointer (RFC 6901) refers to a possible element in a JSON data structure.
Definition: JsonPointer.h:66
void Append(bool value)
Append a bool value to the array.
Definition: Json.h:1082
Helper macros.
unsigned int Value() const
Return the uint32_t value.
Definition: Json.h:407
A double value.
Definition: Json.h:649
bool operator>(const JsonNumber &other) const
Greater than operator.
Definition: Json.h:346
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:852
void Append()
Append a null value to the array.
Definition: Json.h:1089
bool IsEmpty() const
Check if there are elements within the array.
Definition: Json.h:1183
int64_t Value() const
Return the int64_t value.
Definition: Json.h:607
uint64_t full
Definition: Json.h:665
void Append(const std::string &value)
Append a string value to the array.
Definition: Json.h:1050
JsonObject()
Create a new JsonObject.
Definition: Json.h:895
An implementation of Json Pointers (RFC 6901).
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:1000
int Value() const
Return the int32_t value.
Definition: Json.h:473
bool operator<(const JsonNumber &other) const
Less than operator.
Definition: Json.h:455
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
void AppendRaw(const std::string &value)
Append a raw value to the array.
Definition: Json.h:1143
JsonBool(bool value)
Create a new JsonBool.
Definition: Json.h:774
bool IsEmpty() const
Check if there are properties within the object.
Definition: Json.h:1010
unsigned int Size() const
Return the number of elements in the array.
Definition: Json.h:1188
JsonString(const std::string &value)
Create a new JsonString.
Definition: Json.h:285
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:531
bool operator<(const JsonNumber &other) const
Less than operator.
Definition: Json.h:589
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:398
uint64_t Value() const
Return the uint64_t value.
Definition: Json.h:540
uint32_t leading_fractional_zeros
Definition: Json.h:667
JsonNumber is the base class for various integer / number classes.
Definition: Json.h:324
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:781
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:823
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:464
The const interface for the JsonValueVisitor class.
Definition: Json.h:99
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition: Json.h:853
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition: Json.h:785
bool operator<(const JsonNumber &other) const
Less than operator.
Definition: Json.h:689
JsonUInt(unsigned int value)
Create a new JsonUInt.
Definition: Json.h:385
JsonNull()
Create a new JsonNull.
Definition: Json.h:816
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition: Json.h:780
bool operator==(const JsonValue &other) const
Equality operator.
Definition: Json.h:287