1 #ifndef CPPTL_JSON_H_INCLUDED 2 # define CPPTL_JSON_H_INCLUDED 8 # ifndef JSON_USE_CPPTL_SMALLMAP 11 # include <cpptl/smallmap.h> 13 # ifdef JSON_USE_CPPTL 14 # include <cpptl/forwards.h> 73 operator const char *()
const 117 # ifdef JSON_VALUE_USE_INTERNAL_MAP 118 friend class ValueInternalLink;
119 friend class ValueInternalMap;
135 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION 136 # ifndef JSON_VALUE_USE_INTERNAL_MAP 151 bool operator<(
const CZString &other )
const;
152 bool operator==(
const CZString &other )
const;
154 const char *c_str()
const;
155 bool isStaticString()
const;
163 # ifndef JSON_USE_CPPTL_SMALLMAP 166 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
167 # endif // ifndef JSON_USE_CPPTL_SMALLMAP 168 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP 169 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION 190 Value(
double value );
191 Value(
const char *value );
192 Value(
const char *beginValue,
const char *endValue );
204 Value(
const std::string &value );
205 # ifdef JSON_USE_CPPTL 206 Value(
const CppTL::ConstString &value );
216 void swap(
Value &other );
220 bool operator <(
const Value &other )
const;
221 bool operator <=(
const Value &other )
const;
222 bool operator >=(
const Value &other )
const;
223 bool operator >(
const Value &other )
const;
225 bool operator ==(
const Value &other )
const;
226 bool operator !=(
const Value &other )
const;
228 int compare(
const Value &other );
230 const char *asCString()
const;
231 std::string asString()
const;
232 # ifdef JSON_USE_CPPTL 233 CppTL::ConstString asConstString()
const;
237 double asDouble()
const;
244 bool isIntegral()
const;
245 bool isDouble()
const;
246 bool isNumeric()
const;
247 bool isString()
const;
248 bool isArray()
const;
249 bool isNonNullArray()
const;
250 bool isObject()
const;
251 bool isNonNullObject()
const;
253 bool isConvertibleTo(
ValueType other )
const;
263 bool operator!()
const;
275 void resize( UInt size );
282 Value &operator[]( UInt index );
286 const Value &operator[]( UInt index )
const;
289 Value get( UInt index,
290 const Value &defaultValue )
const;
292 bool isValidIndex( UInt index )
const;
299 Value &operator[](
const char *key );
301 const Value &operator[](
const char *key )
const;
303 Value &operator[](
const std::string &key );
305 const Value &operator[](
const std::string &key )
const;
318 # ifdef JSON_USE_CPPTL 319 Value &operator[](
const CppTL::ConstString &key );
322 const Value &operator[](
const CppTL::ConstString &key )
const;
324 Value get(
const char *key,
326 const Value &defaultValue )
const;
328 Value get(
const std::string &key,
329 const Value &defaultValue )
const;
330 # ifdef JSON_USE_CPPTL 331 Value get(
const CppTL::ConstString &key,
333 const Value &defaultValue )
const;
335 Value removeMember(
const char* key );
343 Value removeMember(
const std::string &key );
346 bool isMember(
const char *key )
const;
348 bool isMember(
const std::string &key )
const;
349 # ifdef JSON_USE_CPPTL 350 bool isMember(
const CppTL::ConstString &key )
const;
359 Members getMemberNames()
const;
367 void setComment(
const char *comment,
370 void setComment(
const std::string &comment,
376 std::string toStyledString()
const;
378 const_iterator begin()
const;
379 const_iterator end()
const;
385 Value &resolveReference(
const char *key,
388 # ifdef JSON_VALUE_USE_INTERNAL_MAP 389 inline bool isItemAvailable()
const 391 return itemIsUsed_ == 0;
394 inline void setItemUsed(
bool isUsed =
true )
396 itemIsUsed_ = isUsed ? 1 : 0;
399 inline bool isMemberNameStatic()
const 401 return memberNameIsStatic_ == 0;
404 inline void setMemberNameIsStatic(
bool isStatic )
406 memberNameIsStatic_ = isStatic ? 1 : 0;
408 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP 416 void setComment(
const char *text );
437 # ifdef JSON_VALUE_USE_INTERNAL_MAP 438 ValueInternalArray *array_;
439 ValueInternalMap *map_;
446 # ifdef JSON_VALUE_USE_INTERNAL_MAP 447 unsigned int itemIsUsed_ : 1;
448 int memberNameIsStatic_ : 1;
492 Path(
const std::string &path,
499 const Value &resolve(
const Value &root )
const;
501 const Value &defaultValue )
const;
506 typedef std::vector<const PathArgument *>
InArgs;
507 typedef std::vector<PathArgument>
Args;
509 void makePath(
const std::string &path,
511 void addPathInArg(
const std::string &path,
513 InArgs::const_iterator &itInArg,
515 void invalidPath(
const std::string &path,
531 static const unsigned int unknown =
static_cast<unsigned int>(-1);
535 virtual char *makeMemberName(
const char *memberName ) = 0;
536 virtual void releaseMemberName(
char *memberName ) = 0;
537 virtual char *duplicateStringValue(
const char *value,
538 unsigned int length = unknown ) = 0;
539 virtual void releaseStringValue(
char *value ) = 0;
542 #ifdef JSON_VALUE_USE_INTERNAL_MAP 590 virtual ~ValueMapAllocator();
591 virtual ValueInternalMap *newMap() = 0;
592 virtual ValueInternalMap *newMapCopy(
const ValueInternalMap &other ) = 0;
593 virtual void destructMap( ValueInternalMap *map ) = 0;
594 virtual ValueInternalLink *allocateMapBuckets(
unsigned int size ) = 0;
595 virtual void releaseMapBuckets( ValueInternalLink *links ) = 0;
596 virtual ValueInternalLink *allocateMapLink() = 0;
597 virtual void releaseMapLink( ValueInternalLink *link ) = 0;
606 enum { itemPerLink = 6 };
614 ~ValueInternalLink();
616 Value items_[itemPerLink];
617 char *keys_[itemPerLink];
618 ValueInternalLink *previous_;
619 ValueInternalLink *next_;
640 typedef unsigned int HashKey;
641 typedef unsigned int BucketIndex;
643 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION 653 ValueInternalMap *map_;
654 ValueInternalLink *link_;
655 BucketIndex itemIndex_;
656 BucketIndex bucketIndex_;
658 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION 661 ValueInternalMap(
const ValueInternalMap &other );
662 ValueInternalMap &operator =(
const ValueInternalMap &other );
665 void swap( ValueInternalMap &other );
667 BucketIndex size()
const;
671 bool reserveDelta( BucketIndex growth );
673 bool reserve( BucketIndex newItemCount );
675 const Value *find(
const char *key )
const;
677 Value *find(
const char *key );
679 Value &resolveReference(
const char *key,
682 void remove(
const char *key );
684 void doActualRemove( ValueInternalLink *link,
686 BucketIndex bucketIndex );
688 ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex );
690 Value &setNewItem(
const char *key,
692 ValueInternalLink *link,
695 Value &unsafeAdd(
const char *key,
699 HashKey hash(
const char *key )
const;
701 int compare(
const ValueInternalMap &other )
const;
704 void makeBeginIterator( IteratorState &it )
const;
705 void makeEndIterator( IteratorState &it )
const;
706 static bool equals(
const IteratorState &x,
const IteratorState &other );
707 static void increment( IteratorState &iterator );
708 static void incrementBucket( IteratorState &iterator );
709 static void decrement( IteratorState &iterator );
710 static const char *key(
const IteratorState &iterator );
711 static const char *key(
const IteratorState &iterator,
bool &isStatic );
712 static Value &value(
const IteratorState &iterator );
713 static int distance(
const IteratorState &x,
const IteratorState &y );
716 ValueInternalLink *buckets_;
717 ValueInternalLink *tailLink_;
718 BucketIndex bucketsSize_;
719 BucketIndex itemCount_;
738 enum { itemsPerPage = 8 };
740 typedef unsigned int PageIndex;
742 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION 747 , currentPageIndex_(0)
748 , currentItemIndex_(0)
751 ValueInternalArray *array_;
752 Value **currentPageIndex_;
753 unsigned int currentItemIndex_;
755 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION 757 ValueInternalArray();
758 ValueInternalArray(
const ValueInternalArray &other );
759 ValueInternalArray &operator =(
const ValueInternalArray &other );
760 ~ValueInternalArray();
761 void swap( ValueInternalArray &other );
764 void resize( ArrayIndex newSize );
766 Value &resolveReference( ArrayIndex index );
768 Value *find( ArrayIndex index )
const;
770 ArrayIndex size()
const;
772 int compare(
const ValueInternalArray &other )
const;
775 static bool equals(
const IteratorState &x,
const IteratorState &other );
776 static void increment( IteratorState &iterator );
777 static void decrement( IteratorState &iterator );
778 static Value &dereference(
const IteratorState &iterator );
779 static Value &unsafeDereference(
const IteratorState &iterator );
780 static int distance(
const IteratorState &x,
const IteratorState &y );
781 static ArrayIndex indexOf(
const IteratorState &iterator );
782 void makeBeginIterator( IteratorState &it )
const;
783 void makeEndIterator( IteratorState &it )
const;
784 void makeIterator( IteratorState &it, ArrayIndex index )
const;
786 void makeIndexValid( ArrayIndex index );
790 PageIndex pageCount_;
855 virtual ~ValueArrayAllocator();
856 virtual ValueInternalArray *newArray() = 0;
857 virtual ValueInternalArray *newArrayCopy(
const ValueInternalArray &other ) = 0;
858 virtual void destructArray( ValueInternalArray *array ) = 0;
870 virtual void reallocateArrayPageIndex(
Value **&indexes,
871 ValueInternalArray::PageIndex &indexCount,
872 ValueInternalArray::PageIndex minNewIndexCount ) = 0;
873 virtual void releaseArrayPageIndex(
Value **indexes,
874 ValueInternalArray::PageIndex indexCount ) = 0;
875 virtual Value *allocateArrayPage() = 0;
876 virtual void releaseArrayPage(
Value *value ) = 0;
878 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP 892 #ifndef JSON_VALUE_USE_INTERNAL_MAP 899 bool operator ==(
const SelfType &other )
const 901 return isEqual( other );
904 bool operator !=(
const SelfType &other )
const 906 return !isEqual( other );
909 difference_type operator -(
const SelfType &other )
const 911 return computeDistance( other );
921 const char *memberName()
const;
924 Value &deref()
const;
930 difference_type computeDistance(
const SelfType &other )
const;
932 bool isEqual(
const SelfType &other )
const;
934 void copy(
const SelfType &other );
937 #ifndef JSON_VALUE_USE_INTERNAL_MAP 944 ValueInternalArray::IteratorState array_;
945 ValueInternalMap::IteratorState map_;
968 #ifndef JSON_VALUE_USE_INTERNAL_MAP 979 SelfType temp( *
this );
986 SelfType temp( *
this );
1003 reference operator *()
const 1028 #ifndef JSON_VALUE_USE_INTERNAL_MAP 1029 explicit ValueIterator(
const Value::ObjectValues::iterator ¤t );
1031 ValueIterator(
const ValueInternalArray::IteratorState &state );
1032 ValueIterator(
const ValueInternalMap::IteratorState &state );
1036 SelfType &operator =(
const SelfType &other );
1040 SelfType temp( *
this );
1047 SelfType temp( *
this );
1064 reference operator *()
const 1076 #endif // CPPTL_JSON_H_INCLUDED char * string_
Definition: value.h:436
int64_t Int64
Definition: forwards.h:24
UInt ArrayIndex
Definition: value.h:127
DuplicationPolicy
Definition: value.h:140
Kind
Definition: value.h:467
#define JSON_API
Definition: config.h:40
std::string key_
Definition: value.h:473
a comment placed on the line before a value
Definition: value.h:40
ObjectValues * map_
Definition: value.h:441
SelfType operator--(int)
Definition: value.h:1045
SelfType & operator++()
Definition: value.h:997
std::vector< const PathArgument * > InArgs
Definition: value.h:506
array value (ordered list)
Definition: value.h:34
UInt index_
Definition: value.h:474
static const Int maxInt
Definition: value.h:131
unsigned int size_t
Definition: value.h:1016
a comment on the line after a value (only make sense for root value)
Definition: value.h:42
std::vector< std::string > Members
Definition: value.h:122
Experimental and untested: represents a "path" to access a node.
Definition: value.h:489
signed integer value
Definition: value.h:29
UTF-8 string value.
Definition: value.h:32
Iterator for object and array value.
Definition: value.h:1012
int difference_type
Definition: value.h:959
int allocated_
Definition: value.h:445
SelfType & operator--()
Definition: value.h:1052
SelfType operator++(int)
Definition: value.h:977
static const Value null
Definition: value.h:129
bool isNull_
Definition: value.h:940
bool value
Definition: value.h:33
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
json::UInt64 UInt
Definition: value.h:125
json::Int64 Int
Definition: value.h:126
Lightweight wrapper to tag static string.
Definition: value.h:65
Value & reference
Definition: value.h:1018
Represents a JSON value.
Definition: value.h:114
ValueIteratorBase SelfType
Definition: value.h:889
const Value & reference
Definition: value.h:960
Args args_
Definition: value.h:518
Int index_
Definition: value.h:159
static const UInt maxUInt
Definition: value.h:132
int difference_type
Definition: value.h:888
base class for Value iterators.
Definition: value.h:884
StaticString(const char *czstring)
Definition: value.h:68
const Value * pointer
Definition: value.h:961
a comment just after a value on the same line
Definition: value.h:41
Kind kind_
Definition: value.h:475
SelfType operator--(int)
Definition: value.h:984
std::vector< PathArgument > Args
Definition: value.h:507
Value::ObjectValues::iterator current_
Definition: value.h:938
ValueConstIterator const_iterator
Definition: value.h:124
unsigned integer value
Definition: value.h:30
uint64_t UInt64
Definition: forwards.h:25
Value * pointer
Definition: value.h:1019
static const Int minInt
Definition: value.h:130
bool bool_
Definition: value.h:435
const char * str_
Definition: value.h:84
'null' value
Definition: value.h:28
UInt uint_
Definition: value.h:433
const char * cstr_
Definition: value.h:158
ValueConstIterator SelfType
Definition: value.h:962
ValueType type_
Definition: value.h:444
ValueType
Type of the value held by a Value object.
Definition: value.h:26
std::map< CZString, Value > ObjectValues
Definition: value.h:164
object value (collection of name/value pairs).
Definition: value.h:35
uint32_t UInt
Definition: forwards.h:23
SelfType & operator++()
Definition: value.h:1058
Experimental and untested: represents an element of the "path" to access a node.
Definition: value.h:456
const iterator for object and array value.
Definition: value.h:954
unsigned int size_t
Definition: value.h:887
int difference_type
Definition: value.h:1017
unsigned int size_t
Definition: value.h:958
ValueIterator iterator
Definition: value.h:123
double real_
Definition: value.h:434
CommentPlacement
Definition: value.h:38
Int int_
Definition: value.h:432
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: value.h:528
const char * c_str() const
Definition: value.h:78
CommentInfo * comments_
Definition: value.h:450
double value
Definition: value.h:31
SelfType operator++(int)
Definition: value.h:1038
ValueIterator SelfType
Definition: value.h:1020
SelfType & operator--()
Definition: value.h:991