6 #if !defined(JSON_IS_AMALGAMATION)
9 # ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
11 # endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
12 #endif // if !defined(JSON_IS_AMALGAMATION)
19 # include <cpptl/conststring.h>
23 #define JSON_ASSERT_UNREACHABLE assert( false )
24 #define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw
25 #define JSON_FAIL_MESSAGE( message ) throw std::runtime_error( message );
26 #define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) JSON_FAIL_MESSAGE( message )
43 static const unsigned int unknown = (unsigned)-1;
58 length = (
unsigned int)strlen(value);
59 char *newString =
static_cast<char *
>( malloc( length + 1 ) );
61 memcpy( newString, value, length );
62 newString[length] = 0;
86 #if !defined(JSON_IS_AMALGAMATION)
87 # ifdef JSON_VALUE_USE_INTERNAL_MAP
90 # endif // JSON_VALUE_USE_INTERNAL_MAP
93 #endif // if !defined(JSON_IS_AMALGAMATION)
106 Value::CommentInfo::CommentInfo()
111 Value::CommentInfo::~CommentInfo()
119 Value::CommentInfo::setComment(
const char *text )
137 # ifndef JSON_VALUE_USE_INTERNAL_MAP
148 Value::CZString::CZString(
const char *cstr, DuplicationPolicy allocate )
155 Value::CZString::CZString(
const CZString &other )
156 : cstr_( other.index_ != noDuplication && other.cstr_ != 0
159 , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
164 Value::CZString::~CZString()
166 if ( cstr_ && index_ == duplicate )
171 Value::CZString::swap( CZString &other )
173 std::swap( cstr_, other.cstr_ );
174 std::swap( index_, other.index_ );
178 Value::CZString::operator =(
const CZString &other )
180 CZString temp( other );
186 Value::CZString::operator<(
const CZString &other )
const
189 return strcmp( cstr_, other.cstr_ ) < 0;
190 return index_ < other.index_;
194 Value::CZString::operator==(
const CZString &other )
const
197 return strcmp( cstr_, other.cstr_ ) == 0;
198 return index_ == other.index_;
203 Value::CZString::index()
const
210 Value::CZString::c_str()
const
216 Value::CZString::isStaticString()
const
218 return index_ == noDuplication;
221 #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
240 # ifdef JSON_VALUE_USE_INTERNAL_MAP
258 #ifndef JSON_VALUE_USE_INTERNAL_MAP
261 value_.map_ =
new ObjectValues();
272 value_.bool_ =
false;
280 #if defined(JSON_HAS_INT64)
284 # ifdef JSON_VALUE_USE_INTERNAL_MAP
288 value_.uint_ = value;
294 # ifdef JSON_VALUE_USE_INTERNAL_MAP
301 #endif // if defined(JSON_HAS_INT64)
307 # ifdef JSON_VALUE_USE_INTERNAL_MAP
318 # ifdef JSON_VALUE_USE_INTERNAL_MAP
322 value_.uint_ = value;
328 # ifdef JSON_VALUE_USE_INTERNAL_MAP
332 value_.real_ = value;
339 # ifdef JSON_VALUE_USE_INTERNAL_MAP
348 const char *endValue )
352 # ifdef JSON_VALUE_USE_INTERNAL_MAP
357 (
unsigned int)(endValue - beginValue) );
365 # ifdef JSON_VALUE_USE_INTERNAL_MAP
370 (
unsigned int)value.length() );
376 , allocated_( false )
378 # ifdef JSON_VALUE_USE_INTERNAL_MAP
382 value_.string_ =
const_cast<char *
>( value.
c_str() );
386 # ifdef JSON_USE_CPPTL
391 # ifdef JSON_VALUE_USE_INTERNAL_MAP
402 # ifdef JSON_VALUE_USE_INTERNAL_MAP
406 value_.bool_ = value;
411 : type_( other.type_ )
413 # ifdef JSON_VALUE_USE_INTERNAL_MAP
424 value_ = other.value_;
427 if ( other.value_.string_ )
435 #ifndef JSON_VALUE_USE_INTERNAL_MAP
438 value_.map_ =
new ObjectValues( *other.value_.map_ );
451 if ( other.comments_ )
456 const CommentInfo &otherComment = other.comments_[comment];
457 if ( otherComment.comment_ )
458 comments_[comment].setComment( otherComment.comment_ );
478 #ifndef JSON_VALUE_USE_INTERNAL_MAP
513 std::swap( value_, other.value_ );
514 int temp2 = allocated_;
515 allocated_ = other.allocated_;
516 other.allocated_ = temp2;
540 int typeDelta = type_ - other.type_;
542 return typeDelta < 0 ?
true :
false;
548 return value_.int_ < other.value_.int_;
550 return value_.uint_ < other.value_.uint_;
552 return value_.real_ < other.value_.real_;
554 return value_.bool_ < other.value_.bool_;
556 return ( value_.string_ == 0 && other.value_.string_ )
557 || ( other.value_.string_
559 && strcmp( value_.string_, other.value_.string_ ) < 0 );
560 #ifndef JSON_VALUE_USE_INTERNAL_MAP
564 int delta = int( value_.map_->size() - other.value_.map_->size() );
567 return (*value_.map_) < (*other.value_.map_);
571 return value_.array_->compare( *(other.value_.array_) ) < 0;
573 return value_.map_->compare( *(other.value_.map_) ) < 0;
584 return !(other < *
this);
590 return !(*
this < other);
596 return other < *
this;
606 int temp = other.type_;
614 return value_.int_ == other.value_.int_;
616 return value_.uint_ == other.value_.uint_;
618 return value_.real_ == other.value_.real_;
620 return value_.bool_ == other.value_.bool_;
622 return ( value_.string_ == other.value_.string_ )
623 || ( other.value_.string_
625 && strcmp( value_.string_, other.value_.string_ ) == 0 );
626 #ifndef JSON_VALUE_USE_INTERNAL_MAP
629 return value_.map_->size() == other.value_.map_->size()
630 && (*value_.map_) == (*other.value_.map_);
633 return value_.array_->compare( *(other.value_.array_) ) == 0;
635 return value_.map_->compare( *(other.value_.map_) ) == 0;
646 return !( *
this == other );
653 return value_.string_;
665 return value_.string_ ? value_.string_ :
"";
667 return value_.bool_ ?
"true" :
"false";
680 # ifdef JSON_USE_CPPTL
682 Value::asConstString()
const
684 return CppTL::ConstString(
asString().c_str() );
698 return Int(value_.int_);
701 return Int(value_.uint_);
704 return Int( value_.real_ );
706 return value_.bool_ ? 1 : 0;
726 JSON_ASSERT_MESSAGE( value_.int_ >= 0,
"Negative integer can not be converted to unsigned integer" );
728 return UInt(value_.int_);
731 return UInt(value_.uint_);
734 return UInt( value_.real_ );
736 return value_.bool_ ? 1 : 0;
748 # if defined(JSON_HAS_INT64)
764 return Int( value_.real_ );
766 return value_.bool_ ? 1 : 0;
786 JSON_ASSERT_MESSAGE( value_.int_ >= 0,
"Negative integer can not be converted to UInt64" );
792 return UInt( value_.real_ );
794 return value_.bool_ ? 1 : 0;
804 # endif // if defined(JSON_HAS_INT64)
810 #if defined(JSON_NO_INT64)
821 #if defined(JSON_NO_INT64)
837 return static_cast<double>( value_.int_ );
839 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
840 return static_cast<double>( value_.uint_ );
841 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
842 return static_cast<double>(
Int(value_.uint_/2) ) * 2 +
Int(value_.uint_ & 1);
843 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
847 return value_.bool_ ? 1.0 : 0.0;
866 return static_cast<float>( value_.int_ );
868 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
869 return static_cast<float>( value_.uint_ );
870 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
871 return static_cast<float>(
Int(value_.uint_/2) ) * 2 +
Int(value_.uint_ & 1);
872 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
874 return static_cast<float>( value_.real_ );
876 return value_.bool_ ? 1.0f : 0.0f;
896 return value_.int_ != 0;
898 return value_.real_ != 0.0;
902 return value_.string_ && value_.string_[0] != 0;
905 return value_.map_->size() != 0;
921 return ( other ==
nullValue && value_.int_ == 0 )
923 || ( other ==
uintValue && value_.int_ >= 0 )
928 return ( other ==
nullValue && value_.uint_ == 0 )
935 return ( other ==
nullValue && value_.real_ == 0.0 )
942 return ( other ==
nullValue && value_.bool_ ==
false )
950 || ( other ==
nullValue && (!value_.string_ || value_.string_[0] == 0) );
953 || ( other ==
nullValue && value_.map_->size() == 0 );
956 || ( other ==
nullValue && value_.map_->size() == 0 );
977 #ifndef JSON_VALUE_USE_INTERNAL_MAP
979 if ( !value_.map_->empty() )
981 ObjectValues::const_iterator itLast = value_.map_->end();
983 return (*itLast).first.index()+1;
990 return Int( value_.array_->size() );
992 return Int( value_.map_->size() );
1005 return size() == 0u;
1025 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1028 value_.map_->clear();
1032 value_.array_->clear();
1035 value_.map_->clear();
1049 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1053 else if ( newSize > oldSize )
1054 (*this)[ newSize - 1 ];
1057 for (
ArrayIndex index = newSize; index < oldSize; ++index )
1059 value_.map_->erase( index );
1061 assert(
size() == newSize );
1064 value_.array_->resize( newSize );
1075 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1076 CZString key( index );
1077 ObjectValues::iterator it = value_.map_->lower_bound( key );
1078 if ( it != value_.map_->end() && (*it).first == key )
1079 return (*it).second;
1081 ObjectValues::value_type defaultValue( key,
null );
1082 it = value_.map_->insert( it, defaultValue );
1083 return (*it).second;
1085 return value_.array_->resolveReference( index );
1104 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1105 CZString key( index );
1106 ObjectValues::const_iterator it = value_.map_->find( key );
1107 if ( it == value_.map_->end() )
1109 return (*it).second;
1111 Value *value = value_.array_->find( index );
1112 return value ? *value :
null;
1128 return resolveReference( key,
false );
1133 Value::resolveReference(
const char *key,
1139 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1140 CZString actualKey( key, isStatic ? CZString::noDuplication
1141 : CZString::duplicateOnCopy );
1142 ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
1143 if ( it != value_.map_->end() && (*it).first == actualKey )
1144 return (*it).second;
1146 ObjectValues::value_type defaultValue( actualKey,
null );
1147 it = value_.map_->insert( it, defaultValue );
1148 Value &value = (*it).second;
1151 return value_.map_->resolveReference( key, isStatic );
1158 const Value &defaultValue )
const
1160 const Value *value = &((*this)[index]);
1161 return value == &
null ? defaultValue : *value;
1168 return index <
size();
1179 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1180 CZString actualKey( key, CZString::noDuplication );
1181 ObjectValues::const_iterator it = value_.map_->find( actualKey );
1182 if ( it == value_.map_->end() )
1184 return (*it).second;
1186 const Value *value = value_.map_->find( key );
1187 return value ? *value :
null;
1195 return (*
this)[ key.c_str() ];
1202 return (*
this)[ key.c_str() ];
1208 return resolveReference( key,
true );
1212 # ifdef JSON_USE_CPPTL
1216 return (*
this)[ key.c_str() ];
1223 return (*
this)[ key.c_str() ];
1231 return (*
this)[
size()] = value;
1237 const Value &defaultValue )
const
1239 const Value *value = &((*this)[key]);
1240 return value == &
null ? defaultValue : *value;
1246 const Value &defaultValue )
const
1248 return get( key.c_str(), defaultValue );
1257 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1258 CZString actualKey( key, CZString::noDuplication );
1259 ObjectValues::iterator it = value_.map_->find( actualKey );
1260 if ( it == value_.map_->end() )
1262 Value old(it->second);
1263 value_.map_->erase(it);
1266 Value *value = value_.map_->find( key );
1269 value_.map_.remove( key );
1283 # ifdef JSON_USE_CPPTL
1286 const Value &defaultValue )
const
1288 return get( key.c_str(), defaultValue );
1295 const Value *value = &((*this)[key]);
1296 return value != &
null;
1307 # ifdef JSON_USE_CPPTL
1322 members.reserve( value_.map_->size() );
1323 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1324 ObjectValues::const_iterator it = value_.map_->begin();
1325 ObjectValues::const_iterator itEnd = value_.map_->end();
1326 for ( ; it != itEnd; ++it )
1327 members.push_back( std::string( (*it).first.c_str() ) );
1329 ValueInternalMap::IteratorState it;
1330 ValueInternalMap::IteratorState itEnd;
1331 value_.map_->makeBeginIterator( it );
1332 value_.map_->makeEndIterator( itEnd );
1333 for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
1334 members.push_back( std::string( ValueInternalMap::key( it ) ) );
1443 comments_[placement].setComment( comment );
1458 return comments_ != 0 && comments_[placement].comment_ != 0;
1465 return comments_[placement].comment_;
1474 return writer.
write( *
this );
1483 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1485 if ( value_.array_ )
1487 ValueInternalArray::IteratorState it;
1488 value_.array_->makeBeginIterator( it );
1495 ValueInternalMap::IteratorState it;
1496 value_.map_->makeBeginIterator( it );
1518 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1520 if ( value_.array_ )
1522 ValueInternalArray::IteratorState it;
1523 value_.array_->makeEndIterator( it );
1530 ValueInternalMap::IteratorState it;
1531 value_.map_->makeEndIterator( it );
1554 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1556 if ( value_.array_ )
1558 ValueInternalArray::IteratorState it;
1559 value_.array_->makeBeginIterator( it );
1566 ValueInternalMap::IteratorState it;
1567 value_.map_->makeBeginIterator( it );
1575 return iterator( value_.map_->begin() );
1589 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1591 if ( value_.array_ )
1593 ValueInternalArray::IteratorState it;
1594 value_.array_->makeEndIterator( it );
1601 ValueInternalMap::IteratorState it;
1602 value_.map_->makeEndIterator( it );
1610 return iterator( value_.map_->end() );
1631 , kind_( kindIndex )
1644 : key_( key.c_str() )
1660 in.push_back( &a1 );
1661 in.push_back( &a2 );
1662 in.push_back( &a3 );
1663 in.push_back( &a4 );
1664 in.push_back( &a5 );
1665 makePath( path, in );
1670 Path::makePath(
const std::string &path,
1673 const char *current = path.c_str();
1674 const char *end = current + path.length();
1675 InArgs::const_iterator itInArg = in.begin();
1676 while ( current != end )
1678 if ( *current ==
'[' )
1681 if ( *current ==
'%' )
1682 addPathInArg( path, in, itInArg, PathArgument::kindIndex );
1686 for ( ; current != end && *current >=
'0' && *current <=
'9'; ++current )
1687 index = index * 10 +
ArrayIndex(*current -
'0');
1688 args_.push_back( index );
1690 if ( current == end || *current++ !=
']' )
1691 invalidPath( path,
int(current - path.c_str()) );
1693 else if ( *current ==
'%' )
1695 addPathInArg( path, in, itInArg, PathArgument::kindKey );
1698 else if ( *current ==
'.' )
1704 const char *beginName = current;
1705 while ( current != end && !strchr(
"[.", *current ) )
1707 args_.push_back( std::string( beginName, current ) );
1714 Path::addPathInArg(
const std::string &path,
1716 InArgs::const_iterator &itInArg,
1717 PathArgument::Kind kind )
1719 if ( itInArg == in.end() )
1723 else if ( (*itInArg)->kind_ != kind )
1729 args_.push_back( **itInArg );
1735 Path::invalidPath(
const std::string &path,
1745 const Value *node = &root;
1746 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1749 if ( arg.kind_ == PathArgument::kindIndex )
1755 node = &((*node)[arg.index_]);
1757 else if ( arg.kind_ == PathArgument::kindKey )
1763 node = &((*node)[arg.key_]);
1776 const Value &defaultValue )
const
1778 const Value *node = &root;
1779 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1782 if ( arg.kind_ == PathArgument::kindIndex )
1785 return defaultValue;
1786 node = &((*node)[arg.index_]);
1788 else if ( arg.kind_ == PathArgument::kindKey )
1791 return defaultValue;
1792 node = &((*node)[arg.key_]);
1794 return defaultValue;
1804 Value *node = &root;
1805 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1808 if ( arg.kind_ == PathArgument::kindIndex )
1814 node = &((*node)[arg.index_]);
1816 else if ( arg.kind_ == PathArgument::kindKey )
1822 node = &((*node)[arg.key_]);
bool hasComment(CommentPlacement placement) const
Path(const std::string &path, const PathArgument &a1=PathArgument(), const PathArgument &a2=PathArgument(), const PathArgument &a3=PathArgument(), const PathArgument &a4=PathArgument(), const PathArgument &a5=PathArgument())
Writes a Value in JSON format in a human friendly way.
Value & make(Value &root) const
Creates the "path" to access the specified node and returns a reference on the node.
std::string asString() const
static const Int64 maxInt64
Maximum signed 64 bits int value that can be stored in a Json::Value.
std::vector< std::string > Members
array value (ordered list)
static char * duplicateStringValue(const char *value, unsigned int length=unknown)
Duplicates the specified string value.
LargestUInt asLargestUInt() const
bool operator<(const Value &other) const
Json::ArrayIndex ArrayIndex
int compare(const Value &other) const
object value (collection of name/value pairs).
static const Int maxInt
Maximum signed int value that can be stored in a Json::Value.
bool empty() const
Return true if empty array, empty object, or null; otherwise, false.
Lightweight wrapper to tag static string.
Value removeMember(const char *key)
Remove and return the named member.
static const UInt maxUInt
Maximum unsigned int value that can be stored in a Json::Value.
static ValueArrayAllocator *& arrayAllocator()
virtual ValueInternalMap * newMap()=0
const iterator for object and array value.
virtual void destructArray(ValueInternalArray *array)=0
Value(ValueType type=nullValue)
Create a default Value of the given type.
Experimental and untested: represents an element of the "path" to access a node.
void setComment(const char *comment, CommentPlacement placement)
Comments must be //... or /* ... */.
std::string getComment(CommentPlacement placement) const
Include delimiters and embedded newlines.
static const LargestInt minLargestInt
Minimum signed integer value that can be stored in a Json::Value.
virtual ValueInternalArray * newArray()=0
bool isMember(const char *key) const
Return true if the object has a member named key.
#define JSON_ASSERT_MESSAGE(condition, message)
virtual ValueInternalArray * newArrayCopy(const ValueInternalArray &other)=0
Value & operator[](ArrayIndex index)
Access an array element (zero based index ).
ValueConstIterator const_iterator
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
virtual std::string write(const Value &root)
Serialize a Value in JSON format.
Members getMemberNames() const
Return a list of the member names.
void swap(Value &other)
Swap values.
const char * c_str() const
const char * asCString() const
static const UInt64 maxUInt64
Maximum unsigned 64 bits int value that can be stored in a Json::Value.
static const unsigned int unknown
Unknown size marker.
bool operator>(const Value &other) const
static ValueMapAllocator *& mapAllocator()
bool operator>=(const Value &other) const
bool operator==(const Value &other) const
const Value & resolve(const Value &root) const
bool isValidIndex(ArrayIndex index) const
Return true if index < size().
Value & append(const Value &value)
Append value to array at the end.
Value & operator=(const Value &other)
ArrayIndex size() const
Number of values in array or object.
std::string toStyledString() const
#define JSON_ASSERT_UNREACHABLE
#define JSON_ASSERT(condition)
bool isConvertibleTo(ValueType other) const
static const Int64 minInt64
Minimum signed 64 bits int value that can be stored in a Json::Value.
static const Int minInt
Minimum signed int value that can be stored in a Json::Value.
bool operator!() const
Return isNull()
LargestInt asLargestInt() const
virtual ValueInternalMap * newMapCopy(const ValueInternalMap &other)=0
void resize(ArrayIndex size)
Resize the array to size elements.
void clear()
Remove all object members and array elements.
Iterator for object and array value.
bool operator<=(const Value &other) const
virtual void destructMap(ValueInternalMap *map)=0
static void releaseStringValue(char *value)
Free the string duplicated by duplicateStringValue().
ValueType
Type of the value held by a Value object.
Value get(ArrayIndex index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
#define JSON_FAIL_MESSAGE(message)
const_iterator begin() const
bool operator!=(const Value &other) const
static const LargestInt maxLargestInt
Maximum signed integer value that can be stored in a Json::Value.
const_iterator end() const
static const LargestUInt maxLargestUInt
Maximum unsigned integer value that can be stored in a Json::Value.