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;
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;
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;
417 bool Equals(
const JsonUInt64 &other)
const;
418 bool Equals(
const JsonInt64 &other)
const;
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;
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;
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;
483 bool Equals(
const JsonUInt64 &other)
const;
484 bool Equals(
const JsonInt64 &other)
const;
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;
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;
503 DISALLOW_COPY_AND_ASSIGN(
JsonInt);
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;
557 int Compare(
const JsonDouble &value)
const;
559 bool FactorOf(
const JsonUInt &value)
const;
560 bool FactorOf(
const JsonInt &value)
const;
562 bool FactorOf(
const JsonInt64 &value)
const;
563 bool FactorOf(
const JsonDouble &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;
618 bool Equals(
const JsonUInt64 &other)
const;
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;
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;
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; }
720 static bool AsDouble(
const DoubleRepresentation &rep,
double *out);
727 static std::string
AsString(
const DoubleRepresentation &rep);
740 return m_value == other.m_value;
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;
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;
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);
980 void AddRaw(
const std::string &key,
const std::string &value);
987 bool Remove(
const std::string &key);
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);
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);
1174 visitor->Visit(*
this);
1188 unsigned int Size()
const {
return m_values.size(); }
1202 typedef std::vector<JsonValue*> ValuesVector;
1203 ValuesVector m_values;
1206 bool m_complex_type;
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) {
1265 return new JsonInt(value);
1269 inline JsonNumber* JsonValue::NewNumberValue<uint64_t>(
1270 const uint64_t &value) {
1271 return new JsonUInt64(value);
1275 inline JsonNumber* JsonValue::NewNumberValue<int64_t>(
1276 const int64_t &value) {
1277 return new JsonInt64(value);
1281 inline JsonNumber* JsonValue::NewNumberValue<double>(
1282 const double &value) {
1283 return new JsonDouble(value);
1287 inline JsonNumber* JsonValue::NewNumberValue<
1288 JsonDouble::DoubleRepresentation>(
1289 const JsonDouble::DoubleRepresentation &value) {
1290 return new JsonDouble(value);
1295 inline JsonValue* JsonValue::NewValue<std::string>(
const std::string &value) {
1296 return new JsonString(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>(
1326 const JsonDouble::DoubleRepresentation &value) {
1327 return NewNumberValue(value);
1331 inline JsonValue* JsonValue::NewValue<bool>(
const bool &value) {
1332 return new JsonBool(value);
1337 #endif // INCLUDE_OLA_WEB_JSON_H_