/usr/include/ola/web/Json.h is in libola-dev 0.9.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 | /*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Json.h
* A simple set of classes for generating JSON.
* See http://www.json.org/
* Copyright (C) 2012 Simon Newton
*/
/**
* @addtogroup json
* @{
* @file Json.h
* @brief Basic data types used to represent elements in a JSON document.
* @}
*/
#ifndef INCLUDE_OLA_WEB_JSON_H_
#define INCLUDE_OLA_WEB_JSON_H_
#include <ola/StringUtils.h>
#include <ola/base/Macro.h>
#include <ola/web/JsonPointer.h>
#include <stdint.h>
#include <map>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
namespace ola {
namespace web {
/**
* @addtogroup json
* @{
*/
class JsonObjectPropertyVisitor;
class JsonValueConstVisitorInterface;
class JsonValueVisitorInterface;
class JsonArray;
class JsonBool;
class JsonDouble;
class JsonInt64;
class JsonInt;
class JsonNull;
class JsonNumber;
class JsonObject;
class JsonRawValue;
class JsonString;
class JsonUInt64;
class JsonUInt;
/**
* @brief The interface for the JsonValueVisitor class.
*
* An implementation of a JsonValueVisitorInterface can be passed to the
* Accept() method of a JsonValue. This provides traversal of a JSON tree in a
* type safe manner.
*/
class JsonValueVisitorInterface {
public:
virtual ~JsonValueVisitorInterface() {}
virtual void Visit(JsonString *value) = 0;
virtual void Visit(JsonBool *value) = 0;
virtual void Visit(JsonNull *value) = 0;
virtual void Visit(JsonRawValue *value) = 0;
virtual void Visit(JsonObject *value) = 0;
virtual void Visit(JsonArray *value) = 0;
virtual void Visit(JsonUInt *value) = 0;
virtual void Visit(JsonUInt64 *value) = 0;
virtual void Visit(JsonInt *value) = 0;
virtual void Visit(JsonInt64 *value) = 0;
virtual void Visit(JsonDouble *value) = 0;
};
/**
* @brief The const interface for the JsonValueVisitor class.
*
* An implementation of a JsonValueConstVisitorInterface can be passed to the
* Accept() method of a JsonValue. This provides traversal of a JSON tree in a
* type safe manner.
*/
class JsonValueConstVisitorInterface {
public:
virtual ~JsonValueConstVisitorInterface() {}
virtual void Visit(const JsonString &value) = 0;
virtual void Visit(const JsonBool &value) = 0;
virtual void Visit(const JsonNull &value) = 0;
virtual void Visit(const JsonRawValue &value) = 0;
virtual void Visit(const JsonObject &value) = 0;
virtual void Visit(const JsonArray &value) = 0;
virtual void Visit(const JsonUInt &value) = 0;
virtual void Visit(const JsonUInt64 &value) = 0;
virtual void Visit(const JsonInt &value) = 0;
virtual void Visit(const JsonInt64 &value) = 0;
virtual void Visit(const JsonDouble &value) = 0;
};
/**
* @brief The base class for JSON values.
*/
class JsonValue {
public:
virtual ~JsonValue() {}
/**
* @brief Locate the JsonValue referred to by the JSON Pointer.
*/
virtual JsonValue* LookupElement(const JsonPointer &pointer);
/**
* @brief Equality operator.
*
* This implements equality as defined in section 3.6 of the JSON Schema Core
* document.
*/
virtual bool operator==(const JsonValue &other) const = 0;
/**
* @brief Not-equals operator.
*/
virtual bool operator!=(const JsonValue &other) const {
return !(*this == other);
}
/**
* @brief The Accept method for the visitor pattern.
*
* This can be used to traverse the Json Tree in a type-safe manner.
*/
virtual void Accept(JsonValueVisitorInterface *visitor) = 0;
/**
* @brief The Accept (const) method for the visitor pattern.
*
* This can be used to traverse the Json Tree in a type-safe manner.
*/
virtual void Accept(JsonValueConstVisitorInterface *visitor) const = 0;
/**
* @brief Make a copy of this JsonValue.
*/
virtual JsonValue* Clone() const = 0;
/**
* @privatesection
*/
/**
* @brief Lookup the Value referred to by the Iterator.
*
* This is used by recursively by JsonValue classes. You should call
* LookupElement() instead.
*/
virtual JsonValue* LookupElementWithIter(JsonPointer::Iterator *iterator) = 0;
/**
* @brief Check if this JsonValue equals a JsonString.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonString &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonUInt.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonUInt &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonInt.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonInt &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonUInt64.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonUInt64 &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonInt64.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonInt64 &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonBool.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonBool &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonNull.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonNull &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonDouble.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonDouble &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonRawValue.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonRawValue &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonObject.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonObject &) const { return false; }
/**
* @brief Check if this JsonValue equals a JsonArray.
* @returns true if the two values are equal, false otherwise.
*/
virtual bool Equals(const JsonArray &) const { return false; }
/**
* @endsection
*/
/**
* @brief Given a variable, create a new JsonValue of the appropriate type.
*/
template <typename T>
static JsonValue* NewValue(const T &value);
/**
* @brief Given a variable, create a new JsonNumber of the appropriate
* type.
*/
template <typename T>
static JsonNumber* NewNumberValue(const T &value);
};
/**
* @brief A base class used to describe values which are leafs of a JSON tree.
*
* Leaf values are those which don't contain other values. All values except
* JsonObject and JsonArray are leaf values.
*/
class JsonLeafValue : public JsonValue {
public:
/**
* @privatesection
*/
JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
/**
* @endsection
*/
};
/**
* @brief A string value.
*/
class JsonString: public JsonLeafValue {
public:
/**
* @brief Create a new JsonString
* @param value the string to use.
*/
explicit JsonString(const std::string &value) : m_value(value) {}
bool operator==(const JsonValue &other) const { return other.Equals(*this); }
/**
* @brief Return the string value.
*/
const std::string& Value() const { return m_value; }
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonString(m_value); }
/**
* @privatesection
*/
bool Equals(const JsonString &other) const {
return m_value == other.m_value;
}
/**
* @endsection
*/
private:
const std::string m_value;
DISALLOW_COPY_AND_ASSIGN(JsonString);
};
/**
* @brief JsonNumber is the base class for various integer / number
* classes.
*
* This allows inequality comparisons between values that represent numbers.
*/
class JsonNumber : public JsonLeafValue {
public:
/**
* @brief Checks if the remainder if non-0;
*/
virtual bool MultipleOf(const JsonNumber &other) const = 0;
/**
* @brief Less than operator.
*/
virtual bool operator<(const JsonNumber &other) const = 0;
/**
* @brief Less than or equals operator.
*/
bool operator<=(const JsonNumber &other) const {
return *this == other || *this < other;
}
/**
* @brief Greater than operator.
*/
bool operator>(const JsonNumber &other) const {
return !(*this <= other);
}
/**
* @brief Greater than or equals operator.
*/
bool operator>=(const JsonNumber &other) const {
return !(*this < other);
}
/**
* @privatesection
*/
virtual int Compare(const JsonUInt &value) const = 0;
virtual int Compare(const JsonInt &value) const = 0;
virtual int Compare(const JsonUInt64 &value) const = 0;
virtual int Compare(const JsonInt64 &value) const = 0;
virtual int Compare(const JsonDouble &value) const = 0;
virtual bool FactorOf(const JsonUInt &value) const = 0;
virtual bool FactorOf(const JsonInt &value) const = 0;
virtual bool FactorOf(const JsonUInt64 &value) const = 0;
virtual bool FactorOf(const JsonInt64 &value) const = 0;
virtual bool FactorOf(const JsonDouble &value) const = 0;
/**
* @endsection
*/
};
/**
* @brief An unsigned 32bit int value.
*/
class JsonUInt: public JsonNumber {
public:
/**
* @brief Create a new JsonUInt
* @param value the unsigned int to use.
*/
explicit JsonUInt(unsigned int value) : m_value(value) {}
bool operator==(const JsonValue &other) const { return other.Equals(*this); }
bool operator<(const JsonNumber &other) const {
return other.Compare(*this) == 1;
}
bool MultipleOf(const JsonNumber &other) const {
return other.FactorOf(*this);
}
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonUInt(m_value); }
/**
* @brief Return the uint32_t value.
*/
unsigned int Value() const { return m_value; }
/**
* @privatesection
*/
bool Equals(const JsonUInt &other) const {
return m_value == other.m_value;
}
bool Equals(const JsonInt &other) const;
bool Equals(const JsonUInt64 &other) const;
bool Equals(const JsonInt64 &other) const;
int Compare(const JsonUInt &value) const;
int Compare(const JsonInt &value) const;
int Compare(const JsonUInt64 &value) const;
int Compare(const JsonInt64 &value) const;
int Compare(const JsonDouble &value) const;
bool FactorOf(const JsonUInt &value) const;
bool FactorOf(const JsonInt &value) const;
bool FactorOf(const JsonUInt64 &value) const;
bool FactorOf(const JsonInt64 &value) const;
bool FactorOf(const JsonDouble &value) const;
/**
* @endsection
*/
private:
const unsigned int m_value;
DISALLOW_COPY_AND_ASSIGN(JsonUInt);
};
/**
* @brief A signed 32bit int value.
*/
class JsonInt: public JsonNumber {
public:
/**
* @brief Create a new JsonInt
* @param value the int to use.
*/
explicit JsonInt(int value) : m_value(value) {}
bool operator==(const JsonValue &other) const { return other.Equals(*this); }
bool operator<(const JsonNumber &other) const {
return other.Compare(*this) == 1;
}
bool MultipleOf(const JsonNumber &other) const {
return other.FactorOf(*this);
}
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonInt(m_value); }
/**
* @brief Return the int32_t value.
*/
int Value() const { return m_value; }
/**
* @privatesection
*/
bool Equals(const JsonInt &other) const {
return m_value == other.m_value;
}
bool Equals(const JsonUInt &other) const;
bool Equals(const JsonUInt64 &other) const;
bool Equals(const JsonInt64 &other) const;
int Compare(const JsonUInt &value) const;
int Compare(const JsonInt &value) const;
int Compare(const JsonUInt64 &value) const;
int Compare(const JsonInt64 &value) const;
int Compare(const JsonDouble &value) const;
bool FactorOf(const JsonUInt &value) const;
bool FactorOf(const JsonInt &value) const;
bool FactorOf(const JsonUInt64 &value) const;
bool FactorOf(const JsonInt64 &value) const;
bool FactorOf(const JsonDouble &value) const;
/**
* @endsection
*/
private:
const int m_value;
DISALLOW_COPY_AND_ASSIGN(JsonInt);
};
/**
* @brief An unsigned 64bit int value.
*/
class JsonUInt64: public JsonNumber {
public:
/**
* @brief Create a new JsonUInt64
* @param value the unsigned int 64 to use.
*/
explicit JsonUInt64(uint64_t value) : m_value(value) {}
bool operator==(const JsonValue &other) const {
return other.Equals(*this);
}
bool operator<(const JsonNumber &other) const {
return other.Compare(*this) == 1;
}
bool MultipleOf(const JsonNumber &other) const {
return other.FactorOf(*this);
}
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonUInt64(m_value); }
/**
* @brief Return the uint64_t value.
*/
uint64_t Value() const { return m_value; }
/**
* @privatesection
*/
bool Equals(const JsonUInt64 &other) const {
return m_value == other.m_value;
}
bool Equals(const JsonUInt &other) const;
bool Equals(const JsonInt &other) const;
bool Equals(const JsonInt64 &other) const;
int Compare(const JsonUInt &value) const;
int Compare(const JsonInt &value) const;
int Compare(const JsonUInt64 &value) const;
int Compare(const JsonInt64 &value) const;
int Compare(const JsonDouble &value) const;
bool FactorOf(const JsonUInt &value) const;
bool FactorOf(const JsonInt &value) const;
bool FactorOf(const JsonUInt64 &value) const;
bool FactorOf(const JsonInt64 &value) const;
bool FactorOf(const JsonDouble &value) const;
/**
* @endsection
*/
private:
const uint64_t m_value;
DISALLOW_COPY_AND_ASSIGN(JsonUInt64);
};
/**
* @brief A signed 64bit int value.
*/
class JsonInt64: public JsonNumber {
public:
/**
* @brief Create a new JsonInt64
* @param value the int 64 to use.
*/
explicit JsonInt64(int64_t value) : m_value(value) {}
bool operator==(const JsonValue &other) const {
return other.Equals(*this);
}
bool operator<(const JsonNumber &other) const {
return other.Compare(*this) == 1;
}
bool MultipleOf(const JsonNumber &other) const {
return other.FactorOf(*this);
}
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonInt64(m_value); }
/**
* @brief Return the int64_t value.
*/
int64_t Value() const { return m_value; }
/**
* @privatesection
*/
bool Equals(const JsonInt64 &other) const {
return m_value == other.m_value;
}
bool Equals(const JsonUInt &other) const;
bool Equals(const JsonInt &other) const;
bool Equals(const JsonUInt64 &other) const;
int Compare(const JsonUInt &value) const;
int Compare(const JsonInt &value) const;
int Compare(const JsonUInt64 &value) const;
int Compare(const JsonInt64 &value) const;
int Compare(const JsonDouble &value) const;
bool FactorOf(const JsonUInt &value) const;
bool FactorOf(const JsonInt &value) const;
bool FactorOf(const JsonUInt64 &value) const;
bool FactorOf(const JsonInt64 &value) const;
bool FactorOf(const JsonDouble &value) const;
/**
* @endsection
*/
private:
const int64_t m_value;
DISALLOW_COPY_AND_ASSIGN(JsonInt64);
};
/**
* @brief A double value.
*
* Double Values represent numbers which are not simple integers. They can have
* a fractional and/or exponent. A double
* value takes the form: [full].[fractional]e<sup>[exponent]</sup>.
* e.g 23.00456e<sup>-3</sup>.
*/
class JsonDouble: public JsonNumber {
public:
/**
* @struct DoubleRepresentation
* @brief Represents a JSON double value broken down as separate components.
*
* For the value 23.00456e<sup>-3</sup>:
* full: 23
* leading_fractional_zeros: 2
* fractional: 456
* exponent: -3
*/
struct DoubleRepresentation {
/** The sign of the double, true is negative, false is positive */
bool is_negative;
/** The number to the left of the decimal point */
uint64_t full;
/** The number of leading 0s in the fractional part of the double */
uint32_t leading_fractional_zeros;
/** The fractional part of the double, without the leading 0s */
uint64_t fractional;
/** The exponent, or 0 if there isn't one. */
int32_t exponent;
};
/**
* @brief Create a new JsonDouble
* @param value the double to use.
*/
explicit JsonDouble(double value);
/**
* @brief Create a new JsonDouble from separate components.
*/
explicit JsonDouble(const DoubleRepresentation &rep);
bool operator==(const JsonValue &other) const {
return other.Equals(*this);
}
bool operator<(const JsonNumber &other) const {
return other.Compare(*this) == 1;
}
bool MultipleOf(const JsonNumber &other) const {
return other.FactorOf(*this);
}
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
/**
* @brief Return the double value as a string.
*/
const std::string& ToString() const { return m_as_string; }
/**
* @brief Returns the value as a double.
*
* This may be incorrect if the value exceeds the storage space of the double.
*/
double Value() const { return m_value; }
/**
* @brief Convert a DoubleRepresentation to a double value.
* @param rep the DoubleRepresentation
* @param[out] out The value stored as a double.
* @returns false if the DoubleRepresentation can't fit in a double.
*/
static bool AsDouble(const DoubleRepresentation &rep, double *out);
/**
* @brief Convert the DoubleRepresentation to a string.
* @param rep the DoubleRepresentation
* @returns The string representation of the DoubleRepresentation.
*/
static std::string AsString(const DoubleRepresentation &rep);
JsonValue* Clone() const {
// This loses precision, maybe we should fix it?
return new JsonDouble(m_value);
}
/**
* @privatesection
*/
bool Equals(const JsonDouble &other) const {
// This is sketchy. The standard says "have the same mathematical value"
// TODO(simon): Think about this some more.
return m_value == other.m_value;
}
int Compare(const JsonUInt &value) const;
int Compare(const JsonInt &value) const;
int Compare(const JsonUInt64 &value) const;
int Compare(const JsonInt64 &value) const;
int Compare(const JsonDouble &value) const;
bool FactorOf(const JsonUInt &value) const;
bool FactorOf(const JsonInt &value) const;
bool FactorOf(const JsonUInt64 &value) const;
bool FactorOf(const JsonInt64 &value) const;
bool FactorOf(const JsonDouble &value) const;
/**
* @endsection
*/
private:
double m_value;
std::string m_as_string;
DISALLOW_COPY_AND_ASSIGN(JsonDouble);
};
/**
* @brief A Bool value
*/
class JsonBool: public JsonLeafValue {
public:
/**
* @brief Create a new JsonBool
* @param value the bool to use.
*/
explicit JsonBool(bool value) : m_value(value) {}
bool operator==(const JsonValue &other) const {
return other.Equals(*this);
}
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonBool(m_value); }
/**
* @brief Return the bool value.
*/
bool Value() const { return m_value; }
/**
* @privatesection
*/
bool Equals(const JsonBool &other) const {
return m_value == other.m_value;
}
/**
* @endsection
*/
private:
const bool m_value;
DISALLOW_COPY_AND_ASSIGN(JsonBool);
};
/**
* @brief The null value
*/
class JsonNull: public JsonLeafValue {
public:
/**
* @brief Create a new JsonNull
*/
JsonNull() {}
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonNull(); }
bool operator==(const JsonValue &other) const { return other.Equals(*this); }
/**
* @privatesection
*/
bool Equals(const JsonNull &) const { return true; }
/**
* @endsection
*/
private:
DISALLOW_COPY_AND_ASSIGN(JsonNull);
};
/**
* @brief A raw value, useful if you want to cheat.
*/
class JsonRawValue: public JsonLeafValue {
public:
/**
* @brief Create a new JsonRawValue
* @param value the raw data to insert.
*/
explicit JsonRawValue(const std::string &value) : m_value(value) {}
bool operator==(const JsonValue &other) const { return other.Equals(*this); }
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const { return new JsonRawValue(m_value); }
/**
* @brief Return the raw value as a string.
*/
const std::string& Value() const { return m_value; }
/**
* @privatesection
*/
bool Equals(const JsonRawValue &other) const {
return m_value == other.m_value;
}
/**
* @endsection
*/
private:
const std::string m_value;
DISALLOW_COPY_AND_ASSIGN(JsonRawValue);
};
/**
* @brief A JSON object.
* JSON Objects are key : value mappings, similar to dictionaries in Python.
*
* If the same key is added more than once, the latest value wins.
*
* @todo Since key names tend to reuse the same strings, it would be nice to
* intern the strings here. That's a future optimization for someone.
*/
class JsonObject: public JsonValue {
public:
/**
* @brief Create a new JsonObject
*/
JsonObject() {}
~JsonObject();
JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
bool operator==(const JsonValue &other) const {
return other.Equals(*this);
}
bool Equals(const JsonObject &other) const;
/**
* @brief Add a key to string mapping.
* @param key the key to set.
* @param value the value to add.
*/
void Add(const std::string &key, const std::string &value);
/**
* @brief Set the given key to a string value.
* @param key the key to set.
* @param value the value to add
*/
void Add(const std::string &key, const char *value);
/**
* @brief Set the given key to a unsigned int value.
* @param key the key to set.
* @param i the value to add
*/
void Add(const std::string &key, unsigned int i);
/**
* @brief Set the given key to a int value.
* @param key the key to set.
* @param i the value to add
*/
void Add(const std::string &key, int i);
/**
* @brief Set the given key to a double value.
* @param key the key to set.
* @param d the value to add
*/
void Add(const std::string &key, double d);
/**
* @brief Set the given key to a bool value.
* @param key the key to set.
* @param value the value to add
*/
void Add(const std::string &key, bool value);
/**
* @brief Set the given key to a null value.
* @param key the key to set.
*/
void Add(const std::string &key);
/**
* @brief Set the given key to a JsonObject.
* @param key the key to add
* @returns the new JsonObject.
*/
JsonObject* AddObject(const std::string &key);
/**
* @brief Set the given key to a JsonArray.
* @param key the key to add
* @returns the new JsonObject.
*/
class JsonArray* AddArray(const std::string &key);
/**
* @brief Set the key to the supplied JsonValue.
* @param key the key to add
* @param value the JsonValue object, ownership is transferred.
*/
void AddValue(const std::string &key, JsonValue *value);
/**
* @brief Set the given key to a raw value.
* @param key the key to add
* @param value the raw value to append.
*/
void AddRaw(const std::string &key, const std::string &value);
/**
* @brief Remove the JsonValue with the specified key.
* @param key the key to remove
* @returns true if the key existed and was removed, false otherwise.
*/
bool Remove(const std::string &key);
/**
* @brief Replace the key with the supplied JsonValue.
* @param key the key to add.
* @param value the JsonValue object, ownership is transferred.
* @returns true if the key was replaced, false otherwise.
*
* If the key was not replaced, the value is deleted.
*/
bool ReplaceValue(const std::string &key, JsonValue *value);
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const;
/**
* @brief Check if there are properties within the object
* @returns true if the object is empty, false if there are properties.
*/
bool IsEmpty() const { return m_members.empty(); }
unsigned int Size() const { return m_members.size(); }
/**
* @brief Visit each of the properties in this object.
*
* For each property : value, the visitor is called.
*/
void VisitProperties(JsonObjectPropertyVisitor *visitor) const;
private:
typedef std::map<std::string, JsonValue*> MemberMap;
MemberMap m_members;
DISALLOW_COPY_AND_ASSIGN(JsonObject);
};
/**
* @brief An array of JSON values.
* Arrays in JSON can contain values of different types.
*/
class JsonArray: public JsonValue {
public:
JsonArray() : m_complex_type(false) {}
~JsonArray();
JsonValue* LookupElementWithIter(JsonPointer::Iterator *iter);
bool operator==(const JsonValue &other) const {
return other.Equals(*this);
}
bool Equals(const JsonArray &other) const;
/**
* @brief Append a string value to the array
* @param value the value to append
*/
void Append(const std::string &value) {
m_values.push_back(new JsonString(value));
}
/**
* @brief Append a string value to the array
* @param value the value to append
*/
void Append(const char *value) {
m_values.push_back(new JsonString(value));
}
/**
* @brief Append an unsigned int value to the array
* @param i the value to append
*/
void Append(unsigned int i) {
m_values.push_back(new JsonUInt(i));
}
/**
* @brief Append an int value to the array
* @param i the value to append
*/
void Append(int i) {
m_values.push_back(new JsonInt(i));
}
/**
* @brief Append a bool value to the array
* @param value the value to append
*/
void Append(bool value) {
m_values.push_back(new JsonBool(value));
}
/**
* @brief Append a null value to the array
*/
void Append() {
m_values.push_back(new JsonNull());
}
/**
* @brief Append a JsonValue. Takes ownership of the pointer.
*/
void Append(JsonValue *value) {
m_values.push_back(value);
}
/**
* @brief Append a JsonValue. Takes ownership of the pointer.
*/
void Append(JsonObject *value) {
m_values.push_back(value);
m_complex_type |= !value->IsEmpty();
}
/**
* @brief Append a JsonObject to the array
* @returns the new JsonObject. Ownership is not transferred and the
* pointer is valid for the lifetime of this JsonArray.
*/
JsonObject* AppendObject() {
JsonObject *obj = new JsonObject();
m_values.push_back(obj);
m_complex_type = true;
return obj;
}
/**
* @brief Append a JsonArray to the array
* @returns the new JsonArray. Ownership is not transferred and the
* pointer is valid for the lifetime of this JsonArray.
*/
JsonArray* AppendArray() {
JsonArray *array = new JsonArray();
m_values.push_back(array);
m_complex_type = true;
return array;
}
/**
* @brief Append a JsonValue to the array.
* @param value the JsonValue to append, ownership is transferred.
*/
void AppendValue(JsonValue *value) {
m_values.push_back(value);
}
/**
* @brief Append a raw value to the array
*/
void AppendRaw(const std::string &value) {
m_values.push_back(new JsonRawValue(value));
}
/**
* @brief Remove the JsonValue at the specified index.
* @param index the index of the value to remove
* @returns true if the index is within the current array, false otherwise.
*/
bool RemoveElementAt(uint32_t index);
/**
* @brief Replace the JsonValue at the specified index.
* @param index the index of the value to replace.
* @param value the new JsonValue. Ownership is transferred.
* @returns true if the index is within the current array, false otherwise.
*/
bool ReplaceElementAt(uint32_t index, JsonValue *value);
/**
* @brief Inserts the JsonValue at the specified index.
* @param index the index to insert at.
* @param value the new JsonValue. Ownership is transferred.
* @returns true if the index is within the current array, false otherwise.
*
* Any elements at or above the index are shifted one to the right.
*/
bool InsertElementAt(uint32_t index, JsonValue *value);
void Accept(JsonValueVisitorInterface *visitor) { visitor->Visit(this); }
void Accept(JsonValueConstVisitorInterface *visitor) const {
visitor->Visit(*this);
}
JsonValue* Clone() const;
/**
* @brief Check if there are elements within the array.
* @returns true if the array is empty, false if there are elements.
*/
bool IsEmpty() const { return m_values.empty(); }
/**
* @brief Return the number of elements in the array.
*/
unsigned int Size() const { return m_values.size(); }
/**
* @brief Return the element at index i.
* @returns The JsonValue at i, of NULL if the index is out of range.
*/
const JsonValue *ElementAt(unsigned int i) const;
/**
* @brief Return true if this array contains nested arrays or objects.
*/
bool IsComplexType() const { return m_complex_type; }
private:
typedef std::vector<JsonValue*> ValuesVector;
ValuesVector m_values;
// true if this array contains a nested object or array
bool m_complex_type;
DISALLOW_COPY_AND_ASSIGN(JsonArray);
};
/**
* @brief A class used to visit Json values within a JsonObject
*/
class JsonObjectPropertyVisitor {
public:
virtual ~JsonObjectPropertyVisitor() {}
/**
* @brief Visit the value at the given property
*/
virtual void VisitProperty(const std::string &property,
const JsonValue &value) = 0;
};
/**
* @brief Downcast a JsonValue to a JsonObject
*
* This should be used rather than dynamic_cast<JsonObject> since it doesn't
* rely on RTTI.
*/
JsonObject* ObjectCast(JsonValue *value);
/**
* @brief Downcast a JsonValue to a JsonArray.
*
* This should be used rather than dynamic_cast<JsonArray> since it doesn't
* rely on RTTI.
*/
JsonArray* ArrayCast(JsonValue *value);
/**@}*/
// operator<<
std::ostream& operator<<(std::ostream &os, const JsonString &value);
std::ostream& operator<<(std::ostream &os, const JsonUInt &value);
std::ostream& operator<<(std::ostream &os, const JsonInt &value);
std::ostream& operator<<(std::ostream &os, const JsonUInt64 &value);
std::ostream& operator<<(std::ostream &os, const JsonInt64 &value);
std::ostream& operator<<(std::ostream &os, const JsonDouble &value);
std::ostream& operator<<(std::ostream &os, const JsonBool &value);
std::ostream& operator<<(std::ostream &os, const JsonNull &value);
std::ostream& operator<<(std::ostream &os, const JsonRawValue &value);
// JsonValue::NewNumberValue implementations.
template <>
inline JsonNumber* JsonValue::NewNumberValue<uint32_t>(
const uint32_t &value) {
return new JsonUInt(value);
}
template <>
inline JsonNumber* JsonValue::NewNumberValue<int32_t>(
const int32_t &value) {
return new JsonInt(value);
}
template <>
inline JsonNumber* JsonValue::NewNumberValue<uint64_t>(
const uint64_t &value) {
return new JsonUInt64(value);
}
template <>
inline JsonNumber* JsonValue::NewNumberValue<int64_t>(
const int64_t &value) {
return new JsonInt64(value);
}
template <>
inline JsonNumber* JsonValue::NewNumberValue<double>(
const double &value) {
return new JsonDouble(value);
}
template <>
inline JsonNumber* JsonValue::NewNumberValue<
JsonDouble::DoubleRepresentation>(
const JsonDouble::DoubleRepresentation &value) {
return new JsonDouble(value);
}
// JsonValue::NewValue implementations.
template <>
inline JsonValue* JsonValue::NewValue<std::string>(const std::string &value) {
return new JsonString(value);
}
template <>
inline JsonValue* JsonValue::NewValue<uint32_t>(const uint32_t &value) {
return NewNumberValue(value);
}
template <>
inline JsonValue* JsonValue::NewValue<int32_t>(const int32_t &value) {
return NewNumberValue(value);
}
template <>
inline JsonValue* JsonValue::NewValue<uint64_t>(const uint64_t &value) {
return NewNumberValue(value);
}
template <>
inline JsonValue* JsonValue::NewValue<int64_t>(const int64_t &value) {
return NewNumberValue(value);
}
template <>
inline JsonValue* JsonValue::NewValue<double>(const double &value) {
return NewNumberValue(value);
}
template <>
inline JsonValue* JsonValue::NewValue<JsonDouble::DoubleRepresentation>(
const JsonDouble::DoubleRepresentation &value) {
return NewNumberValue(value);
}
template <>
inline JsonValue* JsonValue::NewValue<bool>(const bool &value) {
return new JsonBool(value);
}
/**@}*/
} // namespace web
} // namespace ola
#endif // INCLUDE_OLA_WEB_JSON_H_
|