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;
55 unsigned int length = unknown )
57 if ( length == unknown )
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";
681 # ifdef JSON_USE_CPPTL
683 Value::asConstString()
const
685 return CppTL::ConstString(
asString().c_str() );
699 return Int(value_.int_);
702 return Int(value_.uint_);
705 return Int( value_.real_ );
707 return value_.bool_ ? 1 : 0;
727 JSON_ASSERT_MESSAGE( value_.int_ >= 0,
"Negative integer can not be converted to unsigned integer" );
729 return UInt(value_.int_);
732 return UInt(value_.uint_);
735 return UInt( value_.real_ );
737 return value_.bool_ ? 1 : 0;
749 # if defined(JSON_HAS_INT64)
765 return Int( value_.real_ );
767 return value_.bool_ ? 1 : 0;
787 JSON_ASSERT_MESSAGE( value_.int_ >= 0,
"Negative integer can not be converted to UInt64" );
793 return UInt( value_.real_ );
795 return value_.bool_ ? 1 : 0;
805 # endif // if defined(JSON_HAS_INT64)
811 #if defined(JSON_NO_INT64)
822 #if defined(JSON_NO_INT64)
838 return static_cast<double>( value_.int_ );
840 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
841 return static_cast<double>( value_.uint_ );
842 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
843 return static_cast<double>(
Int(value_.uint_/2) ) * 2 +
Int(value_.uint_ & 1);
844 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
848 return value_.bool_ ? 1.0 : 0.0;
867 return static_cast<float>( value_.int_ );
869 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
870 return static_cast<float>( value_.uint_ );
871 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
872 return static_cast<float>(
Int(value_.uint_/2) ) * 2 +
Int(value_.uint_ & 1);
873 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
875 return static_cast<float>( value_.real_ );
877 return value_.bool_ ? 1.0f : 0.0f;
897 return value_.int_ != 0;
899 return value_.real_ != 0.0;
903 return value_.string_ && value_.string_[0] != 0;
906 return value_.map_->size() != 0;
922 return ( other ==
nullValue && value_.int_ == 0 )
924 || ( other ==
uintValue && value_.int_ >= 0 )
929 return ( other ==
nullValue && value_.uint_ == 0 )
936 return ( other ==
nullValue && value_.real_ == 0.0 )
943 return ( other ==
nullValue && value_.bool_ ==
false )
951 || ( other ==
nullValue && (!value_.string_ || value_.string_[0] == 0) );
954 || ( other ==
nullValue && value_.map_->size() == 0 );
957 || ( other ==
nullValue && value_.map_->size() == 0 );
978 #ifndef JSON_VALUE_USE_INTERNAL_MAP
980 if ( !value_.map_->empty() )
982 ObjectValues::const_iterator itLast = value_.map_->end();
984 return (*itLast).first.index()+1;
991 return Int( value_.array_->size() );
993 return Int( value_.map_->size() );
1006 return size() == 0u;
1026 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1029 value_.map_->clear();
1033 value_.array_->clear();
1036 value_.map_->clear();
1050 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1054 else if ( newSize > oldSize )
1055 (*this)[ newSize - 1 ];
1058 for (
ArrayIndex index = newSize; index < oldSize; ++index )
1060 value_.map_->erase( index );
1062 assert(
size() == newSize );
1065 value_.array_->resize( newSize );
1076 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1077 CZString key( index );
1078 ObjectValues::iterator it = value_.map_->lower_bound( key );
1079 if ( it != value_.map_->end() && (*it).first == key )
1080 return (*it).second;
1082 ObjectValues::value_type defaultValue( key,
null );
1083 it = value_.map_->insert( it, defaultValue );
1084 return (*it).second;
1086 return value_.array_->resolveReference( index );
1105 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1106 CZString key( index );
1107 ObjectValues::const_iterator it = value_.map_->find( key );
1108 if ( it == value_.map_->end() )
1110 return (*it).second;
1112 Value *value = value_.array_->find( index );
1113 return value ? *value :
null;
1129 return resolveReference( key,
false );
1134 Value::resolveReference(
const char *key,
1140 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1141 CZString actualKey( key, isStatic ? CZString::noDuplication
1142 : CZString::duplicateOnCopy );
1143 ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
1144 if ( it != value_.map_->end() && (*it).first == actualKey )
1145 return (*it).second;
1147 ObjectValues::value_type defaultValue( actualKey,
null );
1148 it = value_.map_->insert( it, defaultValue );
1149 Value &value = (*it).second;
1152 return value_.map_->resolveReference( key, isStatic );
1159 const Value &defaultValue )
const
1161 const Value *value = &((*this)[index]);
1162 return value == &
null ? defaultValue : *value;
1169 return index <
size();
1180 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1181 CZString actualKey( key, CZString::noDuplication );
1182 ObjectValues::const_iterator it = value_.map_->find( actualKey );
1183 if ( it == value_.map_->end() )
1185 return (*it).second;
1187 const Value *value = value_.map_->find( key );
1188 return value ? *value :
null;
1196 return (*
this)[ key.c_str() ];
1203 return (*
this)[ key.c_str() ];
1209 return resolveReference( key,
true );
1213 # ifdef JSON_USE_CPPTL
1217 return (*
this)[ key.c_str() ];
1224 return (*
this)[ key.c_str() ];
1232 return (*
this)[
size()] = value;
1238 const Value &defaultValue )
const
1240 const Value *value = &((*this)[key]);
1241 return value == &
null ? defaultValue : *value;
1247 const Value &defaultValue )
const
1249 return get( key.c_str(), defaultValue );
1258 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1259 CZString actualKey( key, CZString::noDuplication );
1260 ObjectValues::iterator it = value_.map_->find( actualKey );
1261 if ( it == value_.map_->end() )
1263 Value old(it->second);
1264 value_.map_->erase(it);
1267 Value *value = value_.map_->find( key );
1270 value_.map_.remove( key );
1284 # ifdef JSON_USE_CPPTL
1287 const Value &defaultValue )
const
1289 return get( key.c_str(), defaultValue );
1296 const Value *value = &((*this)[key]);
1297 return value != &
null;
1308 # ifdef JSON_USE_CPPTL
1323 members.reserve( value_.map_->size() );
1324 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1325 ObjectValues::const_iterator it = value_.map_->begin();
1326 ObjectValues::const_iterator itEnd = value_.map_->end();
1327 for ( ; it != itEnd; ++it )
1328 members.push_back( std::string( (*it).first.c_str() ) );
1330 ValueInternalMap::IteratorState it;
1331 ValueInternalMap::IteratorState itEnd;
1332 value_.map_->makeBeginIterator( it );
1333 value_.map_->makeEndIterator( itEnd );
1334 for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
1335 members.push_back( std::string( ValueInternalMap::key( it ) ) );
1444 comments_[placement].setComment( comment );
1459 return comments_ != 0 && comments_[placement].comment_ != 0;
1466 return comments_[placement].comment_;
1475 return writer.
write( *
this );
1484 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1486 if ( value_.array_ )
1488 ValueInternalArray::IteratorState it;
1489 value_.array_->makeBeginIterator( it );
1496 ValueInternalMap::IteratorState it;
1497 value_.map_->makeBeginIterator( it );
1519 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1521 if ( value_.array_ )
1523 ValueInternalArray::IteratorState it;
1524 value_.array_->makeEndIterator( it );
1531 ValueInternalMap::IteratorState it;
1532 value_.map_->makeEndIterator( it );
1555 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1557 if ( value_.array_ )
1559 ValueInternalArray::IteratorState it;
1560 value_.array_->makeBeginIterator( it );
1567 ValueInternalMap::IteratorState it;
1568 value_.map_->makeBeginIterator( it );
1576 return iterator( value_.map_->begin() );
1590 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1592 if ( value_.array_ )
1594 ValueInternalArray::IteratorState it;
1595 value_.array_->makeEndIterator( it );
1602 ValueInternalMap::IteratorState it;
1603 value_.map_->makeEndIterator( it );
1611 return iterator( value_.map_->end() );
1632 , kind_( kindIndex )
1645 : key_( key.c_str() )
1661 in.push_back( &a1 );
1662 in.push_back( &a2 );
1663 in.push_back( &a3 );
1664 in.push_back( &a4 );
1665 in.push_back( &a5 );
1666 makePath( path, in );
1671 Path::makePath(
const std::string &path,
1674 const char *current = path.c_str();
1675 const char *end = current + path.length();
1676 InArgs::const_iterator itInArg = in.begin();
1677 while ( current != end )
1679 if ( *current ==
'[' )
1682 if ( *current ==
'%' )
1683 addPathInArg( path, in, itInArg, PathArgument::kindIndex );
1687 for ( ; current != end && *current >=
'0' && *current <=
'9'; ++current )
1688 index = index * 10 +
ArrayIndex(*current -
'0');
1689 args_.push_back( index );
1691 if ( current == end || *current++ !=
']' )
1692 invalidPath( path,
int(current - path.c_str()) );
1694 else if ( *current ==
'%' )
1696 addPathInArg( path, in, itInArg, PathArgument::kindKey );
1699 else if ( *current ==
'.' )
1705 const char *beginName = current;
1706 while ( current != end && !strchr(
"[.", *current ) )
1708 args_.push_back( std::string( beginName, current ) );
1715 Path::addPathInArg(
const std::string &path,
1717 InArgs::const_iterator &itInArg,
1718 PathArgument::Kind kind )
1720 if ( itInArg == in.end() )
1724 else if ( (*itInArg)->kind_ != kind )
1730 args_.push_back( **itInArg );
1736 Path::invalidPath(
const std::string &path,
1746 const Value *node = &root;
1747 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1750 if ( arg.kind_ == PathArgument::kindIndex )
1756 node = &((*node)[arg.index_]);
1758 else if ( arg.kind_ == PathArgument::kindKey )
1764 node = &((*node)[arg.key_]);
1777 const Value &defaultValue )
const
1779 const Value *node = &root;
1780 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1783 if ( arg.kind_ == PathArgument::kindIndex )
1786 return defaultValue;
1787 node = &((*node)[arg.index_]);
1789 else if ( arg.kind_ == PathArgument::kindKey )
1792 return defaultValue;
1793 node = &((*node)[arg.key_]);
1795 return defaultValue;
1805 Value *node = &root;
1806 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1809 if ( arg.kind_ == PathArgument::kindIndex )
1815 node = &((*node)[arg.index_]);
1817 else if ( arg.kind_ == PathArgument::kindKey )
1823 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
std::string valueToString(Int value)
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.
JSON (JavaScript Object Notation).
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.