39 #ifndef CPPTL_JSON_H_INCLUDED
40 # define CPPTL_JSON_H_INCLUDED
42 # include "forwards.h"
46 # ifndef JSON_USE_CPPTL_SMALLMAP
49 # include <cpptl/smallmap.h>
51 # ifdef JSON_USE_CPPTL
52 # include <cpptl/forwards.h>
78 numberOfCommentPlacement
108 operator const char *()
const
113 const char *c_str()
const
152 # ifdef JSON_VALUE_USE_INTERNAL_MAP
153 friend class ValueInternalLink;
154 friend class ValueInternalMap;
157 typedef std::vector<std::string> Members;
160 typedef Json::UInt UInt;
161 typedef Json::Int Int;
162 typedef UInt ArrayIndex;
164 static const Value null;
165 static const Int minInt;
166 static const Int maxInt;
167 static const UInt maxUInt;
170 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
171 # ifndef JSON_VALUE_USE_INTERNAL_MAP
175 enum DuplicationPolicy
182 CZString(
const char *cstr, DuplicationPolicy allocate );
186 bool operator<(
const CZString &other )
const;
187 bool operator==(
const CZString &other )
const;
189 const char *c_str()
const;
190 bool isStaticString()
const;
198 # ifndef JSON_USE_CPPTL_SMALLMAP
199 typedef std::map<CZString, Value> ObjectValues;
201 typedef CppTL::SmallMap<CZString, Value> ObjectValues;
202 # endif // ifndef JSON_USE_CPPTL_SMALLMAP
203 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
204 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
225 Value(
double value );
226 Value(
const char *value );
227 Value(
const char *beginValue,
const char *endValue );
239 Value(
const std::string &value );
240 # ifdef JSON_USE_CPPTL
241 Value(
const CppTL::ConstString &value );
251 void swap(
Value &other );
255 bool operator <(
const Value &other )
const;
256 bool operator <=(
const Value &other )
const;
257 bool operator >=(
const Value &other )
const;
258 bool operator >(
const Value &other )
const;
260 bool operator ==(
const Value &other )
const;
261 bool operator !=(
const Value &other )
const;
263 int compare(
const Value &other );
265 const char *asCString()
const;
266 std::string asString();
267 # ifdef JSON_USE_CPPTL
268 CppTL::ConstString asConstString()
const;
272 double asDouble()
const;
279 bool isIntegral()
const;
280 bool isDouble()
const;
281 bool isNumeric()
const;
282 bool isString()
const;
283 bool isArray()
const;
284 bool isObject()
const;
286 bool isConvertibleTo(
ValueType other )
const;
296 bool operator!()
const;
308 void resize( UInt size );
315 Value &operator[]( UInt index );
319 const Value &operator[]( UInt index )
const;
322 Value get( UInt index,
323 const Value &defaultValue )
const;
325 bool isValidIndex( UInt index )
const;
332 Value &operator[](
const char *key );
334 const Value &operator[](
const char *key )
const;
336 Value &operator[](
const std::string &key );
338 const Value &operator[](
const std::string &key )
const;
351 # ifdef JSON_USE_CPPTL
352 Value &operator[](
const CppTL::ConstString &key );
355 const Value &operator[](
const CppTL::ConstString &key )
const;
357 Value get(
const char *key,
359 const Value &defaultValue )
const;
361 Value get(
const std::string &key,
362 const Value &defaultValue )
const;
363 # ifdef JSON_USE_CPPTL
364 Value get(
const CppTL::ConstString &key,
366 const Value &defaultValue )
const;
368 Value removeMember(
const char* key );
376 Value removeMember(
const std::string &key );
379 bool isMember(
const char *key )
const;
381 bool isMember(
const std::string &key )
const;
382 # ifdef JSON_USE_CPPTL
383 bool isMember(
const CppTL::ConstString &key )
const;
392 Members getMemberNames()
const;
400 void setComment(
const char *comment,
403 void setComment(
const std::string &comment,
409 std::string toStyledString()
const;
418 Value &resolveReference(
const char *key,
421 # ifdef JSON_VALUE_USE_INTERNAL_MAP
422 inline bool isItemAvailable()
const
424 return itemIsUsed_ == 0;
427 inline void setItemUsed(
bool isUsed =
true )
429 itemIsUsed_ = isUsed ? 1 : 0;
432 inline bool isMemberNameStatic()
const
434 return memberNameIsStatic_ == 0;
437 inline void setMemberNameIsStatic(
bool isStatic )
439 memberNameIsStatic_ = isStatic ? 1 : 0;
441 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
449 void setComment(
const char *text );
470 # ifdef JSON_VALUE_USE_INTERNAL_MAP
471 ValueInternalArray *array_;
472 ValueInternalMap *map_;
479 # ifdef JSON_VALUE_USE_INTERNAL_MAP
480 unsigned int itemIsUsed_ : 1;
481 int memberNameIsStatic_ : 1;
484 char *value_as_string_;
526 Path(
const std::string &path,
533 const Value &resolve(
const Value &root )
const;
535 const Value &defaultValue )
const;
540 typedef std::vector<const PathArgument *> InArgs;
541 typedef std::vector<PathArgument> Args;
543 void makePath(
const std::string &path,
545 void addPathInArg(
const std::string &path,
547 InArgs::const_iterator &itInArg,
548 PathArgument::Kind kind );
549 void invalidPath(
const std::string &path,
550 int location )
const;
565 enum { unknown = (unsigned)-1 };
569 virtual char *makeMemberName(
const char *memberName ) = 0;
570 virtual void releaseMemberName(
char *memberName ) = 0;
571 virtual char *duplicateStringValue(
const char *value,
572 unsigned int length = unknown ) = 0;
573 virtual void releaseStringValue(
char *value ) = 0;
576 #ifdef JSON_VALUE_USE_INTERNAL_MAP
621 class JSON_API ValueMapAllocator
624 virtual ~ValueMapAllocator();
625 virtual ValueInternalMap *newMap() = 0;
626 virtual ValueInternalMap *newMapCopy(
const ValueInternalMap &other ) = 0;
627 virtual void destructMap( ValueInternalMap *map ) = 0;
628 virtual ValueInternalLink *allocateMapBuckets(
unsigned int size ) = 0;
629 virtual void releaseMapBuckets( ValueInternalLink *links ) = 0;
630 virtual ValueInternalLink *allocateMapLink() = 0;
631 virtual void releaseMapLink( ValueInternalLink *link ) = 0;
637 class JSON_API ValueInternalLink
640 enum { itemPerLink = 6 };
648 ~ValueInternalLink();
650 Value items_[itemPerLink];
651 char *keys_[itemPerLink];
652 ValueInternalLink *previous_;
653 ValueInternalLink *next_;
669 class JSON_API ValueInternalMap
674 typedef unsigned int HashKey;
675 typedef unsigned int BucketIndex;
677 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
687 ValueInternalMap *map_;
688 ValueInternalLink *link_;
689 BucketIndex itemIndex_;
690 BucketIndex bucketIndex_;
692 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
695 ValueInternalMap(
const ValueInternalMap &other );
696 ValueInternalMap &operator =(
const ValueInternalMap &other );
699 void swap( ValueInternalMap &other );
701 BucketIndex size()
const;
705 bool reserveDelta( BucketIndex growth );
707 bool reserve( BucketIndex newItemCount );
709 const Value *find(
const char *key )
const;
711 Value *find(
const char *key );
713 Value &resolveReference(
const char *key,
716 void remove(
const char *key );
718 void doActualRemove( ValueInternalLink *link,
720 BucketIndex bucketIndex );
722 ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex );
724 Value &setNewItem(
const char *key,
726 ValueInternalLink *link,
729 Value &unsafeAdd(
const char *key,
733 HashKey hash(
const char *key )
const;
735 int compare(
const ValueInternalMap &other )
const;
738 void makeBeginIterator( IteratorState &it )
const;
739 void makeEndIterator( IteratorState &it )
const;
740 static bool equals(
const IteratorState &x,
const IteratorState &other );
741 static void increment( IteratorState &iterator );
742 static void incrementBucket( IteratorState &iterator );
743 static void decrement( IteratorState &iterator );
744 static const char *key(
const IteratorState &iterator );
745 static const char *key(
const IteratorState &iterator,
bool &isStatic );
746 static Value &value(
const IteratorState &iterator );
747 static int distance(
const IteratorState &x,
const IteratorState &y );
750 ValueInternalLink *buckets_;
751 ValueInternalLink *tailLink_;
752 BucketIndex bucketsSize_;
753 BucketIndex itemCount_;
767 class JSON_API ValueInternalArray
772 enum { itemsPerPage = 8 };
773 typedef Value::ArrayIndex ArrayIndex;
774 typedef unsigned int PageIndex;
776 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
781 , currentPageIndex_(0)
782 , currentItemIndex_(0)
785 ValueInternalArray *array_;
786 Value **currentPageIndex_;
787 unsigned int currentItemIndex_;
789 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
791 ValueInternalArray();
792 ValueInternalArray(
const ValueInternalArray &other );
793 ValueInternalArray &operator =(
const ValueInternalArray &other );
794 ~ValueInternalArray();
795 void swap( ValueInternalArray &other );
798 void resize( ArrayIndex newSize );
800 Value &resolveReference( ArrayIndex index );
802 Value *find( ArrayIndex index )
const;
804 ArrayIndex size()
const;
806 int compare(
const ValueInternalArray &other )
const;
809 static bool equals(
const IteratorState &x,
const IteratorState &other );
810 static void increment( IteratorState &iterator );
811 static void decrement( IteratorState &iterator );
812 static Value &dereference(
const IteratorState &iterator );
813 static Value &unsafeDereference(
const IteratorState &iterator );
814 static int distance(
const IteratorState &x,
const IteratorState &y );
815 static ArrayIndex indexOf(
const IteratorState &iterator );
816 void makeBeginIterator( IteratorState &it )
const;
817 void makeEndIterator( IteratorState &it )
const;
818 void makeIterator( IteratorState &it, ArrayIndex index )
const;
820 void makeIndexValid( ArrayIndex index );
824 PageIndex pageCount_;
886 class JSON_API ValueArrayAllocator
889 virtual ~ValueArrayAllocator();
890 virtual ValueInternalArray *newArray() = 0;
891 virtual ValueInternalArray *newArrayCopy(
const ValueInternalArray &other ) = 0;
892 virtual void destructArray( ValueInternalArray *array ) = 0;
904 virtual void reallocateArrayPageIndex(
Value **&indexes,
905 ValueInternalArray::PageIndex &indexCount,
906 ValueInternalArray::PageIndex minNewIndexCount ) = 0;
907 virtual void releaseArrayPageIndex(
Value **indexes,
908 ValueInternalArray::PageIndex indexCount ) = 0;
909 virtual Value *allocateArrayPage() = 0;
910 virtual void releaseArrayPage(
Value *value ) = 0;
912 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
921 typedef unsigned int size_t;
922 typedef int difference_type;
926 #ifndef JSON_VALUE_USE_INTERNAL_MAP
933 bool operator ==(
const SelfType &other )
const
935 return isEqual( other );
938 bool operator !=(
const SelfType &other )
const
940 return !isEqual( other );
943 difference_type operator -(
const SelfType &other )
const
945 return computeDistance( other );
955 const char *memberName()
const;
958 Value &deref()
const;
964 difference_type computeDistance(
const SelfType &other )
const;
966 bool isEqual(
const SelfType &other )
const;
971 #ifndef JSON_VALUE_USE_INTERNAL_MAP
972 Value::ObjectValues::iterator current_;
978 ValueInternalArray::IteratorState array_;
979 ValueInternalMap::IteratorState map_;
992 typedef unsigned int size_t;
993 typedef int difference_type;
1002 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1050 typedef unsigned int size_t;
1051 typedef int difference_type;
1062 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1063 explicit ValueIterator(
const Value::ObjectValues::iterator ¤t );
1065 ValueIterator(
const ValueInternalArray::IteratorState &state );
1066 ValueIterator(
const ValueInternalMap::IteratorState &state );
1108 #endif // CPPTL_JSON_H_INCLUDED
Experimental and untested: represents an element of the "path" to access a node.
Lightweight wrapper to tag static string.
a comment placed on the line before a value
object value (collection of name/value pairs).
ValueType
Type of the value held by a Value object.
a comment just after a value on the same line
base class for Value iterators.
a comment on the line after a value (only make sense for root value)
Value & make(Value &root) const
Creates the "path" to access the specified node and returns a reference on the node.
Experimental do not use: Allocator to customize member name and string value memory management done b...
Iterator for object and array value.
Experimental and untested: represents a "path" to access a node.
const iterator for object and array value.
array value (ordered list)