9 # include <cpptl/conststring.h>
12 #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
14 #endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
16 #define JSON_ASSERT_UNREACHABLE assert( false )
17 #define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw
18 #define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) throw std::runtime_error( message );
62 virtual ~DefaultValueAllocator()
66 virtual char *makeMemberName(
const char *memberName )
68 return duplicateStringValue( memberName );
71 virtual void releaseMemberName(
char *memberName )
73 releaseStringValue( memberName );
76 virtual char *duplicateStringValue(
const char *value,
77 unsigned int length = unknown )
83 if ( length == unknown )
84 length = (
unsigned int)strlen(value);
85 char *newString =
static_cast<char *
>( malloc( length + 1 ) );
86 memcpy( newString, value, length );
87 newString[length] = 0;
91 virtual void releaseStringValue(
char *value )
100 static DefaultValueAllocator defaultAllocator;
105 static struct DummyValueAllocatorInitializer {
106 DummyValueAllocatorInitializer()
121 #ifdef JSON_VALUE_USE_INTERNAL_MAP
124 #endif // JSON_VALUE_USE_INTERNAL_MAP
138 Value::CommentInfo::CommentInfo()
143 Value::CommentInfo::~CommentInfo()
151 Value::CommentInfo::setComment(
const char *text )
169 # ifndef JSON_VALUE_USE_INTERNAL_MAP
174 Value::CZString::CZString(
int index )
180 Value::CZString::CZString(
const char *cstr, DuplicationPolicy allocate )
187 Value::CZString::CZString(
const CZString &other )
188 : cstr_( other.index_ != noDuplication && other.cstr_ != 0
191 , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
196 Value::CZString::~CZString()
198 if ( cstr_ && index_ == duplicate )
203 Value::CZString::swap( CZString &other )
205 std::swap( cstr_, other.cstr_ );
206 std::swap( index_, other.index_ );
210 Value::CZString::operator =(
const CZString &other )
212 CZString temp( other );
218 Value::CZString::operator<(
const CZString &other )
const
221 return strcmp( cstr_, other.cstr_ ) < 0;
222 return index_ < other.index_;
226 Value::CZString::operator==(
const CZString &other )
const
229 return strcmp( cstr_, other.cstr_ ) == 0;
230 return index_ == other.index_;
235 Value::CZString::index()
const
242 Value::CZString::c_str()
const
248 Value::CZString::isStaticString()
const
250 return index_ == noDuplication;
253 #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
272 # ifdef JSON_VALUE_USE_INTERNAL_MAP
290 #ifndef JSON_VALUE_USE_INTERNAL_MAP
293 value_.map_ =
new ObjectValues();
304 value_.bool_ =
false;
315 # ifdef JSON_VALUE_USE_INTERNAL_MAP
326 # ifdef JSON_VALUE_USE_INTERNAL_MAP
330 value_.uint_ = value;
336 # ifdef JSON_VALUE_USE_INTERNAL_MAP
340 value_.real_ = value;
347 # ifdef JSON_VALUE_USE_INTERNAL_MAP
356 const char *endValue )
360 # ifdef JSON_VALUE_USE_INTERNAL_MAP
365 UInt(endValue - beginValue) );
373 # ifdef JSON_VALUE_USE_INTERNAL_MAP
378 (
unsigned int)value.length() );
384 , allocated_( false )
386 # ifdef JSON_VALUE_USE_INTERNAL_MAP
390 value_.string_ =
const_cast<char *
>( value.
c_str() );
394 # ifdef JSON_USE_CPPTL
399 # ifdef JSON_VALUE_USE_INTERNAL_MAP
410 # ifdef JSON_VALUE_USE_INTERNAL_MAP
414 value_.bool_ = value;
419 : type_( other.type_ )
421 # ifdef JSON_VALUE_USE_INTERNAL_MAP
432 value_ = other.value_;
435 if ( other.value_.string_ )
443 #ifndef JSON_VALUE_USE_INTERNAL_MAP
446 value_.map_ =
new ObjectValues( *other.value_.map_ );
459 if ( other.comments_ )
464 const CommentInfo &otherComment = other.comments_[comment];
465 if ( otherComment.comment_ )
466 comments_[comment].setComment( otherComment.comment_ );
486 #ifndef JSON_VALUE_USE_INTERNAL_MAP
521 std::swap( value_, other.value_ );
522 int temp2 = allocated_;
523 allocated_ = other.allocated_;
524 other.allocated_ = temp2;
567 int typeDelta = type_ - other.type_;
569 return typeDelta < 0 ?
true :
false;
575 return value_.int_ < other.value_.int_;
577 return value_.uint_ < other.value_.uint_;
579 return value_.real_ < other.value_.real_;
581 return value_.bool_ < other.value_.bool_;
583 return ( value_.string_ == 0 && other.value_.string_ )
584 || ( other.value_.string_
586 && strcmp( value_.string_, other.value_.string_ ) < 0 );
587 #ifndef JSON_VALUE_USE_INTERNAL_MAP
591 int delta = int( value_.map_->size() - other.value_.map_->size() );
594 return (*value_.map_) < (*other.value_.map_);
598 return value_.array_->compare( *(other.value_.array_) ) < 0;
600 return value_.map_->compare( *(other.value_.map_) ) < 0;
611 return !(other > *
this);
617 return !(*
this < other);
623 return other < *
this;
633 int temp = other.type_;
641 return value_.int_ == other.value_.int_;
643 return value_.uint_ == other.value_.uint_;
645 return value_.real_ == other.value_.real_;
647 return value_.bool_ == other.value_.bool_;
649 return ( value_.string_ == other.value_.string_ )
650 || ( other.value_.string_
652 && strcmp( value_.string_, other.value_.string_ ) == 0 );
653 #ifndef JSON_VALUE_USE_INTERNAL_MAP
656 return value_.map_->size() == other.value_.map_->size()
657 && (*value_.map_) == (*other.value_.map_);
660 return value_.array_->compare( *(other.value_.array_) ) == 0;
662 return value_.map_->compare( *(other.value_.map_) ) == 0;
673 return !( *
this == other );
680 return value_.string_;
692 return value_.string_ ? value_.string_ :
"";
694 return value_.bool_ ?
"true" :
"false";
707 # ifdef JSON_USE_CPPTL
709 Value::asConstString()
const
711 return CppTL::ConstString(
asString().c_str() );
729 return Int( value_.real_ );
731 return value_.bool_ ? 1 : 0;
750 JSON_ASSERT_MESSAGE( value_.int_ >= 0,
"Negative integer can not be converted to unsigned integer" );
756 return UInt( value_.real_ );
758 return value_.bool_ ? 1 : 0;
783 return value_.bool_ ? 1.0 : 0.0;
803 return value_.int_ != 0;
805 return value_.real_ != 0.0;
809 return value_.string_ && value_.string_[0] != 0;
812 return value_.map_->size() != 0;
828 return ( other ==
nullValue && value_.int_ == 0 )
830 || ( other ==
uintValue && value_.int_ >= 0 )
835 return ( other ==
nullValue && value_.uint_ == 0 )
842 return ( other ==
nullValue && value_.real_ == 0.0 )
849 return ( other ==
nullValue && value_.bool_ ==
false )
857 || ( other ==
nullValue && (!value_.string_ || value_.string_[0] == 0) );
860 || ( other ==
nullValue && value_.map_->size() == 0 );
863 || ( other ==
nullValue && value_.map_->size() == 0 );
884 #ifndef JSON_VALUE_USE_INTERNAL_MAP
886 if ( !value_.map_->empty() )
888 ObjectValues::const_iterator itLast = value_.map_->end();
890 return (*itLast).first.index()+1;
894 return Int( value_.map_->size() );
897 return Int( value_.array_->size() );
899 return Int( value_.map_->size() );
932 #ifndef JSON_VALUE_USE_INTERNAL_MAP
935 value_.map_->clear();
939 value_.array_->clear();
942 value_.map_->clear();
956 #ifndef JSON_VALUE_USE_INTERNAL_MAP
960 else if ( newSize > oldSize )
961 (*this)[ newSize - 1 ];
964 for (
UInt index = newSize; index < oldSize; ++index )
965 value_.map_->erase( index );
966 assert(
size() == newSize );
969 value_.array_->resize( newSize );
980 #ifndef JSON_VALUE_USE_INTERNAL_MAP
981 CZString key( index );
982 ObjectValues::iterator it = value_.map_->lower_bound( key );
983 if ( it != value_.map_->end() && (*it).first == key )
986 ObjectValues::value_type defaultValue( key,
null );
987 it = value_.map_->insert( it, defaultValue );
990 return value_.array_->resolveReference( index );
1001 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1002 CZString key( index );
1003 ObjectValues::const_iterator it = value_.map_->find( key );
1004 if ( it == value_.map_->end() )
1006 return (*it).second;
1008 Value *value = value_.array_->find( index );
1009 return value ? *value :
null;
1017 return resolveReference( key,
false );
1022 Value::resolveReference(
const char *key,
1028 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1029 CZString actualKey( key, isStatic ? CZString::noDuplication
1030 : CZString::duplicateOnCopy );
1031 ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
1032 if ( it != value_.map_->end() && (*it).first == actualKey )
1033 return (*it).second;
1035 ObjectValues::value_type defaultValue( actualKey,
null );
1036 it = value_.map_->insert( it, defaultValue );
1037 Value &value = (*it).second;
1040 return value_.map_->resolveReference( key, isStatic );
1047 const Value &defaultValue )
const
1049 const Value *value = &((*this)[index]);
1050 return value == &
null ? defaultValue : *value;
1057 return index <
size();
1068 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1069 CZString actualKey( key, CZString::noDuplication );
1070 ObjectValues::const_iterator it = value_.map_->find( actualKey );
1071 if ( it == value_.map_->end() )
1073 return (*it).second;
1075 const Value *value = value_.map_->find( key );
1076 return value ? *value :
null;
1084 return (*
this)[ key.c_str() ];
1091 return (*
this)[ key.c_str() ];
1097 return resolveReference( key,
true );
1101 # ifdef JSON_USE_CPPTL
1105 return (*
this)[ key.c_str() ];
1112 return (*
this)[ key.c_str() ];
1120 return (*
this)[
size()] = value;
1126 const Value &defaultValue )
const
1128 const Value *value = &((*this)[key]);
1129 return value == &
null ? defaultValue : *value;
1135 const Value &defaultValue )
const
1137 return get( key.c_str(), defaultValue );
1146 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1147 CZString actualKey( key, CZString::noDuplication );
1148 ObjectValues::iterator it = value_.map_->find( actualKey );
1149 if ( it == value_.map_->end() )
1151 Value old(it->second);
1152 value_.map_->erase(it);
1155 Value *value = value_.map_->find( key );
1158 value_.map_.remove( key );
1172 # ifdef JSON_USE_CPPTL
1175 const Value &defaultValue )
const
1177 return get( key.c_str(), defaultValue );
1184 const Value *value = &((*this)[key]);
1185 return value != &
null;
1196 # ifdef JSON_USE_CPPTL
1211 members.reserve( value_.map_->size() );
1212 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1213 ObjectValues::const_iterator it = value_.map_->begin();
1214 ObjectValues::const_iterator itEnd = value_.map_->end();
1215 for ( ; it != itEnd; ++it )
1216 members.push_back( std::string( (*it).first.c_str() ) );
1218 ValueInternalMap::IteratorState it;
1219 ValueInternalMap::IteratorState itEnd;
1220 value_.map_->makeBeginIterator( it );
1221 value_.map_->makeEndIterator( itEnd );
1222 for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
1223 members.push_back( std::string( ValueInternalMap::key( it ) ) );
1332 comments_[placement].setComment( comment );
1347 return comments_ != 0 && comments_[placement].comment_ != 0;
1354 return comments_[placement].comment_;
1363 return writer.
write( *
this );
1372 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1374 if ( value_.array_ )
1376 ValueInternalArray::IteratorState it;
1377 value_.array_->makeBeginIterator( it );
1384 ValueInternalMap::IteratorState it;
1385 value_.map_->makeBeginIterator( it );
1407 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1409 if ( value_.array_ )
1411 ValueInternalArray::IteratorState it;
1412 value_.array_->makeEndIterator( it );
1419 ValueInternalMap::IteratorState it;
1420 value_.map_->makeEndIterator( it );
1443 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1445 if ( value_.array_ )
1447 ValueInternalArray::IteratorState it;
1448 value_.array_->makeBeginIterator( it );
1455 ValueInternalMap::IteratorState it;
1456 value_.map_->makeBeginIterator( it );
1464 return iterator( value_.map_->begin() );
1478 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1480 if ( value_.array_ )
1482 ValueInternalArray::IteratorState it;
1483 value_.array_->makeEndIterator( it );
1490 ValueInternalMap::IteratorState it;
1491 value_.map_->makeEndIterator( it );
1499 return iterator( value_.map_->end() );
1520 , kind_( kindIndex )
1533 : key_( key.c_str() )
1549 in.push_back( &a1 );
1550 in.push_back( &a2 );
1551 in.push_back( &a3 );
1552 in.push_back( &a4 );
1553 in.push_back( &a5 );
1554 makePath( path, in );
1559 Path::makePath(
const std::string &path,
1562 const char *current = path.c_str();
1563 const char *end = current + path.length();
1564 InArgs::const_iterator itInArg = in.begin();
1565 while ( current != end )
1567 if ( *current ==
'[' )
1570 if ( *current ==
'%' )
1571 addPathInArg( path, in, itInArg, PathArgument::kindIndex );
1575 for ( ; current != end && *current >=
'0' && *current <=
'9'; ++current )
1577 args_.push_back( index );
1579 if ( current == end || *current++ !=
']' )
1580 invalidPath( path,
int(current - path.c_str()) );
1582 else if ( *current ==
'%' )
1584 addPathInArg( path, in, itInArg, PathArgument::kindKey );
1587 else if ( *current ==
'.' )
1593 const char *beginName = current;
1594 while ( current != end && !strchr(
"[.", *current ) )
1596 args_.push_back( std::string( beginName, current ) );
1603 Path::addPathInArg(
const std::string &path,
1605 InArgs::const_iterator &itInArg,
1606 PathArgument::Kind kind )
1608 if ( itInArg == in.end() )
1612 else if ( (*itInArg)->kind_ != kind )
1618 args_.push_back( **itInArg );
1624 Path::invalidPath(
const std::string &path,
1634 const Value *node = &root;
1635 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1638 if ( arg.kind_ == PathArgument::kindIndex )
1644 node = &((*node)[arg.index_]);
1646 else if ( arg.kind_ == PathArgument::kindKey )
1652 node = &((*node)[arg.key_]);
1665 const Value &defaultValue )
const
1667 const Value *node = &root;
1668 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1671 if ( arg.kind_ == PathArgument::kindIndex )
1674 return defaultValue;
1675 node = &((*node)[arg.index_]);
1677 else if ( arg.kind_ == PathArgument::kindKey )
1680 return defaultValue;
1681 node = &((*node)[arg.key_]);
1683 return defaultValue;
1693 Value *node = &root;
1694 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1697 if ( arg.kind_ == PathArgument::kindIndex )
1703 node = &((*node)[arg.index_]);
1705 else if ( arg.kind_ == PathArgument::kindKey )
1711 node = &((*node)[arg.key_]);