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

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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
29
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
43namespace ola {
44namespace web {
45
50
51class JsonObjectPropertyVisitor;
52class JsonValueConstVisitorInterface;
53class JsonValueVisitorInterface;
54
55class JsonArray;
56class JsonBool;
57class JsonDouble;
58class JsonInt64;
59class JsonInt;
60class JsonNull;
61class JsonNumber;
62class JsonObject;
63class JsonRawValue;
64class JsonString;
65class JsonUInt64;
66class JsonUInt;
67
75 class JsonValueVisitorInterface {
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
99 class JsonValueConstVisitorInterface {
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
165
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
243
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 }
310
311 private:
312 const std::string m_value;
313
314 JsonString(const JsonString &) = delete;
315 const JsonString &operator=(const JsonString &) = delete;
316};
317
318
325 class JsonNumber : public JsonLeafValue {
326 public:
330 virtual bool MultipleOf(const JsonNumber &other) const = 0;
331
335 virtual bool operator<(const JsonNumber &other) const = 0;
336
340 bool operator<=(const JsonNumber &other) const {
341 return *this == other || *this < other;
342 }
343
347 bool operator>(const JsonNumber &other) const {
348 return !(*this <= other);
349 }
350
354 bool operator>=(const JsonNumber &other) const {
355 return !(*this < other);
356 }
357
361 virtual int Compare(const JsonUInt &value) const = 0;
362 virtual int Compare(const JsonInt &value) const = 0;
363 virtual int Compare(const JsonUInt64 &value) const = 0;
364 virtual int Compare(const JsonInt64 &value) const = 0;
365 virtual int Compare(const JsonDouble &value) const = 0;
366
367 virtual bool FactorOf(const JsonUInt &value) const = 0;
368 virtual bool FactorOf(const JsonInt &value) const = 0;
369 virtual bool FactorOf(const JsonUInt64 &value) const = 0;
370 virtual bool FactorOf(const JsonInt64 &value) const = 0;
371 virtual bool FactorOf(const JsonDouble &value) const = 0;
375};
376
380 class JsonUInt: public JsonNumber {
381 public:
386 explicit JsonUInt(unsigned int value) : m_value(value) {}
387
388 bool operator==(const JsonValue &other) const { return other.Equals(*this); }
389
390 bool operator<(const JsonNumber &other) const {
391 return other.Compare(*this) == 1;
392 }
393
394 bool MultipleOf(const JsonNumber &other) const {
395 return other.FactorOf(*this);
396 }
397
398 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
399 void Accept(JsonValueConstVisitorInterface *visitor) const {
400 visitor->Visit(*this);
401 }
402
403 JsonValue* Clone() const { return new JsonUInt(m_value); }
404
408 unsigned int Value() const { return m_value; }
409
413 bool Equals(const JsonUInt &other) const {
414 return m_value == other.m_value;
415 }
416
417 bool Equals(const JsonInt &other) const;
418 bool Equals(const JsonUInt64 &other) const;
419 bool Equals(const JsonInt64 &other) const;
420
421 int Compare(const JsonUInt &value) const;
422 int Compare(const JsonInt &value) const;
423 int Compare(const JsonUInt64 &value) const;
424 int Compare(const JsonInt64 &value) const;
425 int Compare(const JsonDouble &value) const;
426
427 bool FactorOf(const JsonUInt &value) const;
428 bool FactorOf(const JsonInt &value) const;
429 bool FactorOf(const JsonUInt64 &value) const;
430 bool FactorOf(const JsonInt64 &value) const;
431 bool FactorOf(const JsonDouble &value) const;
435
436 private:
437 const unsigned int m_value;
438
439 JsonUInt(const JsonUInt &) = delete;
440 const JsonUInt &operator=(const JsonUInt &) = delete;
441};
442
443
447 class JsonInt: public JsonNumber {
448 public:
453 explicit JsonInt(int value) : m_value(value) {}
454
455 bool operator==(const JsonValue &other) const { return other.Equals(*this); }
456
457 bool operator<(const JsonNumber &other) const {
458 return other.Compare(*this) == 1;
459 }
460
461 bool MultipleOf(const JsonNumber &other) const {
462 return other.FactorOf(*this);
463 }
464
465 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
466 void Accept(JsonValueConstVisitorInterface *visitor) const {
467 visitor->Visit(*this);
468 }
469
470 JsonValue* Clone() const { return new JsonInt(m_value); }
471
475 int Value() const { return m_value; }
476
480 bool Equals(const JsonInt &other) const {
481 return m_value == other.m_value;
482 }
483
484 bool Equals(const JsonUInt &other) const;
485 bool Equals(const JsonUInt64 &other) const;
486 bool Equals(const JsonInt64 &other) const;
487
488 int Compare(const JsonUInt &value) const;
489 int Compare(const JsonInt &value) const;
490 int Compare(const JsonUInt64 &value) const;
491 int Compare(const JsonInt64 &value) const;
492 int Compare(const JsonDouble &value) const;
493
494 bool FactorOf(const JsonUInt &value) const;
495 bool FactorOf(const JsonInt &value) const;
496 bool FactorOf(const JsonUInt64 &value) const;
497 bool FactorOf(const JsonInt64 &value) const;
498 bool FactorOf(const JsonDouble &value) const;
502 private:
503 const int m_value;
504
505 JsonInt(const JsonInt &) = delete;
506 const JsonInt &operator=(const JsonInt &) = delete;
507};
508
509
513 class JsonUInt64: public JsonNumber {
514 public:
519 explicit JsonUInt64(uint64_t value) : m_value(value) {}
520
521 bool operator==(const JsonValue &other) const {
522 return other.Equals(*this);
523 }
524
525 bool operator<(const JsonNumber &other) const {
526 return other.Compare(*this) == 1;
527 }
528
529 bool MultipleOf(const JsonNumber &other) const {
530 return other.FactorOf(*this);
531 }
532
533 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
534 void Accept(JsonValueConstVisitorInterface *visitor) const {
535 visitor->Visit(*this);
536 }
537
538 JsonValue* Clone() const { return new JsonUInt64(m_value); }
539
543 uint64_t Value() const { return m_value; }
544
548 bool Equals(const JsonUInt64 &other) const {
549 return m_value == other.m_value;
550 }
551
552 bool Equals(const JsonUInt &other) const;
553 bool Equals(const JsonInt &other) const;
554 bool Equals(const JsonInt64 &other) const;
555
556 int Compare(const JsonUInt &value) const;
557 int Compare(const JsonInt &value) const;
558 int Compare(const JsonUInt64 &value) const;
559 int Compare(const JsonInt64 &value) const;
560 int Compare(const JsonDouble &value) const;
561
562 bool FactorOf(const JsonUInt &value) const;
563 bool FactorOf(const JsonInt &value) const;
564 bool FactorOf(const JsonUInt64 &value) const;
565 bool FactorOf(const JsonInt64 &value) const;
566 bool FactorOf(const JsonDouble &value) const;
570 private:
571 const uint64_t m_value;
572
573 JsonUInt64(const JsonUInt64 &) = delete;
574 const JsonUInt64 &operator=(const JsonUInt64 &) = delete;
575};
576
577
581 class JsonInt64: public JsonNumber {
582 public:
587 explicit JsonInt64(int64_t value) : m_value(value) {}
588
589 bool operator==(const JsonValue &other) const {
590 return other.Equals(*this);
591 }
592
593 bool operator<(const JsonNumber &other) const {
594 return other.Compare(*this) == 1;
595 }
596
597 bool MultipleOf(const JsonNumber &other) const {
598 return other.FactorOf(*this);
599 }
600
601 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
602 void Accept(JsonValueConstVisitorInterface *visitor) const {
603 visitor->Visit(*this);
604 }
605
606 JsonValue* Clone() const { return new JsonInt64(m_value); }
607
611 int64_t Value() const { return m_value; }
612
616 bool Equals(const JsonInt64 &other) const {
617 return m_value == other.m_value;
618 }
619
620 bool Equals(const JsonUInt &other) const;
621 bool Equals(const JsonInt &other) const;
622 bool Equals(const JsonUInt64 &other) const;
623
624 int Compare(const JsonUInt &value) const;
625 int Compare(const JsonInt &value) const;
626 int Compare(const JsonUInt64 &value) const;
627 int Compare(const JsonInt64 &value) const;
628 int Compare(const JsonDouble &value) const;
629
630 bool FactorOf(const JsonUInt &value) const;
631 bool FactorOf(const JsonInt &value) const;
632 bool FactorOf(const JsonUInt64 &value) const;
633 bool FactorOf(const JsonInt64 &value) const;
634 bool FactorOf(const JsonDouble &value) const;
638 private:
639 const int64_t m_value;
640
641 JsonInt64(const JsonInt64 &) = delete;
642 const JsonInt64 &operator=(const JsonInt64 &) = delete;
643};
644
645
654 class JsonDouble: public JsonNumber {
655 public:
666 struct DoubleRepresentation {
668 bool is_negative;
670 uint64_t full;
672 uint32_t leading_fractional_zeros;
674 uint64_t fractional;
676 int32_t exponent;
677 };
678
683 explicit JsonDouble(double value);
684
688 explicit JsonDouble(const DoubleRepresentation &rep);
689
690 bool operator==(const JsonValue &other) const {
691 return other.Equals(*this);
692 }
693
694 bool operator<(const JsonNumber &other) const {
695 return other.Compare(*this) == 1;
696 }
697
698 bool MultipleOf(const JsonNumber &other) const {
699 return other.FactorOf(*this);
700 }
701
702 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
703 void Accept(JsonValueConstVisitorInterface *visitor) const {
704 visitor->Visit(*this);
705 }
706
710 const std::string& ToString() const { return m_as_string; }
711
717 double Value() const { return m_value; }
718
725 static bool AsDouble(const DoubleRepresentation &rep, double *out);
726
732 static std::string AsString(const DoubleRepresentation &rep);
733
734 JsonValue* Clone() const {
735 // This loses precision, maybe we should fix it?
736 return new JsonDouble(m_value);
737 }
738
742 bool Equals(const JsonDouble &other) const {
743 // This is sketchy. The standard says "have the same mathematical value"
744 // TODO(simon): Think about this some more.
745 return m_value == other.m_value;
746 }
747
748 int Compare(const JsonUInt &value) const;
749 int Compare(const JsonInt &value) const;
750 int Compare(const JsonUInt64 &value) const;
751 int Compare(const JsonInt64 &value) const;
752 int Compare(const JsonDouble &value) const;
753
754 bool FactorOf(const JsonUInt &value) const;
755 bool FactorOf(const JsonInt &value) const;
756 bool FactorOf(const JsonUInt64 &value) const;
757 bool FactorOf(const JsonInt64 &value) const;
758 bool FactorOf(const JsonDouble &value) const;
762 private:
763 double m_value;
764 std::string m_as_string;
765
766 JsonDouble(const JsonDouble &) = delete;
767 const JsonDouble &operator=(const JsonDouble &) = delete;
768};
769
770
774 class JsonBool: public JsonLeafValue {
775 public:
780 explicit JsonBool(bool value) : m_value(value) {}
781
782 bool operator==(const JsonValue &other) const {
783 return other.Equals(*this);
784 }
785
786 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
787 void Accept(JsonValueConstVisitorInterface *visitor) const {
788 visitor->Visit(*this);
789 }
790
791 JsonValue* Clone() const { return new JsonBool(m_value); }
792
796 bool Value() const { return m_value; }
797
801 bool Equals(const JsonBool &other) const {
802 return m_value == other.m_value;
803 }
807 private:
808 const bool m_value;
809
810 JsonBool(const JsonBool &) = delete;
811 const JsonBool &operator=(const JsonBool &) = delete;
812};
813
814
818 class JsonNull: public JsonLeafValue {
819 public:
823 JsonNull() {}
824
825 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
826 void Accept(JsonValueConstVisitorInterface *visitor) const {
827 visitor->Visit(*this);
828 }
829
830 JsonValue* Clone() const { return new JsonNull(); }
831
832 bool operator==(const JsonValue &other) const { return other.Equals(*this); }
833
837 bool Equals(const JsonNull &) const { return true; }
841 private:
842 JsonNull(const JsonNull &) = delete;
843 const JsonNull &operator=(const JsonNull &) = delete;
844};
845
846
850 class JsonRawValue: public JsonLeafValue {
851 public:
856 explicit JsonRawValue(const std::string &value) : m_value(value) {}
857
858 bool operator==(const JsonValue &other) const { return other.Equals(*this); }
859
860 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
861 void Accept(JsonValueConstVisitorInterface *visitor) const {
862 visitor->Visit(*this);
863 }
864
865 JsonValue* Clone() const { return new JsonRawValue(m_value); }
866
870 const std::string& Value() const { return m_value; }
871
875 bool Equals(const JsonRawValue &other) const {
876 return m_value == other.m_value;
877 }
881
882 private:
883 const std::string m_value;
884
885 JsonRawValue(const JsonRawValue &) = delete;
886 const JsonRawValue &operator=(const JsonRawValue &) = delete;
887};
888
889
899 class JsonObject: public JsonValue {
900 public:
904 JsonObject() {}
905 ~JsonObject();
906
907 JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
908
909 bool operator==(const JsonValue &other) const {
910 return other.Equals(*this);
911 }
912
913 bool Equals(const JsonObject &other) const;
914
920 void Add(const std::string &key, const std::string &value);
921
927 void Add(const std::string &key, const char *value);
928
934 void Add(const std::string &key, unsigned int i);
935
941 void Add(const std::string &key, int i);
942
948 void Add(const std::string &key, double d);
949
955 void Add(const std::string &key, bool value);
956
961 void Add(const std::string &key);
962
968 JsonObject* AddObject(const std::string &key);
969
975 class JsonArray* AddArray(const std::string &key);
976
982 void AddValue(const std::string &key, JsonValue *value);
983
989 void AddRaw(const std::string &key, const std::string &value);
990
996 bool Remove(const std::string &key);
997
1006 bool ReplaceValue(const std::string &key, JsonValue *value);
1007
1008 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
1009 void Accept(JsonValueConstVisitorInterface *visitor) const {
1010 visitor->Visit(*this);
1011 }
1012
1013 JsonValue* Clone() const;
1014
1019 bool IsEmpty() const { return m_members.empty(); }
1020
1021 unsigned int Size() const { return m_members.size(); }
1022
1028 void VisitProperties(JsonObjectPropertyVisitor *visitor) const;
1029
1030 private:
1031 typedef std::map<std::string, JsonValue*> MemberMap;
1032 MemberMap m_members;
1033
1034 JsonObject(const JsonObject &) = delete;
1035 const JsonObject &operator=(const JsonObject &) = delete;
1036};
1037
1038
1043 class JsonArray: public JsonValue {
1044 public:
1045 JsonArray() : m_complex_type(false) {}
1046 ~JsonArray();
1047
1048 JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
1049
1050 bool operator==(const JsonValue &other) const {
1051 return other.Equals(*this);
1052 }
1053
1054 bool Equals(const JsonArray &other) const;
1055
1060 void Append(const std::string &value) {
1061 m_values.push_back(new JsonString(value));
1062 }
1063
1068 void Append(const char *value) {
1069 m_values.push_back(new JsonString(value));
1070 }
1071
1076 void Append(unsigned int i) {
1077 m_values.push_back(new JsonUInt(i));
1078 }
1079
1084 void Append(int i) {
1085 m_values.push_back(new JsonInt(i));
1086 }
1087
1092 void Append(bool value) {
1093 m_values.push_back(new JsonBool(value));
1094 }
1095
1099 void Append() {
1100 m_values.push_back(new JsonNull());
1101 }
1102
1106 void Append(JsonValue *value) {
1107 m_values.push_back(value);
1108 }
1109
1113 void Append(JsonObject *value) {
1114 m_values.push_back(value);
1115 m_complex_type |= !value->IsEmpty();
1116 }
1117
1123 JsonObject* AppendObject() {
1124 JsonObject *obj = new JsonObject();
1125 m_values.push_back(obj);
1126 m_complex_type = true;
1127 return obj;
1128 }
1129
1135 JsonArray* AppendArray() {
1136 JsonArray *array = new JsonArray();
1137 m_values.push_back(array);
1138 m_complex_type = true;
1139 return array;
1140 }
1141
1146 void AppendValue(JsonValue *value) {
1147 m_values.push_back(value);
1148 }
1149
1153 void AppendRaw(const std::string &value) {
1154 m_values.push_back(new JsonRawValue(value));
1155 }
1156
1162 bool RemoveElementAt(uint32_t index);
1163
1170 bool ReplaceElementAt(uint32_t index, JsonValue *value);
1171
1180 bool InsertElementAt(uint32_t index, JsonValue *value);
1181
1182 void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
1183 void Accept(JsonValueConstVisitorInterface *visitor) const {
1184 visitor->Visit(*this);
1185 }
1186
1187 JsonValue* Clone() const;
1188
1193 bool IsEmpty() const { return m_values.empty(); }
1194
1198 unsigned int Size() const { return m_values.size(); }
1199
1204 const JsonValue *ElementAt(unsigned int i) const;
1205
1209 bool IsComplexType() const { return m_complex_type; }
1210
1211 private:
1212 typedef std::vector<JsonValue*> ValuesVector;
1213 ValuesVector m_values;
1214
1215 // true if this array contains a nested object or array
1216 bool m_complex_type;
1217
1218 JsonArray(const JsonArray &) = delete;
1219 const JsonArray &operator=(const JsonArray &) = delete;
1220};
1221
1222
1226 class JsonObjectPropertyVisitor {
1227 public:
1228 virtual ~JsonObjectPropertyVisitor() {}
1229
1233 virtual void VisitProperty(const std::string &property,
1234 const JsonValue &value) = 0;
1235};
1236
1243 JsonObject* ObjectCast(JsonValue *value);
1244
1251 JsonArray* ArrayCast(JsonValue *value);
1252
1254
1255// operator<<
1256std::ostream& operator<<(std::ostream &os, const JsonString &value);
1257std::ostream& operator<<(std::ostream &os, const JsonUInt &value);
1258std::ostream& operator<<(std::ostream &os, const JsonInt &value);
1259std::ostream& operator<<(std::ostream &os, const JsonUInt64 &value);
1260std::ostream& operator<<(std::ostream &os, const JsonInt64 &value);
1261std::ostream& operator<<(std::ostream &os, const JsonDouble &value);
1262std::ostream& operator<<(std::ostream &os, const JsonBool &value);
1263std::ostream& operator<<(std::ostream &os, const JsonNull &value);
1264std::ostream& operator<<(std::ostream &os, const JsonRawValue &value);
1265
1266// JsonValue::NewNumberValue implementations.
1267template <>
1268inline JsonNumber* JsonValue::NewNumberValue<uint32_t>(
1269 const uint32_t &value) {
1270 return new JsonUInt(value);
1271}
1272
1273template <>
1274inline JsonNumber* JsonValue::NewNumberValue<int32_t>(
1275 const int32_t &value) {
1276 return new JsonInt(value);
1277}
1278
1279template <>
1280inline JsonNumber* JsonValue::NewNumberValue<uint64_t>(
1281 const uint64_t &value) {
1282 return new JsonUInt64(value);
1283}
1284
1285template <>
1286inline JsonNumber* JsonValue::NewNumberValue<int64_t>(
1287 const int64_t &value) {
1288 return new JsonInt64(value);
1289}
1290
1291template <>
1292inline JsonNumber* JsonValue::NewNumberValue<double>(
1293 const double &value) {
1294 return new JsonDouble(value);
1295}
1296
1297template <>
1298inline JsonNumber* JsonValue::NewNumberValue<
1299 JsonDouble::DoubleRepresentation>(
1300 const JsonDouble::DoubleRepresentation &value) {
1301 return new JsonDouble(value);
1302}
1303
1304// JsonValue::NewValue implementations.
1305template <>
1306inline JsonValue* JsonValue::NewValue<std::string>(const std::string &value) {
1307 return new JsonString(value);
1308}
1309
1310template <>
1311inline JsonValue* JsonValue::NewValue<uint32_t>(const uint32_t &value) {
1312 return NewNumberValue(value);
1313}
1314
1315template <>
1316inline JsonValue* JsonValue::NewValue<int32_t>(const int32_t &value) {
1317 return NewNumberValue(value);
1318}
1319
1320template <>
1321inline JsonValue* JsonValue::NewValue<uint64_t>(const uint64_t &value) {
1322 return NewNumberValue(value);
1323}
1324
1325template <>
1326inline JsonValue* JsonValue::NewValue<int64_t>(const int64_t &value) {
1327 return NewNumberValue(value);
1328}
1329
1330template <>
1331inline JsonValue* JsonValue::NewValue<double>(const double &value) {
1332 return NewNumberValue(value);
1333}
1334
1335template <>
1336inline JsonValue* JsonValue::NewValue<JsonDouble::DoubleRepresentation>(
1337 const JsonDouble::DoubleRepresentation &value) {
1338 return NewNumberValue(value);
1339}
1340
1341template <>
1342inline JsonValue* JsonValue::NewValue<bool>(const bool &value) {
1343 return new JsonBool(value);
1344}
1346} // namespace web
1347} // namespace ola
1348#endif // INCLUDE_OLA_WEB_JSON_H_
JsonPointer.h
An implementation of Json Pointers (RFC 6901).
Macro.h
Helper macros.
StringUtils.h
Various string utility functions.
ola::JsonObject::JsonObject
JsonObject()
Create a new JsonObject.
Definition Json.h:904
ola::web::JsonArray
An array of JSON values. Arrays in JSON can contain values of different types.
Definition Json.h:1043
ola::web::JsonArray::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.cpp:763
ola::web::JsonArray::IsEmpty
bool IsEmpty() const
Check if there are elements within the array.
Definition Json.h:1193
ola::web::JsonArray::Append
void Append(const char *value)
Append a string value to the array.
Definition Json.h:1068
ola::web::JsonArray::Append
void Append(JsonObject *value)
Append a JsonValue. Takes ownership of the pointer.
Definition Json.h:1113
ola::web::JsonArray::ElementAt
const JsonValue * ElementAt(unsigned int i) const
Return the element at index i.
Definition Json.cpp:772
ola::web::JsonArray::Equals
bool Equals(const JsonArray &other) const
Check if this JsonValue equals a JsonArray.
Definition Json.cpp:714
ola::web::JsonArray::Append
void Append(JsonValue *value)
Append a JsonValue. Takes ownership of the pointer.
Definition Json.h:1106
ola::web::JsonArray::Append
void Append(const std::string &value)
Append a string value to the array.
Definition Json.h:1060
ola::web::JsonArray::AppendRaw
void AppendRaw(const std::string &value)
Append a raw value to the array.
Definition Json.h:1153
ola::web::JsonArray::AppendArray
JsonArray * AppendArray()
Append a JsonArray to the array.
Definition Json.h:1135
ola::web::JsonArray::Append
void Append(unsigned int i)
Append an unsigned int value to the array.
Definition Json.h:1076
ola::web::JsonArray::IsComplexType
bool IsComplexType() const
Return true if this array contains nested arrays or objects.
Definition Json.h:1209
ola::web::JsonArray::AppendValue
void AppendValue(JsonValue *value)
Append a JsonValue to the array.
Definition Json.h:1146
ola::web::JsonArray::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:1183
ola::web::JsonArray::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:1182
ola::web::JsonArray::Append
void Append(int i)
Append an int value to the array.
Definition Json.h:1084
ola::web::JsonArray::RemoveElementAt
bool RemoveElementAt(uint32_t index)
Remove the JsonValue at the specified index.
Definition Json.cpp:730
ola::web::JsonArray::AppendObject
JsonObject * AppendObject()
Append a JsonObject to the array.
Definition Json.h:1123
ola::web::JsonArray::Size
unsigned int Size() const
Return the number of elements in the array.
Definition Json.h:1198
ola::web::JsonArray::ReplaceElementAt
bool ReplaceElementAt(uint32_t index, JsonValue *value)
Replace the JsonValue at the specified index.
Definition Json.cpp:740
ola::web::JsonArray::Append
void Append()
Append a null value to the array.
Definition Json.h:1099
ola::web::JsonArray::Append
void Append(bool value)
Append a bool value to the array.
Definition Json.h:1092
ola::web::JsonArray::LookupElementWithIter
JsonValue * LookupElementWithIter(JsonPointer::Iterator *iter)
Lookup the Value referred to by the Iterator.
Definition Json.cpp:691
ola::web::JsonArray::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:1050
ola::web::JsonArray::InsertElementAt
bool InsertElementAt(uint32_t index, JsonValue *value)
Inserts the JsonValue at the specified index.
Definition Json.cpp:752
ola::web::JsonBool
A Bool value.
Definition Json.h:774
ola::web::JsonBool::Value
bool Value() const
Return the bool value.
Definition Json.h:796
ola::web::JsonBool::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:782
ola::web::JsonBool::JsonBool
JsonBool(bool value)
Create a new JsonBool.
Definition Json.h:780
ola::web::JsonBool::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:786
ola::web::JsonBool::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:791
ola::web::JsonBool::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:787
ola::web::JsonDouble
A double value.
Definition Json.h:654
ola::web::JsonDouble::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:703
ola::web::JsonDouble::AsDouble
static bool AsDouble(const DoubleRepresentation &rep, double *out)
Convert a DoubleRepresentation to a double value.
Definition Json.cpp:524
ola::web::JsonDouble::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:734
ola::web::JsonDouble::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:690
ola::web::JsonDouble::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:702
ola::web::JsonDouble::ToString
const std::string & ToString() const
Return the double value as a string.
Definition Json.h:710
ola::web::JsonDouble::Value
double Value() const
Returns the value as a double.
Definition Json.h:717
ola::web::JsonDouble::operator<
bool operator<(const JsonNumber &other) const
Less than operator.
Definition Json.h:694
ola::web::JsonDouble::AsString
static std::string AsString(const DoubleRepresentation &rep)
Convert the DoubleRepresentation to a string.
Definition Json.cpp:543
ola::web::JsonDouble::JsonDouble
JsonDouble(double value)
Create a new JsonDouble.
Definition Json.cpp:512
ola::web::JsonDouble::MultipleOf
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition Json.h:698
ola::web::JsonInt64
A signed 64bit int value.
Definition Json.h:581
ola::web::JsonInt64::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:589
ola::web::JsonInt64::JsonInt64
JsonInt64(int64_t value)
Create a new JsonInt64.
Definition Json.h:587
ola::web::JsonInt64::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:601
ola::web::JsonInt64::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:602
ola::web::JsonInt64::MultipleOf
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition Json.h:597
ola::web::JsonInt64::Value
int64_t Value() const
Return the int64_t value.
Definition Json.h:611
ola::web::JsonInt64::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:606
ola::web::JsonInt64::operator<
bool operator<(const JsonNumber &other) const
Less than operator.
Definition Json.h:593
ola::web::JsonInt
A signed 32bit int value.
Definition Json.h:447
ola::web::JsonInt::MultipleOf
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition Json.h:461
ola::web::JsonInt::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:455
ola::web::JsonInt::operator<
bool operator<(const JsonNumber &other) const
Less than operator.
Definition Json.h:457
ola::web::JsonInt::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:470
ola::web::JsonInt::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:465
ola::web::JsonInt::JsonInt
JsonInt(int value)
Create a new JsonInt.
Definition Json.h:453
ola::web::JsonInt::Value
int Value() const
Return the int32_t value.
Definition Json.h:475
ola::web::JsonInt::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:466
ola::web::JsonLeafValue
A base class used to describe values which are leafs of a JSON tree.
Definition Json.h:265
ola::web::JsonNull
The null value.
Definition Json.h:818
ola::web::JsonNull::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:832
ola::web::JsonNull::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:825
ola::web::JsonNull::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:830
ola::web::JsonNull::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:826
ola::web::JsonNull::JsonNull
JsonNull()
Create a new JsonNull.
Definition Json.h:823
ola::web::JsonNumber
JsonNumber is the base class for various integer / number classes.
Definition Json.h:325
ola::web::JsonNumber::operator<=
bool operator<=(const JsonNumber &other) const
Less than or equals operator.
Definition Json.h:340
ola::web::JsonNumber::MultipleOf
virtual bool MultipleOf(const JsonNumber &other) const =0
Checks if the remainder if non-0;.
ola::web::JsonNumber::operator>
bool operator>(const JsonNumber &other) const
Greater than operator.
Definition Json.h:347
ola::web::JsonNumber::operator<
virtual bool operator<(const JsonNumber &other) const =0
Less than operator.
ola::web::JsonNumber::operator>=
bool operator>=(const JsonNumber &other) const
Greater than or equals operator.
Definition Json.h:354
ola::web::JsonObject
A JSON object. JSON Objects are key : value mappings, similar to dictionaries in Python.
Definition Json.h:899
ola::web::JsonObject::Remove
bool Remove(const std::string &key)
Remove the JsonValue with the specified key.
Definition Json.cpp:639
ola::web::JsonObject::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:1008
ola::web::JsonObject::LookupElementWithIter
JsonValue * LookupElementWithIter(JsonPointer::Iterator *iter)
Lookup the Value referred to by the Iterator.
Definition Json.cpp:571
ola::web::JsonObject::AddValue
void AddValue(const std::string &key, JsonValue *value)
Set the key to the supplied JsonValue.
Definition Json.cpp:667
ola::web::JsonObject::ReplaceValue
bool ReplaceValue(const std::string &key, JsonValue *value)
Replace the key with the supplied JsonValue.
Definition Json.cpp:643
ola::web::JsonObject::Equals
bool Equals(const JsonObject &other) const
Check if this JsonValue equals a JsonObject.
Definition Json.cpp:590
ola::web::JsonObject::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:1009
ola::web::JsonObject::IsEmpty
bool IsEmpty() const
Check if there are properties within the object.
Definition Json.h:1019
ola::web::JsonObject::AddRaw
void AddRaw(const std::string &key, const std::string &value)
Set the given key to a raw value.
Definition Json.cpp:635
ola::web::JsonObject::Add
void Add(const std::string &key, const std::string &value)
Add a key to string mapping.
Definition Json.cpp:607
ola::web::JsonObject::VisitProperties
void VisitProperties(JsonObjectPropertyVisitor *visitor) const
Visit each of the properties in this object.
Definition Json.cpp:680
ola::web::JsonObject::JsonObject
JsonObject()
Create a new JsonObject.
Definition Json.h:904
ola::web::JsonObject::AddObject
JsonObject * AddObject(const std::string &key)
Set the given key to a JsonObject.
Definition Json.cpp:655
ola::web::JsonObject::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:909
ola::web::JsonObject::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.cpp:671
ola::web::JsonObject::AddArray
class JsonArray * AddArray(const std::string &key)
Set the given key to a JsonArray.
Definition Json.cpp:661
ola::web::JsonObjectPropertyVisitor
A class used to visit Json values within a JsonObject.
Definition Json.h:1226
ola::web::JsonObjectPropertyVisitor::VisitProperty
virtual void VisitProperty(const std::string &property, const JsonValue &value)=0
Visit the value at the given property.
ola::web::JsonPointer::Iterator
An iterator for traversing a JsonPointer.
Definition JsonPointer.h:74
ola::web::JsonRawValue
A raw value, useful if you want to cheat.
Definition Json.h:850
ola::web::JsonRawValue::Value
const std::string & Value() const
Return the raw value as a string.
Definition Json.h:870
ola::web::JsonRawValue::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:865
ola::web::JsonRawValue::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:861
ola::web::JsonRawValue::JsonRawValue
JsonRawValue(const std::string &value)
Create a new JsonRawValue.
Definition Json.h:856
ola::web::JsonRawValue::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:858
ola::web::JsonRawValue::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:860
ola::web::JsonString
A string value.
Definition Json.h:279
ola::web::JsonString::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:294
ola::web::JsonString::JsonString
JsonString(const std::string &value)
Create a new JsonString.
Definition Json.h:285
ola::web::JsonString::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:299
ola::web::JsonString::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:287
ola::web::JsonString::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:295
ola::web::JsonString::Value
const std::string & Value() const
Return the string value.
Definition Json.h:292
ola::web::JsonUInt64
An unsigned 64bit int value.
Definition Json.h:513
ola::web::JsonUInt64::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:534
ola::web::JsonUInt64::JsonUInt64
JsonUInt64(uint64_t value)
Create a new JsonUInt64.
Definition Json.h:519
ola::web::JsonUInt64::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:533
ola::web::JsonUInt64::MultipleOf
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition Json.h:529
ola::web::JsonUInt64::Value
uint64_t Value() const
Return the uint64_t value.
Definition Json.h:543
ola::web::JsonUInt64::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:521
ola::web::JsonUInt64::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:538
ola::web::JsonUInt64::operator<
bool operator<(const JsonNumber &other) const
Less than operator.
Definition Json.h:525
ola::web::JsonUInt
An unsigned 32bit int value.
Definition Json.h:380
ola::web::JsonUInt::Accept
void Accept(JsonValueVisitorInterface *visitor)
The Accept method for the visitor pattern.
Definition Json.h:398
ola::web::JsonUInt::Accept
void Accept(JsonValueConstVisitorInterface *visitor) const
The Accept (const) method for the visitor pattern.
Definition Json.h:399
ola::web::JsonUInt::Clone
JsonValue * Clone() const
Make a copy of this JsonValue.
Definition Json.h:403
ola::web::JsonUInt::Value
unsigned int Value() const
Return the uint32_t value.
Definition Json.h:408
ola::web::JsonUInt::JsonUInt
JsonUInt(unsigned int value)
Create a new JsonUInt.
Definition Json.h:386
ola::web::JsonUInt::operator==
bool operator==(const JsonValue &other) const
Equality operator.
Definition Json.h:388
ola::web::JsonUInt::operator<
bool operator<(const JsonNumber &other) const
Less than operator.
Definition Json.h:390
ola::web::JsonUInt::MultipleOf
bool MultipleOf(const JsonNumber &other) const
Checks if the remainder if non-0;.
Definition Json.h:394
ola::web::JsonValueConstVisitorInterface
The const interface for the JsonValueVisitor class.
Definition Json.h:99
ola::web::JsonValue
The base class for JSON values.
Definition Json.h:119
ola::web::JsonValue::Clone
virtual JsonValue * Clone() const =0
Make a copy of this JsonValue.
ola::web::JsonValue::operator==
virtual bool operator==(const JsonValue &other) const =0
Equality operator.
ola::web::JsonValue::operator!=
virtual bool operator!=(const JsonValue &other) const
Not-equals operator.
Definition Json.h:139
ola::web::JsonValue::Accept
virtual void Accept(JsonValueConstVisitorInterface *visitor) const =0
The Accept (const) method for the visitor pattern.
ola::web::JsonValue::Accept
virtual void Accept(JsonValueVisitorInterface *visitor)=0
The Accept method for the visitor pattern.
ola::web::JsonValue::LookupElement
virtual JsonValue * LookupElement(const JsonPointer &pointer)
Locate the JsonValue referred to by the JSON Pointer.
Definition Json.cpp:83
ola::web::JsonValueVisitorInterface
The interface for the JsonValueVisitor class.
Definition Json.h:75
ola::web::ArrayCast
JsonArray * ArrayCast(JsonValue *value)
Downcast a JsonValue to a JsonArray.
Definition Json.cpp:795
ola::web::ObjectCast
JsonObject * ObjectCast(JsonValue *value)
Downcast a JsonValue to a JsonObject.
Definition Json.cpp:780
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
ola::web::JsonDouble::DoubleRepresentation::is_negative
bool is_negative
Definition Json.h:668
ola::web::JsonDouble::DoubleRepresentation::full
uint64_t full
Definition Json.h:670
ola::web::JsonDouble::DoubleRepresentation::exponent
int32_t exponent
Definition Json.h:676
ola::web::JsonDouble::DoubleRepresentation::leading_fractional_zeros
uint32_t leading_fractional_zeros
Definition Json.h:672
ola::web::JsonDouble::DoubleRepresentation::fractional
uint64_t fractional
Definition Json.h:674