00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #pragma once
00039 #ifndef CPPTL_JSON_H_INCLUDED
00040 # define CPPTL_JSON_H_INCLUDED
00041
00042 # include "forwards.h"
00043 # include <string>
00044 # include <vector>
00045
00046 # ifndef JSON_USE_CPPTL_SMALLMAP
00047 # include <map>
00048 # else
00049 # include <cpptl/smallmap.h>
00050 # endif
00051 # ifdef JSON_USE_CPPTL
00052 # include <cpptl/forwards.h>
00053 # endif
00054
00057 namespace Json {
00058
00061 enum ValueType
00062 {
00063 nullValue = 0,
00064 intValue,
00065 uintValue,
00066 realValue,
00067 stringValue,
00068 booleanValue,
00069 arrayValue,
00070 objectValue
00071 };
00072
00073 enum CommentPlacement
00074 {
00075 commentBefore = 0,
00076 commentAfterOnSameLine,
00077 commentAfter,
00078 numberOfCommentPlacement
00079 };
00080
00081
00082
00083
00084
00085
00100 class JSON_API StaticString
00101 {
00102 public:
00103 explicit StaticString( const char *czstring )
00104 : str_( czstring )
00105 {
00106 }
00107
00108 operator const char *() const
00109 {
00110 return str_;
00111 }
00112
00113 const char *c_str() const
00114 {
00115 return str_;
00116 }
00117
00118 private:
00119 const char *str_;
00120 };
00121
00149 class JSON_API Value
00150 {
00151 friend class ValueIteratorBase;
00152 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00153 friend class ValueInternalLink;
00154 friend class ValueInternalMap;
00155 # endif
00156 public:
00157 typedef std::vector<std::string> Members;
00158 typedef ValueIterator iterator;
00159 typedef ValueConstIterator const_iterator;
00160 typedef Json::UInt UInt;
00161 typedef Json::Int Int;
00162 typedef UInt ArrayIndex;
00163
00164 static const Value null;
00165 static const Int minInt;
00166 static const Int maxInt;
00167 static const UInt maxUInt;
00168
00169 private:
00170 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00171 # ifndef JSON_VALUE_USE_INTERNAL_MAP
00172 class CZString
00173 {
00174 public:
00175 enum DuplicationPolicy
00176 {
00177 noDuplication = 0,
00178 duplicate,
00179 duplicateOnCopy
00180 };
00181 CZString( int index );
00182 CZString( const char *cstr, DuplicationPolicy allocate );
00183 CZString( const CZString &other );
00184 ~CZString();
00185 CZString &operator =( const CZString &other );
00186 bool operator<( const CZString &other ) const;
00187 bool operator==( const CZString &other ) const;
00188 int index() const;
00189 const char *c_str() const;
00190 bool isStaticString() const;
00191 private:
00192 void swap( CZString &other );
00193 const char *cstr_;
00194 int index_;
00195 };
00196
00197 public:
00198 # ifndef JSON_USE_CPPTL_SMALLMAP
00199 typedef std::map<CZString, Value> ObjectValues;
00200 # else
00201 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
00202 # endif // ifndef JSON_USE_CPPTL_SMALLMAP
00203 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
00204 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00205
00206 public:
00222 Value( ValueType type = nullValue );
00223 Value( Int value );
00224 Value( UInt value );
00225 Value( double value );
00226 Value( const char *value );
00227 Value( const char *beginValue, const char *endValue );
00238 Value( const StaticString &value );
00239 Value( const std::string &value );
00240 # ifdef JSON_USE_CPPTL
00241 Value( const CppTL::ConstString &value );
00242 # endif
00243 Value( bool value );
00244 Value( const Value &other );
00245 ~Value();
00246
00247 Value &operator=( const Value &other );
00251 void swap( Value &other );
00252
00253 ValueType type() const;
00254
00255 bool operator <( const Value &other ) const;
00256 bool operator <=( const Value &other ) const;
00257 bool operator >=( const Value &other ) const;
00258 bool operator >( const Value &other ) const;
00259
00260 bool operator ==( const Value &other ) const;
00261 bool operator !=( const Value &other ) const;
00262
00263 int compare( const Value &other );
00264
00265 const char *asCString() const;
00266 std::string asString() const;
00267 # ifdef JSON_USE_CPPTL
00268 CppTL::ConstString asConstString() const;
00269 # endif
00270 Int asInt() const;
00271 UInt asUInt() const;
00272 double asDouble() const;
00273 bool asBool() const;
00274
00275 bool isNull() const;
00276 bool isBool() const;
00277 bool isInt() const;
00278 bool isUInt() const;
00279 bool isIntegral() const;
00280 bool isDouble() const;
00281 bool isNumeric() const;
00282 bool isString() const;
00283 bool isArray() const;
00284 bool isObject() const;
00285
00286 bool isConvertibleTo( ValueType other ) const;
00287
00289 UInt size() const;
00290
00293 bool empty() const;
00294
00296 bool operator!() const;
00297
00301 void clear();
00302
00308 void resize( UInt size );
00309
00315 Value &operator[]( UInt index );
00319 const Value &operator[]( UInt index ) const;
00322 Value get( UInt index,
00323 const Value &defaultValue ) const;
00325 bool isValidIndex( UInt index ) const;
00329 Value &append( const Value &value );
00330
00332 Value &operator[]( const char *key );
00334 const Value &operator[]( const char *key ) const;
00336 Value &operator[]( const std::string &key );
00338 const Value &operator[]( const std::string &key ) const;
00350 Value &operator[]( const StaticString &key );
00351 # ifdef JSON_USE_CPPTL
00352
00353 Value &operator[]( const CppTL::ConstString &key );
00355 const Value &operator[]( const CppTL::ConstString &key ) const;
00356 # endif
00357
00358 Value get( const char *key,
00359 const Value &defaultValue ) const;
00361 Value get( const std::string &key,
00362 const Value &defaultValue ) const;
00363 # ifdef JSON_USE_CPPTL
00364
00365 Value get( const CppTL::ConstString &key,
00366 const Value &defaultValue ) const;
00367 # endif
00368
00369
00370
00371
00372
00373
00374 Value removeMember( const char* key );
00376 Value removeMember( const std::string &key );
00377
00379 bool isMember( const char *key ) const;
00381 bool isMember( const std::string &key ) const;
00382 # ifdef JSON_USE_CPPTL
00383
00384 bool isMember( const CppTL::ConstString &key ) const;
00385 # endif
00386
00392 Members getMemberNames() const;
00393
00394
00395
00396
00397
00398
00400 void setComment( const char *comment,
00401 CommentPlacement placement );
00403 void setComment( const std::string &comment,
00404 CommentPlacement placement );
00405 bool hasComment( CommentPlacement placement ) const;
00407 std::string getComment( CommentPlacement placement ) const;
00408
00409 std::string toStyledString() const;
00410
00411 const_iterator begin() const;
00412 const_iterator end() const;
00413
00414 iterator begin();
00415 iterator end();
00416
00417 private:
00418 Value &resolveReference( const char *key,
00419 bool isStatic );
00420
00421 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00422 inline bool isItemAvailable() const
00423 {
00424 return itemIsUsed_ == 0;
00425 }
00426
00427 inline void setItemUsed( bool isUsed = true )
00428 {
00429 itemIsUsed_ = isUsed ? 1 : 0;
00430 }
00431
00432 inline bool isMemberNameStatic() const
00433 {
00434 return memberNameIsStatic_ == 0;
00435 }
00436
00437 inline void setMemberNameIsStatic( bool isStatic )
00438 {
00439 memberNameIsStatic_ = isStatic ? 1 : 0;
00440 }
00441 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
00442
00443 private:
00444 struct CommentInfo
00445 {
00446 CommentInfo();
00447 ~CommentInfo();
00448
00449 void setComment( const char *text );
00450
00451 char *comment_;
00452 };
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 union ValueHolder
00464 {
00465 Int int_;
00466 UInt uint_;
00467 double real_;
00468 bool bool_;
00469 char *string_;
00470 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00471 ValueInternalArray *array_;
00472 ValueInternalMap *map_;
00473 #else
00474 ObjectValues *map_;
00475 # endif
00476 } value_;
00477 ValueType type_;
00478 bool allocated_;
00479 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00480 unsigned int itemIsUsed_ : 1;
00481 int memberNameIsStatic_ : 1;
00482 # endif
00483 CommentInfo *comments_;
00484 };
00485
00486
00489 class PathArgument
00490 {
00491 public:
00492 friend class Path;
00493
00494 PathArgument();
00495 PathArgument( UInt index );
00496 PathArgument( const char *key );
00497 PathArgument( const std::string &key );
00498
00499 private:
00500 enum Kind
00501 {
00502 kindNone = 0,
00503 kindIndex,
00504 kindKey
00505 };
00506 std::string key_;
00507 UInt index_;
00508 Kind kind_;
00509 };
00510
00522 class Path
00523 {
00524 public:
00525 Path( const std::string &path,
00526 const PathArgument &a1 = PathArgument(),
00527 const PathArgument &a2 = PathArgument(),
00528 const PathArgument &a3 = PathArgument(),
00529 const PathArgument &a4 = PathArgument(),
00530 const PathArgument &a5 = PathArgument() );
00531
00532 const Value &resolve( const Value &root ) const;
00533 Value resolve( const Value &root,
00534 const Value &defaultValue ) const;
00536 Value &make( Value &root ) const;
00537
00538 private:
00539 typedef std::vector<const PathArgument *> InArgs;
00540 typedef std::vector<PathArgument> Args;
00541
00542 void makePath( const std::string &path,
00543 const InArgs &in );
00544 void addPathInArg( const std::string &path,
00545 const InArgs &in,
00546 InArgs::const_iterator &itInArg,
00547 PathArgument::Kind kind );
00548 void invalidPath( const std::string &path,
00549 int location ) const;
00550
00551 Args args_;
00552 };
00553
00561 class ValueAllocator
00562 {
00563 public:
00564 enum { unknown = (unsigned)-1 };
00565
00566 virtual ~ValueAllocator();
00567
00568 virtual char *makeMemberName( const char *memberName ) = 0;
00569 virtual void releaseMemberName( char *memberName ) = 0;
00570 virtual char *duplicateStringValue( const char *value,
00571 unsigned int length = unknown ) = 0;
00572 virtual void releaseStringValue( char *value ) = 0;
00573 };
00574
00575 #ifdef JSON_VALUE_USE_INTERNAL_MAP
00576
00620 class JSON_API ValueMapAllocator
00621 {
00622 public:
00623 virtual ~ValueMapAllocator();
00624 virtual ValueInternalMap *newMap() = 0;
00625 virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0;
00626 virtual void destructMap( ValueInternalMap *map ) = 0;
00627 virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0;
00628 virtual void releaseMapBuckets( ValueInternalLink *links ) = 0;
00629 virtual ValueInternalLink *allocateMapLink() = 0;
00630 virtual void releaseMapLink( ValueInternalLink *link ) = 0;
00631 };
00632
00636 class JSON_API ValueInternalLink
00637 {
00638 public:
00639 enum { itemPerLink = 6 };
00640 enum InternalFlags {
00641 flagAvailable = 0,
00642 flagUsed = 1
00643 };
00644
00645 ValueInternalLink();
00646
00647 ~ValueInternalLink();
00648
00649 Value items_[itemPerLink];
00650 char *keys_[itemPerLink];
00651 ValueInternalLink *previous_;
00652 ValueInternalLink *next_;
00653 };
00654
00655
00668 class JSON_API ValueInternalMap
00669 {
00670 friend class ValueIteratorBase;
00671 friend class Value;
00672 public:
00673 typedef unsigned int HashKey;
00674 typedef unsigned int BucketIndex;
00675
00676 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00677 struct IteratorState
00678 {
00679 IteratorState()
00680 : map_(0)
00681 , link_(0)
00682 , itemIndex_(0)
00683 , bucketIndex_(0)
00684 {
00685 }
00686 ValueInternalMap *map_;
00687 ValueInternalLink *link_;
00688 BucketIndex itemIndex_;
00689 BucketIndex bucketIndex_;
00690 };
00691 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00692
00693 ValueInternalMap();
00694 ValueInternalMap( const ValueInternalMap &other );
00695 ValueInternalMap &operator =( const ValueInternalMap &other );
00696 ~ValueInternalMap();
00697
00698 void swap( ValueInternalMap &other );
00699
00700 BucketIndex size() const;
00701
00702 void clear();
00703
00704 bool reserveDelta( BucketIndex growth );
00705
00706 bool reserve( BucketIndex newItemCount );
00707
00708 const Value *find( const char *key ) const;
00709
00710 Value *find( const char *key );
00711
00712 Value &resolveReference( const char *key,
00713 bool isStatic );
00714
00715 void remove( const char *key );
00716
00717 void doActualRemove( ValueInternalLink *link,
00718 BucketIndex index,
00719 BucketIndex bucketIndex );
00720
00721 ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex );
00722
00723 Value &setNewItem( const char *key,
00724 bool isStatic,
00725 ValueInternalLink *link,
00726 BucketIndex index );
00727
00728 Value &unsafeAdd( const char *key,
00729 bool isStatic,
00730 HashKey hashedKey );
00731
00732 HashKey hash( const char *key ) const;
00733
00734 int compare( const ValueInternalMap &other ) const;
00735
00736 private:
00737 void makeBeginIterator( IteratorState &it ) const;
00738 void makeEndIterator( IteratorState &it ) const;
00739 static bool equals( const IteratorState &x, const IteratorState &other );
00740 static void increment( IteratorState &iterator );
00741 static void incrementBucket( IteratorState &iterator );
00742 static void decrement( IteratorState &iterator );
00743 static const char *key( const IteratorState &iterator );
00744 static const char *key( const IteratorState &iterator, bool &isStatic );
00745 static Value &value( const IteratorState &iterator );
00746 static int distance( const IteratorState &x, const IteratorState &y );
00747
00748 private:
00749 ValueInternalLink *buckets_;
00750 ValueInternalLink *tailLink_;
00751 BucketIndex bucketsSize_;
00752 BucketIndex itemCount_;
00753 };
00754
00766 class JSON_API ValueInternalArray
00767 {
00768 friend class Value;
00769 friend class ValueIteratorBase;
00770 public:
00771 enum { itemsPerPage = 8 };
00772 typedef Value::ArrayIndex ArrayIndex;
00773 typedef unsigned int PageIndex;
00774
00775 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00776 struct IteratorState
00777 {
00778 IteratorState()
00779 : array_(0)
00780 , currentPageIndex_(0)
00781 , currentItemIndex_(0)
00782 {
00783 }
00784 ValueInternalArray *array_;
00785 Value **currentPageIndex_;
00786 unsigned int currentItemIndex_;
00787 };
00788 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00789
00790 ValueInternalArray();
00791 ValueInternalArray( const ValueInternalArray &other );
00792 ValueInternalArray &operator =( const ValueInternalArray &other );
00793 ~ValueInternalArray();
00794 void swap( ValueInternalArray &other );
00795
00796 void clear();
00797 void resize( ArrayIndex newSize );
00798
00799 Value &resolveReference( ArrayIndex index );
00800
00801 Value *find( ArrayIndex index ) const;
00802
00803 ArrayIndex size() const;
00804
00805 int compare( const ValueInternalArray &other ) const;
00806
00807 private:
00808 static bool equals( const IteratorState &x, const IteratorState &other );
00809 static void increment( IteratorState &iterator );
00810 static void decrement( IteratorState &iterator );
00811 static Value &dereference( const IteratorState &iterator );
00812 static Value &unsafeDereference( const IteratorState &iterator );
00813 static int distance( const IteratorState &x, const IteratorState &y );
00814 static ArrayIndex indexOf( const IteratorState &iterator );
00815 void makeBeginIterator( IteratorState &it ) const;
00816 void makeEndIterator( IteratorState &it ) const;
00817 void makeIterator( IteratorState &it, ArrayIndex index ) const;
00818
00819 void makeIndexValid( ArrayIndex index );
00820
00821 Value **pages_;
00822 ArrayIndex size_;
00823 PageIndex pageCount_;
00824 };
00825
00885 class JSON_API ValueArrayAllocator
00886 {
00887 public:
00888 virtual ~ValueArrayAllocator();
00889 virtual ValueInternalArray *newArray() = 0;
00890 virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0;
00891 virtual void destructArray( ValueInternalArray *array ) = 0;
00903 virtual void reallocateArrayPageIndex( Value **&indexes,
00904 ValueInternalArray::PageIndex &indexCount,
00905 ValueInternalArray::PageIndex minNewIndexCount ) = 0;
00906 virtual void releaseArrayPageIndex( Value **indexes,
00907 ValueInternalArray::PageIndex indexCount ) = 0;
00908 virtual Value *allocateArrayPage() = 0;
00909 virtual void releaseArrayPage( Value *value ) = 0;
00910 };
00911 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
00912
00913
00917 class ValueIteratorBase
00918 {
00919 public:
00920 typedef unsigned int size_t;
00921 typedef int difference_type;
00922 typedef ValueIteratorBase SelfType;
00923
00924 ValueIteratorBase();
00925 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00926 explicit ValueIteratorBase( const Value::ObjectValues::iterator ¤t );
00927 #else
00928 ValueIteratorBase( const ValueInternalArray::IteratorState &state );
00929 ValueIteratorBase( const ValueInternalMap::IteratorState &state );
00930 #endif
00931
00932 bool operator ==( const SelfType &other ) const
00933 {
00934 return isEqual( other );
00935 }
00936
00937 bool operator !=( const SelfType &other ) const
00938 {
00939 return !isEqual( other );
00940 }
00941
00942 difference_type operator -( const SelfType &other ) const
00943 {
00944 return computeDistance( other );
00945 }
00946
00948 Value key() const;
00949
00951 UInt index() const;
00952
00954 const char *memberName() const;
00955
00956 protected:
00957 Value &deref() const;
00958
00959 void increment();
00960
00961 void decrement();
00962
00963 difference_type computeDistance( const SelfType &other ) const;
00964
00965 bool isEqual( const SelfType &other ) const;
00966
00967 void copy( const SelfType &other );
00968
00969 private:
00970 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00971 Value::ObjectValues::iterator current_;
00972
00973 bool isNull_;
00974 #else
00975 union
00976 {
00977 ValueInternalArray::IteratorState array_;
00978 ValueInternalMap::IteratorState map_;
00979 } iterator_;
00980 bool isArray_;
00981 #endif
00982 };
00983
00987 class ValueConstIterator : public ValueIteratorBase
00988 {
00989 friend class Value;
00990 public:
00991 typedef unsigned int size_t;
00992 typedef int difference_type;
00993 typedef const Value &reference;
00994 typedef const Value *pointer;
00995 typedef ValueConstIterator SelfType;
00996
00997 ValueConstIterator();
00998 private:
01001 #ifndef JSON_VALUE_USE_INTERNAL_MAP
01002 explicit ValueConstIterator( const Value::ObjectValues::iterator ¤t );
01003 #else
01004 ValueConstIterator( const ValueInternalArray::IteratorState &state );
01005 ValueConstIterator( const ValueInternalMap::IteratorState &state );
01006 #endif
01007 public:
01008 SelfType &operator =( const ValueIteratorBase &other );
01009
01010 SelfType operator++( int )
01011 {
01012 SelfType temp( *this );
01013 ++*this;
01014 return temp;
01015 }
01016
01017 SelfType operator--( int )
01018 {
01019 SelfType temp( *this );
01020 --*this;
01021 return temp;
01022 }
01023
01024 SelfType &operator--()
01025 {
01026 decrement();
01027 return *this;
01028 }
01029
01030 SelfType &operator++()
01031 {
01032 increment();
01033 return *this;
01034 }
01035
01036 reference operator *() const
01037 {
01038 return deref();
01039 }
01040 };
01041
01042
01045 class ValueIterator : public ValueIteratorBase
01046 {
01047 friend class Value;
01048 public:
01049 typedef unsigned int size_t;
01050 typedef int difference_type;
01051 typedef Value &reference;
01052 typedef Value *pointer;
01053 typedef ValueIterator SelfType;
01054
01055 ValueIterator();
01056 ValueIterator( const ValueConstIterator &other );
01057 ValueIterator( const ValueIterator &other );
01058 private:
01061 #ifndef JSON_VALUE_USE_INTERNAL_MAP
01062 explicit ValueIterator( const Value::ObjectValues::iterator ¤t );
01063 #else
01064 ValueIterator( const ValueInternalArray::IteratorState &state );
01065 ValueIterator( const ValueInternalMap::IteratorState &state );
01066 #endif
01067 public:
01068
01069 SelfType &operator =( const SelfType &other );
01070
01071 SelfType operator++( int )
01072 {
01073 SelfType temp( *this );
01074 ++*this;
01075 return temp;
01076 }
01077
01078 SelfType operator--( int )
01079 {
01080 SelfType temp( *this );
01081 --*this;
01082 return temp;
01083 }
01084
01085 SelfType &operator--()
01086 {
01087 decrement();
01088 return *this;
01089 }
01090
01091 SelfType &operator++()
01092 {
01093 increment();
01094 return *this;
01095 }
01096
01097 reference operator *() const
01098 {
01099 return deref();
01100 }
01101 };
01102
01103
01104 }
01105
01106
01107 #endif // CPPTL_JSON_H_INCLUDED