30 #ifndef INCLUDE_OLA_WEB_JSON_H_ 31 #define INCLUDE_OLA_WEB_JSON_H_ 51 class JsonObjectPropertyVisitor;
52 class JsonValueConstVisitorInterface;
53 class JsonValueVisitorInterface;
80 virtual void Visit(
JsonBool *value) = 0;
81 virtual void Visit(
JsonNull *value) = 0;
85 virtual void Visit(
JsonUInt *value) = 0;
87 virtual void Visit(
JsonInt *value) = 0;
103 virtual void Visit(
const JsonString &value) = 0;
104 virtual void Visit(
const JsonBool &value) = 0;
105 virtual void Visit(
const JsonNull &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;
134 virtual bool operator==(
const JsonValue &other)
const = 0;
140 return !(*
this == other);
178 virtual bool Equals(
const JsonString &)
const {
return false; }
184 virtual bool Equals(
const JsonUInt &)
const {
return false; }
190 virtual bool Equals(
const JsonInt &)
const {
return false; }
196 virtual bool Equals(
const JsonUInt64 &)
const {
return false; }
202 virtual bool Equals(
const JsonInt64 &)
const {
return false; }
208 virtual bool Equals(
const JsonBool &)
const {
return false; }
214 virtual bool Equals(
const JsonNull &)
const {
return false; }
220 virtual bool Equals(
const JsonDouble &)
const {
return false; }
226 virtual bool Equals(
const JsonRawValue &)
const {
return false; }
232 virtual bool Equals(
const JsonObject &)
const {
return false; }
238 virtual bool Equals(
const JsonArray &)
const {
return false; }
247 template <
typename T>
248 static JsonValue* NewValue(
const T &value);
254 template <
typename T>
255 static JsonNumber* NewNumberValue(
const T &value);
285 explicit JsonString(
const std::string &value) : m_value(value) {}
292 const std::string&
Value()
const {
return m_value; }
296 visitor->Visit(*
this);
305 return m_value == other.m_value;
312 const std::string m_value;
329 virtual bool MultipleOf(
const JsonNumber &other)
const = 0;
334 virtual bool operator<(
const JsonNumber &other)
const = 0;
340 return *
this == other || *
this < other;
347 return !(*
this <= other);
354 return !(*
this < other);
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;
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;
385 explicit JsonUInt(
unsigned int value) : m_value(value) {}
390 return other.Compare(*
this) == 1;
394 return other.FactorOf(*
this);
399 visitor->Visit(*
this);
407 unsigned int Value()
const {
return m_value; }
412 bool Equals(
const JsonUInt &other)
const {
413 return m_value == other.m_value;
416 bool Equals(
const JsonInt &other)
const;
418 bool Equals(
const JsonInt64 &other)
const;
420 int Compare(
const JsonUInt &value)
const;
421 int Compare(
const JsonInt &value)
const;
423 int Compare(
const JsonInt64 &value)
const;
426 bool FactorOf(
const JsonUInt &value)
const;
427 bool FactorOf(
const JsonInt &value)
const;
429 bool FactorOf(
const JsonInt64 &value)
const;
436 const unsigned int m_value;
451 explicit JsonInt(
int value) : m_value(value) {}
456 return other.Compare(*
this) == 1;
460 return other.FactorOf(*
this);
465 visitor->Visit(*
this);
473 int Value()
const {
return m_value; }
478 bool Equals(
const JsonInt &other)
const {
479 return m_value == other.m_value;
482 bool Equals(
const JsonUInt &other)
const;
484 bool Equals(
const JsonInt64 &other)
const;
486 int Compare(
const JsonUInt &value)
const;
487 int Compare(
const JsonInt &value)
const;
489 int Compare(
const JsonInt64 &value)
const;
492 bool FactorOf(
const JsonUInt &value)
const;
493 bool FactorOf(
const JsonInt &value)
const;
495 bool FactorOf(
const JsonInt64 &value)
const;
519 return other.Equals(*
this);
523 return other.Compare(*
this) == 1;
527 return other.FactorOf(*
this);
532 visitor->Visit(*
this);
540 uint64_t
Value()
const {
return m_value; }
546 return m_value == other.m_value;
549 bool Equals(
const JsonUInt &other)
const;
550 bool Equals(
const JsonInt &other)
const;
551 bool Equals(
const JsonInt64 &other)
const;
553 int Compare(
const JsonUInt &value)
const;
554 int Compare(
const JsonInt &value)
const;
556 int Compare(
const JsonInt64 &value)
const;
559 bool FactorOf(
const JsonUInt &value)
const;
560 bool FactorOf(
const JsonInt &value)
const;
562 bool FactorOf(
const JsonInt64 &value)
const;
568 const uint64_t m_value;
586 return other.Equals(*
this);
590 return other.Compare(*
this) == 1;
594 return other.FactorOf(*
this);
599 visitor->Visit(*
this);
607 int64_t
Value()
const {
return m_value; }
612 bool Equals(
const JsonInt64 &other)
const {
613 return m_value == other.m_value;
616 bool Equals(
const JsonUInt &other)
const;
617 bool Equals(
const JsonInt &other)
const;
620 int Compare(
const JsonUInt &value)
const;
621 int Compare(
const JsonInt &value)
const;
623 int Compare(
const JsonInt64 &value)
const;
626 bool FactorOf(
const JsonUInt &value)
const;
627 bool FactorOf(
const JsonInt &value)
const;
629 bool FactorOf(
const JsonInt64 &value)
const;
635 const int64_t m_value;
686 return other.Equals(*
this);
690 return other.Compare(*
this) == 1;
694 return other.FactorOf(*
this);
699 visitor->Visit(*
this);
705 const std::string&
ToString()
const {
return m_as_string; }
712 double Value()
const {
return m_value; }
740 return m_value == other.m_value;
743 int Compare(
const JsonUInt &value)
const;
744 int Compare(
const JsonInt &value)
const;
746 int Compare(
const JsonInt64 &value)
const;
749 bool FactorOf(
const JsonUInt &value)
const;
750 bool FactorOf(
const JsonInt &value)
const;
752 bool FactorOf(
const JsonInt64 &value)
const;
759 std::string m_as_string;
777 return other.Equals(*
this);
782 visitor->Visit(*
this);
790 bool Value()
const {
return m_value; }
795 bool Equals(
const JsonBool &other)
const {
796 return m_value == other.m_value;
820 visitor->Visit(*
this);
830 bool Equals(
const JsonNull &)
const {
return true; }
854 visitor->Visit(*
this);
862 const std::string&
Value()
const {
return m_value; }
868 return m_value == other.m_value;
875 const std::string m_value;
901 return other.Equals(*
this);
911 void Add(
const std::string &key,
const std::string &value);
918 void Add(
const std::string &key,
const char *value);
925 void Add(
const std::string &key,
unsigned int i);
932 void Add(
const std::string &key,
int i);
939 void Add(
const std::string &key,
double d);
946 void Add(
const std::string &key,
bool value);
952 void Add(
const std::string &key);
959 JsonObject* AddObject(
const std::string &key);
966 class JsonArray* AddArray(
const std::string &key);
973 void AddValue(
const std::string &key,
JsonValue *value);
980 void AddRaw(
const std::string &key,
const std::string &value);
987 bool Remove(
const std::string &key);
997 bool ReplaceValue(
const std::string &key,
JsonValue *value);
1001 visitor->Visit(*
this);
1010 bool IsEmpty()
const {
return m_members.empty(); }
1012 unsigned int Size()
const {
return m_members.size(); }
1022 typedef std::map<std::string, JsonValue*> MemberMap;
1023 MemberMap m_members;
1041 return other.Equals(*
this);
1044 bool Equals(
const JsonArray &other)
const;
1067 m_values.push_back(
new JsonUInt(i));
1075 m_values.push_back(
new JsonInt(i));
1083 m_values.push_back(
new JsonBool(value));
1090 m_values.push_back(
new JsonNull());
1097 m_values.push_back(value);
1104 m_values.push_back(value);
1105 m_complex_type |= !value->
IsEmpty();
1115 m_values.push_back(obj);
1116 m_complex_type =
true;
1127 m_values.push_back(array);
1128 m_complex_type =
true;
1137 m_values.push_back(value);
1152 bool RemoveElementAt(uint32_t index);
1160 bool ReplaceElementAt(uint32_t index,
JsonValue *value);
1170 bool InsertElementAt(uint32_t index,
JsonValue *value);
1174 visitor->Visit(*
this);
1188 unsigned int Size()
const {
return m_values.size(); }
1194 const JsonValue *ElementAt(
unsigned int i)
const;
1202 typedef std::vector<JsonValue*> ValuesVector;
1203 ValuesVector m_values;
1206 bool m_complex_type;
1222 virtual void VisitProperty(
const std::string &property,
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);
1257 inline JsonNumber* JsonValue::NewNumberValue<uint32_t>(
1258 const uint32_t &value) {
1263 inline JsonNumber* JsonValue::NewNumberValue<int32_t>(
1264 const int32_t &value) {
1269 inline JsonNumber* JsonValue::NewNumberValue<uint64_t>(
1270 const uint64_t &value) {
1275 inline JsonNumber* JsonValue::NewNumberValue<int64_t>(
1276 const int64_t &value) {
1281 inline JsonNumber* JsonValue::NewNumberValue<double>(
1282 const double &value) {
1287 inline JsonNumber* JsonValue::NewNumberValue<
1295 inline JsonValue* JsonValue::NewValue<std::string>(
const std::string &value) {
1300 inline JsonValue* JsonValue::NewValue<uint32_t>(
const uint32_t &value) {
1301 return NewNumberValue(value);
1305 inline JsonValue* JsonValue::NewValue<int32_t>(
const int32_t &value) {
1306 return NewNumberValue(value);
1310 inline JsonValue* JsonValue::NewValue<uint64_t>(
const uint64_t &value) {
1311 return NewNumberValue(value);
1315 inline JsonValue* JsonValue::NewValue<int64_t>(
const int64_t &value) {
1316 return NewNumberValue(value);
1320 inline JsonValue* JsonValue::NewValue<double>(
const double &value) {
1321 return NewNumberValue(value);
1325 inline JsonValue* JsonValue::NewValue<JsonDouble::DoubleRepresentation>(
1327 return NewNumberValue(value);
1331 inline JsonValue* JsonValue::NewValue<bool>(
const bool &value) {
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
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