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;
00047 class JSON_API RuntimeError;
00054 class JSON_API LogicError;
00055
00057 void throwRuntimeError(std::string const& msg);
00059 void throwLogicError(std::string const& msg);
00060
00063 enum ValueType {
00064 nullValue = 0,
00065 intValue,
00066 uintValue,
00067 realValue,
00068 stringValue,
00069 booleanValue,
00070 arrayValue,
00071 objectValue
00072 };
00073
00074 enum CommentPlacement {
00075 commentBefore = 0,
00076 commentAfterOnSameLine,
00077 commentAfter,
00078
00079 numberOfCommentPlacement
00080 };
00081
00082
00083
00084
00085
00086
00101 class JSON_API StaticString {
00102 public:
00103 explicit StaticString(const char* czstring) : c_str_(czstring) {}
00104
00105 operator const char*() const { return c_str_; }
00106
00107 const char* c_str() const { return c_str_; }
00108
00109 private:
00110 const char* c_str_;
00111 };
00112
00147 class JSON_API Value {
00148 friend class ValueIteratorBase;
00149 public:
00150 typedef std::vector<std::string> Members;
00151 typedef ValueIterator iterator;
00152 typedef ValueConstIterator const_iterator;
00153 typedef Json::UInt UInt;
00154 typedef Json::Int Int;
00155 #if defined(JSON_HAS_INT64)
00156 typedef Json::UInt64 UInt64;
00157 typedef Json::Int64 Int64;
00158 #endif // defined(JSON_HAS_INT64)
00159 typedef Json::LargestInt LargestInt;
00160 typedef Json::LargestUInt LargestUInt;
00161 typedef Json::ArrayIndex ArrayIndex;
00162
00163 static const Value& nullRef;
00164 #if !defined(__ARMEL__)
00165
00166 static const Value null;
00167 #endif
00168
00169 static const LargestInt minLargestInt;
00171 static const LargestInt maxLargestInt;
00173 static const LargestUInt maxLargestUInt;
00174
00176 static const Int minInt;
00178 static const Int maxInt;
00180 static const UInt maxUInt;
00181
00182 #if defined(JSON_HAS_INT64)
00183
00184 static const Int64 minInt64;
00186 static const Int64 maxInt64;
00188 static const UInt64 maxUInt64;
00189 #endif // defined(JSON_HAS_INT64)
00190
00191 private:
00192 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00193 class CZString {
00194 public:
00195 enum DuplicationPolicy {
00196 noDuplication = 0,
00197 duplicate,
00198 duplicateOnCopy
00199 };
00200 CZString(ArrayIndex index);
00201 CZString(char const* str, unsigned length, DuplicationPolicy allocate);
00202 CZString(CZString const& other);
00203 ~CZString();
00204 CZString& operator=(CZString other);
00205 bool operator<(CZString const& other) const;
00206 bool operator==(CZString const& other) const;
00207 ArrayIndex index() const;
00208
00209 char const* data() const;
00210 unsigned length() const;
00211 bool isStaticString() const;
00212
00213 private:
00214 void swap(CZString& other);
00215
00216 struct StringStorage {
00217 DuplicationPolicy policy_: 2;
00218 unsigned length_: 30;
00219 };
00220
00221 char const* cstr_;
00222 union {
00223 ArrayIndex index_;
00224 StringStorage storage_;
00225 };
00226 };
00227
00228 public:
00229 #ifndef JSON_USE_CPPTL_SMALLMAP
00230 typedef std::map<CZString, Value> ObjectValues;
00231 #else
00232 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
00233 #endif // ifndef JSON_USE_CPPTL_SMALLMAP
00234 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00235
00236 public:
00252 Value(ValueType type = nullValue);
00253 Value(Int value);
00254 Value(UInt value);
00255 #if defined(JSON_HAS_INT64)
00256 Value(Int64 value);
00257 Value(UInt64 value);
00258 #endif // if defined(JSON_HAS_INT64)
00259 Value(double value);
00260 Value(const char* value);
00261 Value(const char* beginValue, const char* endValue);
00262
00277 Value(const StaticString& value);
00278 Value(const std::string& value);
00279 #ifdef JSON_USE_CPPTL
00280 Value(const CppTL::ConstString& value);
00281 #endif
00282 Value(bool value);
00284 Value(const Value& other);
00285 ~Value();
00286
00289 Value &operator=(const Value &other);
00291 void swap(Value& other);
00293 void swapPayload(Value& other);
00294
00295 ValueType type() const;
00296
00298 bool operator<(const Value& other) const;
00299 bool operator<=(const Value& other) const;
00300 bool operator>=(const Value& other) const;
00301 bool operator>(const Value& other) const;
00302 bool operator==(const Value& other) const;
00303 bool operator!=(const Value& other) const;
00304 int compare(const Value& other) const;
00305
00306 const char* asCString() const;
00307 std::string asString() const;
00308
00311 bool getString(
00312 char const** str, char const** end) const;
00313 #ifdef JSON_USE_CPPTL
00314 CppTL::ConstString asConstString() const;
00315 #endif
00316 Int asInt() const;
00317 UInt asUInt() const;
00318 #if defined(JSON_HAS_INT64)
00319 Int64 asInt64() const;
00320 UInt64 asUInt64() const;
00321 #endif // if defined(JSON_HAS_INT64)
00322 LargestInt asLargestInt() const;
00323 LargestUInt asLargestUInt() const;
00324 float asFloat() const;
00325 double asDouble() const;
00326 bool asBool() const;
00327
00328 bool isNull() const;
00329 bool isBool() const;
00330 bool isInt() const;
00331 bool isInt64() const;
00332 bool isUInt() const;
00333 bool isUInt64() const;
00334 bool isIntegral() const;
00335 bool isDouble() const;
00336 bool isNumeric() const;
00337 bool isString() const;
00338 bool isArray() const;
00339 bool isObject() const;
00340
00341 bool isConvertibleTo(ValueType other) const;
00342
00344 ArrayIndex size() const;
00345
00348 bool empty() const;
00349
00351 bool operator!() const;
00352
00356 void clear();
00357
00363 void resize(ArrayIndex size);
00364
00371 Value& operator[](ArrayIndex index);
00372
00379 Value& operator[](int index);
00380
00384 const Value& operator[](ArrayIndex index) const;
00385
00389 const Value& operator[](int index) const;
00390
00394 Value get(ArrayIndex index, const Value& defaultValue) const;
00396 bool isValidIndex(ArrayIndex index) const;
00400 Value& append(const Value& value);
00401
00405 Value& operator[](const char* key);
00408 const Value& operator[](const char* key) const;
00411 Value& operator[](const std::string& key);
00415 const Value& operator[](const std::string& key) const;
00428 Value& operator[](const StaticString& key);
00429 #ifdef JSON_USE_CPPTL
00430
00431 Value& operator[](const CppTL::ConstString& key);
00434 const Value& operator[](const CppTL::ConstString& key) const;
00435 #endif
00436
00437
00438 Value get(const char* key, const Value& defaultValue) const;
00442 Value get(const char* key, const char* end, const Value& defaultValue) const;
00446 Value get(const std::string& key, const Value& defaultValue) const;
00447 #ifdef JSON_USE_CPPTL
00448
00449
00450 Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
00451 #endif
00452
00453
00454
00455 Value const* find(char const* key, char const* end) const;
00459 Value const* demand(char const* key, char const* end);
00467 Value removeMember(const char* key);
00471 Value removeMember(const std::string& key);
00474 bool removeMember(const char* key, Value* removed);
00481 bool removeMember(std::string const& key, Value* removed);
00483 bool removeMember(const char* key, const char* end, Value* removed);
00490 bool removeIndex(ArrayIndex i, Value* removed);
00491
00494 bool isMember(const char* key) const;
00497 bool isMember(const std::string& key) const;
00499 bool isMember(const char* key, const char* end) const;
00500 #ifdef JSON_USE_CPPTL
00501
00502 bool isMember(const CppTL::ConstString& key) const;
00503 #endif
00504
00510 Members getMemberNames() const;
00511
00512
00513
00514
00515
00516
00518 JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
00519 void setComment(const char* comment, CommentPlacement placement);
00521 void setComment(const char* comment, size_t len, CommentPlacement placement);
00523 void setComment(const std::string& comment, CommentPlacement placement);
00524 bool hasComment(CommentPlacement placement) const;
00526 std::string getComment(CommentPlacement placement) const;
00527
00528 std::string toStyledString() const;
00529
00530 const_iterator begin() const;
00531 const_iterator end() const;
00532
00533 iterator begin();
00534 iterator end();
00535
00536 private:
00537 void initBasic(ValueType type, bool allocated = false);
00538
00539 Value& resolveReference(const char* key);
00540 Value& resolveReference(const char* key, const char* end);
00541
00542 struct CommentInfo {
00543 CommentInfo();
00544 ~CommentInfo();
00545
00546 void setComment(const char* text, size_t len);
00547
00548 char* comment_;
00549 };
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 union ValueHolder {
00561 LargestInt int_;
00562 LargestUInt uint_;
00563 double real_;
00564 bool bool_;
00565 char* string_;
00566 ObjectValues* map_;
00567 } value_;
00568 ValueType type_ : 8;
00569 unsigned int allocated_ : 1;
00570
00571 CommentInfo* comments_;
00572 };
00573
00577 class JSON_API PathArgument {
00578 public:
00579 friend class Path;
00580
00581 PathArgument();
00582 PathArgument(ArrayIndex index);
00583 PathArgument(const char* key);
00584 PathArgument(const std::string& key);
00585
00586 private:
00587 enum Kind {
00588 kindNone = 0,
00589 kindIndex,
00590 kindKey
00591 };
00592 std::string key_;
00593 ArrayIndex index_;
00594 Kind kind_;
00595 };
00596
00608 class JSON_API Path {
00609 public:
00610 Path(const std::string& path,
00611 const PathArgument& a1 = PathArgument(),
00612 const PathArgument& a2 = PathArgument(),
00613 const PathArgument& a3 = PathArgument(),
00614 const PathArgument& a4 = PathArgument(),
00615 const PathArgument& a5 = PathArgument());
00616
00617 const Value& resolve(const Value& root) const;
00618 Value resolve(const Value& root, const Value& defaultValue) const;
00621 Value& make(Value& root) const;
00622
00623 private:
00624 typedef std::vector<const PathArgument*> InArgs;
00625 typedef std::vector<PathArgument> Args;
00626
00627 void makePath(const std::string& path, const InArgs& in);
00628 void addPathInArg(const std::string& path,
00629 const InArgs& in,
00630 InArgs::const_iterator& itInArg,
00631 PathArgument::Kind kind);
00632 void invalidPath(const std::string& path, int location);
00633
00634 Args args_;
00635 };
00636
00640 class JSON_API ValueIteratorBase {
00641 public:
00642 typedef std::bidirectional_iterator_tag iterator_category;
00643 typedef unsigned int size_t;
00644 typedef int difference_type;
00645 typedef ValueIteratorBase SelfType;
00646
00647 ValueIteratorBase();
00648 explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
00649
00650 bool operator==(const SelfType& other) const { return isEqual(other); }
00651
00652 bool operator!=(const SelfType& other) const { return !isEqual(other); }
00653
00654 difference_type operator-(const SelfType& other) const {
00655 return other.computeDistance(*this);
00656 }
00657
00660 Value key() const;
00661
00663 UInt index() const;
00664
00668 std::string name() const;
00669
00673 JSONCPP_DEPRECATED("Use `key = name();` instead.")
00674 char const* memberName() const;
00678 char const* memberName(char const** end) const;
00679
00680 protected:
00681 Value& deref() const;
00682
00683 void increment();
00684
00685 void decrement();
00686
00687 difference_type computeDistance(const SelfType& other) const;
00688
00689 bool isEqual(const SelfType& other) const;
00690
00691 void copy(const SelfType& other);
00692
00693 private:
00694 Value::ObjectValues::iterator current_;
00695
00696 bool isNull_;
00697 };
00698
00702 class JSON_API ValueConstIterator : public ValueIteratorBase {
00703 friend class Value;
00704
00705 public:
00706 typedef const Value value_type;
00707
00708
00709 typedef const Value& reference;
00710 typedef const Value* pointer;
00711 typedef ValueConstIterator SelfType;
00712
00713 ValueConstIterator();
00714
00715 private:
00718 explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
00719 public:
00720 SelfType& operator=(const ValueIteratorBase& other);
00721
00722 SelfType operator++(int) {
00723 SelfType temp(*this);
00724 ++*this;
00725 return temp;
00726 }
00727
00728 SelfType operator--(int) {
00729 SelfType temp(*this);
00730 --*this;
00731 return temp;
00732 }
00733
00734 SelfType& operator--() {
00735 decrement();
00736 return *this;
00737 }
00738
00739 SelfType& operator++() {
00740 increment();
00741 return *this;
00742 }
00743
00744 reference operator*() const { return deref(); }
00745
00746 pointer operator->() const { return &deref(); }
00747 };
00748
00751 class JSON_API ValueIterator : public ValueIteratorBase {
00752 friend class Value;
00753
00754 public:
00755 typedef Value value_type;
00756 typedef unsigned int size_t;
00757 typedef int difference_type;
00758 typedef Value& reference;
00759 typedef Value* pointer;
00760 typedef ValueIterator SelfType;
00761
00762 ValueIterator();
00763 ValueIterator(const ValueConstIterator& other);
00764 ValueIterator(const ValueIterator& other);
00765
00766 private:
00769 explicit ValueIterator(const Value::ObjectValues::iterator& current);
00770 public:
00771 SelfType& operator=(const SelfType& other);
00772
00773 SelfType operator++(int) {
00774 SelfType temp(*this);
00775 ++*this;
00776 return temp;
00777 }
00778
00779 SelfType operator--(int) {
00780 SelfType temp(*this);
00781 --*this;
00782 return temp;
00783 }
00784
00785 SelfType& operator--() {
00786 decrement();
00787 return *this;
00788 }
00789
00790 SelfType& operator++() {
00791 increment();
00792 return *this;
00793 }
00794
00795 reference operator*() const { return deref(); }
00796
00797 pointer operator->() const { return &deref(); }
00798 };
00799
00800 }
00801
00802
00803 namespace std {
00805 template<>
00806 inline void swap(Json::Value& a, Json::Value& b) { a.swap(b); }
00807 }
00808
00809
00810 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00811 #pragma warning(pop)
00812 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00813
00814 #endif // CPPTL_JSON_H_INCLUDED