00001
00002
00003
00004
00005
00006 #ifndef CPPTL_JSON_H_INCLUDED
00007 #define CPPTL_JSON_H_INCLUDED
00008
00009 #if !defined(JSON_IS_AMALGAMATION)
00010 #include "forwards.h"
00011 #endif // if !defined(JSON_IS_AMALGAMATION)
00012 #include <string>
00013 #include <vector>
00014 #include <exception>
00015
00016 #ifndef JSON_USE_CPPTL_SMALLMAP
00017 #include <map>
00018 #else
00019 #include <cpptl/smallmap.h>
00020 #endif
00021 #ifdef JSON_USE_CPPTL
00022 #include <cpptl/forwards.h>
00023 #endif
00024
00025
00026
00027 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00028 #pragma warning(push)
00029 #pragma warning(disable : 4251)
00030 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00031
00034 namespace Json {
00035
00040 class JSON_API Exception : public std::exception {
00041 public:
00042 Exception(std::string const& msg);
00043 virtual ~Exception() throw();
00044 virtual char const* what() const throw();
00045 protected:
00046 std::string const msg_;
00047 };
00048
00055 class JSON_API RuntimeError : public Exception {
00056 public:
00057 RuntimeError(std::string const& msg);
00058 };
00059
00066 class JSON_API LogicError : public Exception {
00067 public:
00068 LogicError(std::string const& msg);
00069 };
00070
00072 void throwRuntimeError(std::string const& msg);
00074 void throwLogicError(std::string const& msg);
00075
00078 enum ValueType {
00079 nullValue = 0,
00080 intValue,
00081 uintValue,
00082 realValue,
00083 stringValue,
00084 booleanValue,
00085 arrayValue,
00086 objectValue
00087 };
00088
00089 enum CommentPlacement {
00090 commentBefore = 0,
00091 commentAfterOnSameLine,
00092 commentAfter,
00093
00094 numberOfCommentPlacement
00095 };
00096
00097
00098
00099
00100
00101
00116 class JSON_API StaticString {
00117 public:
00118 explicit StaticString(const char* czstring) : c_str_(czstring) {}
00119
00120 operator const char*() const { return c_str_; }
00121
00122 const char* c_str() const { return c_str_; }
00123
00124 private:
00125 const char* c_str_;
00126 };
00127
00162 class JSON_API Value {
00163 friend class ValueIteratorBase;
00164 public:
00165 typedef std::vector<std::string> Members;
00166 typedef ValueIterator iterator;
00167 typedef ValueConstIterator const_iterator;
00168 typedef Json::UInt UInt;
00169 typedef Json::Int Int;
00170 #if defined(JSON_HAS_INT64)
00171 typedef Json::UInt64 UInt64;
00172 typedef Json::Int64 Int64;
00173 #endif // defined(JSON_HAS_INT64)
00174 typedef Json::LargestInt LargestInt;
00175 typedef Json::LargestUInt LargestUInt;
00176 typedef Json::ArrayIndex ArrayIndex;
00177
00178 static const Value& nullRef;
00179 #if !defined(__ARMEL__)
00181 static const Value null;
00182 #endif
00184 static const LargestInt minLargestInt;
00186 static const LargestInt maxLargestInt;
00188 static const LargestUInt maxLargestUInt;
00189
00191 static const Int minInt;
00193 static const Int maxInt;
00195 static const UInt maxUInt;
00196
00197 #if defined(JSON_HAS_INT64)
00199 static const Int64 minInt64;
00201 static const Int64 maxInt64;
00203 static const UInt64 maxUInt64;
00204 #endif // defined(JSON_HAS_INT64)
00205
00206 private:
00207 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00208 class CZString {
00209 public:
00210 enum DuplicationPolicy {
00211 noDuplication = 0,
00212 duplicate,
00213 duplicateOnCopy
00214 };
00215 CZString(ArrayIndex index);
00216 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
00217 CZString(CZString const& other);
00218 ~CZString();
00219 CZString& operator=(CZString other);
00220 bool operator<(CZString const& other) const;
00221 bool operator==(CZString const& other) const;
00222 ArrayIndex index() const;
00223
00224 char const* data() const;
00225 unsigned length() const;
00226 bool isStaticString() const;
00227
00228 private:
00229 void swap(CZString& other);
00230
00231 struct StringStorage {
00232 unsigned policy_: 2;
00233 unsigned length_: 30;
00234 };
00235
00236 char const* cstr_;
00237 union {
00238 ArrayIndex index_;
00239 StringStorage storage_;
00240 };
00241 };
00242
00243 public:
00244 #ifndef JSON_USE_CPPTL_SMALLMAP
00245 typedef std::map<CZString, Value> ObjectValues;
00246 #else
00247 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
00248 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
00249 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00250
00251 public:
00267 Value(ValueType type = nullValue);
00268 Value(Int value);
00269 Value(UInt value);
00270 #if defined(JSON_HAS_INT64)
00271 Value(Int64 value);
00272 Value(UInt64 value);
00273 #endif // if defined(JSON_HAS_INT64)
00274 Value(double value);
00275 Value(const char* value);
00276 Value(const char* begin, const char* end);
00277
00292 Value(const StaticString& value);
00293 Value(const std::string& value);
00294 #ifdef JSON_USE_CPPTL
00295 Value(const CppTL::ConstString& value);
00296 #endif
00297 Value(bool value);
00299 Value(const Value& other);
00300 ~Value();
00301
00304 Value &operator=(const Value &other);
00306 void swap(Value& other);
00308 void swapPayload(Value& other);
00309
00310 ValueType type() const;
00311
00313 bool operator<(const Value& other) const;
00314 bool operator<=(const Value& other) const;
00315 bool operator>=(const Value& other) const;
00316 bool operator>(const Value& other) const;
00317 bool operator==(const Value& other) const;
00318 bool operator!=(const Value& other) const;
00319 int compare(const Value& other) const;
00320
00321 const char* asCString() const;
00322 std::string asString() const;
00323
00326 bool getString(
00327 char const** begin, char const** end) const;
00328 #ifdef JSON_USE_CPPTL
00329 CppTL::ConstString asConstString() const;
00330 #endif
00331 Int asInt() const;
00332 UInt asUInt() const;
00333 #if defined(JSON_HAS_INT64)
00334 Int64 asInt64() const;
00335 UInt64 asUInt64() const;
00336 #endif // if defined(JSON_HAS_INT64)
00337 LargestInt asLargestInt() const;
00338 LargestUInt asLargestUInt() const;
00339 float asFloat() const;
00340 double asDouble() const;
00341 bool asBool() const;
00342
00343 bool isNull() const;
00344 bool isBool() const;
00345 bool isInt() const;
00346 bool isInt64() const;
00347 bool isUInt() const;
00348 bool isUInt64() const;
00349 bool isIntegral() const;
00350 bool isDouble() const;
00351 bool isNumeric() const;
00352 bool isString() const;
00353 bool isArray() const;
00354 bool isObject() const;
00355
00356 bool isConvertibleTo(ValueType other) const;
00357
00359 ArrayIndex size() const;
00360
00363 bool empty() const;
00364
00366 bool operator!() const;
00367
00371 void clear();
00372
00378 void resize(ArrayIndex size);
00379
00386 Value& operator[](ArrayIndex index);
00387
00394 Value& operator[](int index);
00395
00399 const Value& operator[](ArrayIndex index) const;
00400
00404 const Value& operator[](int index) const;
00405
00409 Value get(ArrayIndex index, const Value& defaultValue) const;
00411 bool isValidIndex(ArrayIndex index) const;
00415 Value& append(const Value& value);
00416
00420 Value& operator[](const char* key);
00423 const Value& operator[](const char* key) const;
00426 Value& operator[](const std::string& key);
00430 const Value& operator[](const std::string& key) const;
00443 Value& operator[](const StaticString& key);
00444 #ifdef JSON_USE_CPPTL
00446 Value& operator[](const CppTL::ConstString& key);
00449 const Value& operator[](const CppTL::ConstString& key) const;
00450 #endif
00453 Value get(const char* key, const Value& defaultValue) const;
00457 Value get(const char* begin, const char* end, const Value& defaultValue) const;
00461 Value get(const std::string& key, const Value& defaultValue) const;
00462 #ifdef JSON_USE_CPPTL
00465 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
00466 #endif
00470 Value const* find(char const* begin, char const* end) const;
00474 Value const* demand(char const* begin, char const* end);
00482 Value removeMember(const char* key);
00486 Value removeMember(const std::string& key);
00489 bool removeMember(const char* key, Value* removed);
00490
00496 bool removeMember(std::string const& key, Value* removed);
00498 bool removeMember(const char* begin, const char* end, Value* removed);
00505 bool removeIndex(ArrayIndex i, Value* removed);
00506
00509 bool isMember(const char* key) const;
00512 bool isMember(const std::string& key) const;
00514 bool isMember(const char* begin, const char* end) const;
00515 #ifdef JSON_USE_CPPTL
00517 bool isMember(const CppTL::ConstString& key) const;
00518 #endif
00519
00525 Members getMemberNames() const;
00526
00527
00528
00529
00530
00531
00533 JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
00534 void setComment(const char* comment, CommentPlacement placement);
00536 void setComment(const char* comment, size_t len, CommentPlacement placement);
00538 void setComment(const std::string& comment, CommentPlacement placement);
00539 bool hasComment(CommentPlacement placement) const;
00541 std::string getComment(CommentPlacement placement) const;
00542
00543 std::string toStyledString() const;
00544
00545 const_iterator begin() const;
00546 const_iterator end() const;
00547
00548 iterator begin();
00549 iterator end();
00550
00551 private:
00552 void initBasic(ValueType type, bool allocated = false);
00553
00554 Value& resolveReference(const char* key);
00555 Value& resolveReference(const char* key, const char* end);
00556
00557 struct CommentInfo {
00558 CommentInfo();
00559 ~CommentInfo();
00560
00561 void setComment(const char* text, size_t len);
00562
00563 char* comment_;
00564 };
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 union ValueHolder {
00576 LargestInt int_;
00577 LargestUInt uint_;
00578 double real_;
00579 bool bool_;
00580 char* string_;
00581 ObjectValues* map_;
00582 } value_;
00583 ValueType type_ : 8;
00584 unsigned int allocated_ : 1;
00585
00586 CommentInfo* comments_;
00587 };
00588
00592 class JSON_API PathArgument {
00593 public:
00594 friend class Path;
00595
00596 PathArgument();
00597 PathArgument(ArrayIndex index);
00598 PathArgument(const char* key);
00599 PathArgument(const std::string& key);
00600
00601 private:
00602 enum Kind {
00603 kindNone = 0,
00604 kindIndex,
00605 kindKey
00606 };
00607 std::string key_;
00608 ArrayIndex index_;
00609 Kind kind_;
00610 };
00611
00623 class JSON_API Path {
00624 public:
00625 Path(const std::string& path,
00626 const PathArgument& a1 = PathArgument(),
00627 const PathArgument& a2 = PathArgument(),
00628 const PathArgument& a3 = PathArgument(),
00629 const PathArgument& a4 = PathArgument(),
00630 const PathArgument& a5 = PathArgument());
00631
00632 const Value& resolve(const Value& root) const;
00633 Value resolve(const Value& root, const Value& defaultValue) const;
00636 Value& make(Value& root) const;
00637
00638 private:
00639 typedef std::vector<const PathArgument*> InArgs;
00640 typedef std::vector<PathArgument> Args;
00641
00642 void makePath(const std::string& path, const InArgs& in);
00643 void addPathInArg(const std::string& path,
00644 const InArgs& in,
00645 InArgs::const_iterator& itInArg,
00646 PathArgument::Kind kind);
00647 void invalidPath(const std::string& path, int location);
00648
00649 Args args_;
00650 };
00651
00655 class JSON_API ValueIteratorBase {
00656 public:
00657 typedef std::bidirectional_iterator_tag iterator_category;
00658 typedef unsigned int size_t;
00659 typedef int difference_type;
00660 typedef ValueIteratorBase SelfType;
00661
00662 bool operator==(const SelfType& other) const { return isEqual(other); }
00663
00664 bool operator!=(const SelfType& other) const { return !isEqual(other); }
00665
00666 difference_type operator-(const SelfType& other) const {
00667 return other.computeDistance(*this);
00668 }
00669
00672 Value key() const;
00673
00675 UInt index() const;
00676
00680 std::string name() const;
00681
00685 JSONCPP_DEPRECATED("Use `key = name();` instead.")
00686 char const* memberName() const;
00690 char const* memberName(char const** end) const;
00691
00692 protected:
00693 Value& deref() const;
00694
00695 void increment();
00696
00697 void decrement();
00698
00699 difference_type computeDistance(const SelfType& other) const;
00700
00701 bool isEqual(const SelfType& other) const;
00702
00703 void copy(const SelfType& other);
00704
00705 private:
00706 Value::ObjectValues::iterator current_;
00707
00708 bool isNull_;
00709
00710 public:
00711
00712
00713 ValueIteratorBase();
00714 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
00715 };
00716
00720 class JSON_API ValueConstIterator : public ValueIteratorBase {
00721 friend class Value;
00722
00723 public:
00724 typedef const Value value_type;
00725
00726
00727 typedef const Value& reference;
00728 typedef const Value* pointer;
00729 typedef ValueConstIterator SelfType;
00730
00731 ValueConstIterator();
00732
00733 private:
00736 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
00737 public:
00738 SelfType& operator=(const ValueIteratorBase& other);
00739
00740 SelfType operator++(int) {
00741 SelfType temp(*this);
00742 ++*this;
00743 return temp;
00744 }
00745
00746 SelfType operator--(int) {
00747 SelfType temp(*this);
00748 --*this;
00749 return temp;
00750 }
00751
00752 SelfType& operator--() {
00753 decrement();
00754 return *this;
00755 }
00756
00757 SelfType& operator++() {
00758 increment();
00759 return *this;
00760 }
00761
00762 reference operator*() const { return deref(); }
00763
00764 pointer operator->() const { return &deref(); }
00765 };
00766
00769 class JSON_API ValueIterator : public ValueIteratorBase {
00770 friend class Value;
00771
00772 public:
00773 typedef Value value_type;
00774 typedef unsigned int size_t;
00775 typedef int difference_type;
00776 typedef Value& reference;
00777 typedef Value* pointer;
00778 typedef ValueIterator SelfType;
00779
00780 ValueIterator();
00781 ValueIterator(const ValueConstIterator& other);
00782 ValueIterator(const ValueIterator& other);
00783
00784 private:
00787 explicit ValueIterator(const Value::ObjectValues::iterator& current);
00788 public:
00789 SelfType& operator=(const SelfType& other);
00790
00791 SelfType operator++(int) {
00792 SelfType temp(*this);
00793 ++*this;
00794 return temp;
00795 }
00796
00797 SelfType operator--(int) {
00798 SelfType temp(*this);
00799 --*this;
00800 return temp;
00801 }
00802
00803 SelfType& operator--() {
00804 decrement();
00805 return *this;
00806 }
00807
00808 SelfType& operator++() {
00809 increment();
00810 return *this;
00811 }
00812
00813 reference operator*() const { return deref(); }
00814
00815 pointer operator->() const { return &deref(); }
00816 };
00817
00818 }
00819
00820
00821 namespace std {
00823 template<>
00824 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
00825 }
00826
00827
00828 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00829 #pragma warning(pop)
00830 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00831
00832 #endif // CPPTL_JSON_H_INCLUDED