40 #include <plugin/json_server/json/value.h>
41 #include <plugin/json_server/json/writer.h>
50 # include <cpptl/conststring.h>
53 #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
54 # include "json_batchallocator.h"
55 #endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
57 #define JSON_ASSERT_UNREACHABLE assert( false )
58 #define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw
59 #define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) throw std::runtime_error( message );
63 const Value Value::null;
64 const Int Value::minInt = Int( ~(UInt(-1)/2) );
65 const Int Value::maxInt = Int( UInt(-1)/2 );
66 const UInt Value::maxUInt = UInt(-1);
96 ValueAllocator::~ValueAllocator()
103 virtual char *makeMemberName(
const char *memberName )
105 return duplicateStringValue( memberName );
108 virtual void releaseMemberName(
char *memberName )
110 releaseStringValue( memberName );
113 virtual char *duplicateStringValue(
const char *value,
114 unsigned int length = unknown )
120 if ( length == unknown )
121 length = (
unsigned int)strlen(value);
122 char *newString =
static_cast<char *
>( malloc( length + 1 ) );
123 memcpy( newString, value, length );
124 newString[length] = 0;
128 virtual void releaseStringValue(
char *value )
138 return valueAllocator;
146 } dummyValueAllocatorInitializer;
157 #ifdef JSON_VALUE_USE_INTERNAL_MAP
158 # include "json_internalarray.inl"
159 # include "json_internalmap.inl"
160 #endif // JSON_VALUE_USE_INTERNAL_MAP
162 # include "json_valueiterator.inl"
174 Value::CommentInfo::CommentInfo()
179 Value::CommentInfo::~CommentInfo()
182 valueAllocator()->releaseStringValue( comment_ );
190 valueAllocator()->releaseStringValue( comment_ );
192 JSON_ASSERT_MESSAGE( text[0]==
'\0' || text[0]==
'/',
"Comments must start with /");
194 comment_ = valueAllocator()->duplicateStringValue( text );
205 # ifndef JSON_VALUE_USE_INTERNAL_MAP
210 Value::CZString::CZString(
int index_arg )
212 , index_( index_arg )
216 Value::CZString::CZString(
const char *cstr, DuplicationPolicy allocate )
217 : cstr_( allocate == duplicate ? valueAllocator()->makeMemberName(cstr)
223 Value::CZString::CZString(
const CZString &other )
224 : cstr_( other.index_ != noDuplication && other.cstr_ != 0
225 ? valueAllocator()->makeMemberName( other.cstr_ )
227 , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
232 Value::CZString::~CZString()
234 if ( cstr_ && index_ == duplicate )
235 valueAllocator()->releaseMemberName( const_cast<char *>( cstr_ ) );
246 Value::CZString::operator =(
const CZString &other )
248 CZString temp( other );
254 Value::CZString::operator<(
const CZString &other )
const
257 return strcmp( cstr_, other.cstr_ ) < 0;
258 return index_ < other.index_;
262 Value::CZString::operator==(
const CZString &other )
const
265 return strcmp( cstr_, other.cstr_ ) == 0;
266 return index_ == other.index_;
271 Value::CZString::index()
const
278 Value::CZString::c_str()
const
284 Value::CZString::isStaticString()
const
286 return index_ == noDuplication;
289 #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
308 # ifdef JSON_VALUE_USE_INTERNAL_MAP
326 #ifndef JSON_VALUE_USE_INTERNAL_MAP
329 value_.map_ =
new ObjectValues();
333 value_.array_ = arrayAllocator()->newArray();
336 value_.map_ = mapAllocator()->newMap();
340 value_.bool_ =
false;
343 JSON_ASSERT_UNREACHABLE;
351 # ifdef JSON_VALUE_USE_INTERNAL_MAP
362 # ifdef JSON_VALUE_USE_INTERNAL_MAP
366 value_.uint_ = value;
372 # ifdef JSON_VALUE_USE_INTERNAL_MAP
376 value_.real_ = value;
383 # ifdef JSON_VALUE_USE_INTERNAL_MAP
387 value_.string_ = valueAllocator()->duplicateStringValue( value );
392 const char *endValue )
396 # ifdef JSON_VALUE_USE_INTERNAL_MAP
400 value_.string_ = valueAllocator()->duplicateStringValue( beginValue,
401 UInt(endValue - beginValue) );
409 # ifdef JSON_VALUE_USE_INTERNAL_MAP
413 value_.string_ = valueAllocator()->duplicateStringValue( value.c_str(),
414 (
unsigned int)value.length() );
420 , allocated_( false )
422 # ifdef JSON_VALUE_USE_INTERNAL_MAP
426 value_.string_ =
const_cast<char *
>( value.c_str() );
430 # ifdef JSON_USE_CPPTL
435 # ifdef JSON_VALUE_USE_INTERNAL_MAP
439 value_.string_ = valueAllocator()->duplicateStringValue( value, value.length() );
446 # ifdef JSON_VALUE_USE_INTERNAL_MAP
450 value_.bool_ = value;
455 : type_( other.type_ )
457 # ifdef JSON_VALUE_USE_INTERNAL_MAP
468 value_ = other.value_;
471 if ( other.value_.string_ )
473 value_.string_ = valueAllocator()->duplicateStringValue( other.value_.string_ );
479 #ifndef JSON_VALUE_USE_INTERNAL_MAP
482 value_.map_ =
new ObjectValues( *other.value_.map_ );
486 value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ );
489 value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ );
493 JSON_ASSERT_UNREACHABLE;
495 if ( other.comments_ )
497 comments_ =
new CommentInfo[numberOfCommentPlacement];
498 for (
int comment =0; comment < numberOfCommentPlacement; ++comment )
500 const CommentInfo &otherComment = other.comments_[comment];
501 if ( otherComment.comment_ )
502 comments_[comment].setComment( otherComment.comment_ );
520 valueAllocator()->releaseStringValue( value_.string_ );
522 #ifndef JSON_VALUE_USE_INTERNAL_MAP
529 arrayAllocator()->destructArray( value_.array_ );
532 mapAllocator()->destructMap( value_.map_ );
536 JSON_ASSERT_UNREACHABLE;
543 Value::operator=(
const Value &other )
557 int temp2 = allocated_;
558 allocated_ = other.allocated_;
559 other.allocated_ = temp2;
570 Value::compare(
const Value & )
600 Value::operator <(
const Value &other )
const
602 int typeDelta = type_ - other.type_;
604 return typeDelta < 0 ?
true :
false;
610 return value_.int_ < other.value_.int_;
612 return value_.uint_ < other.value_.uint_;
614 return value_.real_ < other.value_.real_;
616 return value_.bool_ < other.value_.bool_;
618 return ( value_.string_ == 0 && other.value_.string_ )
619 || ( other.value_.string_
621 && strcmp( value_.string_, other.value_.string_ ) < 0 );
622 #ifndef JSON_VALUE_USE_INTERNAL_MAP
626 int delta = int( value_.map_->size() - other.value_.map_->size() );
629 return (*value_.map_) < (*other.value_.map_);
633 return value_.array_->compare( *(other.value_.array_) ) < 0;
635 return value_.map_->compare( *(other.value_.map_) ) < 0;
638 JSON_ASSERT_UNREACHABLE;
644 Value::operator <=(
const Value &other )
const
646 return !(other > *
this);
650 Value::operator >=(
const Value &other )
const
652 return !(*
this < other);
656 Value::operator >(
const Value &other )
const
658 return other < *
this;
662 Value::operator ==(
const Value &other )
const
668 int temp = other.type_;
676 return value_.int_ == other.value_.int_;
678 return value_.uint_ == other.value_.uint_;
680 return value_.real_ == other.value_.real_;
682 return value_.bool_ == other.value_.bool_;
684 return ( value_.string_ == other.value_.string_ )
685 || ( other.value_.string_
687 && strcmp( value_.string_, other.value_.string_ ) == 0 );
688 #ifndef JSON_VALUE_USE_INTERNAL_MAP
691 return value_.map_->size() == other.value_.map_->size()
692 && (*value_.map_) == (*other.value_.map_);
695 return value_.array_->compare( *(other.value_.array_) ) == 0;
697 return value_.map_->compare( *(other.value_.map_) ) == 0;
700 JSON_ASSERT_UNREACHABLE;
706 Value::operator !=(
const Value &other )
const
708 return !( *
this == other );
712 Value::asCString()
const
715 return value_.string_;
720 Value::asString()
const
727 return value_.string_ ? value_.string_ :
"";
729 return value_.bool_ ?
"true" :
"false";
735 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to string" );
737 JSON_ASSERT_UNREACHABLE;
742 # ifdef JSON_USE_CPPTL
744 Value::asConstString()
const
746 return CppTL::ConstString( asString().c_str() );
760 JSON_ASSERT_MESSAGE( value_.uint_ < (
unsigned)maxInt,
"integer out of signed integer range" );
763 JSON_ASSERT_MESSAGE( value_.real_ >= minInt && value_.real_ <= maxInt,
"Real out of signed integer range" );
764 return Int( value_.real_ );
766 return value_.bool_ ? 1 : 0;
770 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to int" );
772 JSON_ASSERT_UNREACHABLE;
778 Value::asUInt()
const
785 JSON_ASSERT_MESSAGE( value_.int_ >= 0,
"Negative integer can not be converted to unsigned integer" );
790 JSON_ASSERT_MESSAGE( value_.real_ >= 0 && value_.real_ <= maxUInt,
"Real out of unsigned integer range" );
791 return UInt( value_.real_ );
793 return value_.bool_ ? 1 : 0;
797 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to uint" );
799 JSON_ASSERT_UNREACHABLE;
805 Value::asDouble()
const
818 return value_.bool_ ? 1.0 : 0.0;
822 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to double" );
824 JSON_ASSERT_UNREACHABLE;
830 Value::asBool()
const
838 return value_.int_ != 0;
840 return value_.real_ != 0.0;
844 return value_.string_ && value_.string_[0] != 0;
847 return value_.map_->size() != 0;
849 JSON_ASSERT_UNREACHABLE;
856 Value::isConvertibleTo(
ValueType other )
const
863 return ( other ==
nullValue && value_.int_ == 0 )
865 || ( other ==
uintValue && value_.int_ >= 0 )
870 return ( other ==
nullValue && value_.uint_ == 0 )
871 || ( other ==
intValue && value_.uint_ <= (unsigned)maxInt )
877 return ( other ==
nullValue && value_.real_ == 0.0 )
878 || ( other ==
intValue && value_.real_ >= minInt && value_.real_ <= maxInt )
879 || ( other ==
uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt )
884 return ( other ==
nullValue && value_.bool_ ==
false )
892 || ( other ==
nullValue && (!value_.string_ || value_.string_[0] == 0) );
895 || ( other ==
nullValue && value_.map_->size() == 0 );
898 || ( other ==
nullValue && value_.map_->size() == 0 );
900 JSON_ASSERT_UNREACHABLE;
919 #ifndef JSON_VALUE_USE_INTERNAL_MAP
921 if ( !value_.map_->empty() )
923 ObjectValues::const_iterator itLast = value_.map_->end();
925 return (*itLast).first.index()+1;
929 return Int( value_.map_->size() );
932 return Int( value_.array_->size() );
934 return Int( value_.map_->size() );
937 JSON_ASSERT_UNREACHABLE;
946 if ( isNull() || isArray() || isObject() )
967 #ifndef JSON_VALUE_USE_INTERNAL_MAP
970 value_.map_->clear();
974 value_.array_->clear();
977 value_.map_->clear();
991 #ifndef JSON_VALUE_USE_INTERNAL_MAP
992 UInt oldSize =
size();
995 else if ( newSize > oldSize )
996 (*this)[ newSize - 1 ];
999 for ( UInt index = newSize; index < oldSize; ++index )
1000 value_.map_->erase( index );
1001 assert(
size() == newSize );
1004 value_.array_->resize( newSize );
1015 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1017 ObjectValues::iterator it = value_.map_->lower_bound( key );
1018 if ( it != value_.map_->end() && (*it).first == key )
1019 return (*it).second;
1021 ObjectValues::value_type defaultValue( key, null );
1022 it = value_.map_->insert( it, defaultValue );
1023 return (*it).second;
1025 return value_.array_->resolveReference( index );
1036 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1038 ObjectValues::const_iterator it = value_.map_->find( key );
1039 if ( it == value_.map_->end() )
1041 return (*it).second;
1043 Value *value = value_.array_->find( index );
1044 return value ? *value : null;
1052 return resolveReference( key,
false );
1057 Value::resolveReference(
const char *key,
1063 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1064 CZString actualKey( key, isStatic ? CZString::noDuplication
1065 : CZString::duplicateOnCopy );
1066 ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
1067 if ( it != value_.map_->end() && (*it).first == actualKey )
1068 return (*it).second;
1070 ObjectValues::value_type defaultValue( actualKey, null );
1071 it = value_.map_->insert( it, defaultValue );
1072 Value &value = (*it).second;
1075 return value_.map_->resolveReference( key, isStatic );
1082 const Value &defaultValue )
const
1084 const Value *value = &((*this)[index]);
1085 return value == &null ? defaultValue : *value;
1092 return index <
size();
1103 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1104 CZString actualKey( key, CZString::noDuplication );
1105 ObjectValues::const_iterator it = value_.map_->find( actualKey );
1106 if ( it == value_.map_->end() )
1108 return (*it).second;
1110 const Value *value = value_.map_->find( key );
1111 return value ? *value : null;
1119 return (*
this)[ key.c_str() ];
1126 return (*
this)[ key.c_str() ];
1132 return resolveReference( key,
true );
1136 # ifdef JSON_USE_CPPTL
1140 return (*
this)[ key.c_str() ];
1147 return (*
this)[ key.c_str() ];
1155 return (*
this)[
size()] = value;
1161 const Value &defaultValue )
const
1163 const Value *value = &((*this)[key]);
1164 return value == &null ? defaultValue : *value;
1170 const Value &defaultValue )
const
1172 return get( key.c_str(), defaultValue );
1181 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1182 CZString actualKey( key, CZString::noDuplication );
1183 ObjectValues::iterator it = value_.map_->find( actualKey );
1184 if ( it == value_.map_->end() )
1186 Value old(it->second);
1187 value_.map_->erase(it);
1190 Value *value = value_.map_->find( key );
1193 value_.map_.remove( key );
1207 # ifdef JSON_USE_CPPTL
1210 const Value &defaultValue )
const
1212 return get( key.c_str(), defaultValue );
1219 const Value *value = &((*this)[key]);
1220 return value != &null;
1231 # ifdef JSON_USE_CPPTL
1244 return Value::Members();
1246 members.reserve( value_.map_->size() );
1247 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1248 ObjectValues::const_iterator it = value_.map_->begin();
1249 ObjectValues::const_iterator itEnd = value_.map_->end();
1250 for ( ; it != itEnd; ++it )
1251 members.push_back( std::string( (*it).first.c_str() ) );
1253 ValueInternalMap::IteratorState it;
1254 ValueInternalMap::IteratorState itEnd;
1255 value_.map_->makeBeginIterator( it );
1256 value_.map_->makeEndIterator( itEnd );
1257 for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
1258 members.push_back( std::string( ValueInternalMap::key( it ) ) );
1290 Value::isNull()
const
1297 Value::isBool()
const
1304 Value::isInt()
const
1311 Value::isUInt()
const
1318 Value::isIntegral()
const
1327 Value::isDouble()
const
1334 Value::isNumeric()
const
1336 return isIntegral() || isDouble();
1341 Value::isString()
const
1348 Value::isArray()
const
1355 Value::isObject()
const
1366 comments_ =
new CommentInfo[numberOfCommentPlacement];
1367 comments_[placement].setComment( comment );
1382 return comments_ != 0 && comments_[placement].comment_ != 0;
1388 if ( hasComment(placement) )
1389 return comments_[placement].comment_;
1395 Value::toStyledString()
const
1398 return writer.
write( *
this );
1402 Value::const_iterator
1403 Value::begin()
const
1407 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1409 if ( value_.array_ )
1411 ValueInternalArray::IteratorState it;
1412 value_.array_->makeBeginIterator( it );
1413 return const_iterator( it );
1419 ValueInternalMap::IteratorState it;
1420 value_.map_->makeBeginIterator( it );
1421 return const_iterator( it );
1428 return const_iterator( value_.map_->begin() );
1434 return const_iterator();
1437 Value::const_iterator
1442 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1444 if ( value_.array_ )
1446 ValueInternalArray::IteratorState it;
1447 value_.array_->makeEndIterator( it );
1448 return const_iterator( it );
1454 ValueInternalMap::IteratorState it;
1455 value_.map_->makeEndIterator( it );
1456 return const_iterator( it );
1463 return const_iterator( value_.map_->end() );
1469 return const_iterator();
1478 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1480 if ( value_.array_ )
1482 ValueInternalArray::IteratorState it;
1483 value_.array_->makeBeginIterator( it );
1484 return iterator( it );
1490 ValueInternalMap::IteratorState it;
1491 value_.map_->makeBeginIterator( it );
1492 return iterator( it );
1499 return iterator( value_.map_->begin() );
1513 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1515 if ( value_.array_ )
1517 ValueInternalArray::IteratorState it;
1518 value_.array_->makeEndIterator( it );
1519 return iterator( it );
1525 ValueInternalMap::IteratorState it;
1526 value_.map_->makeEndIterator( it );
1527 return iterator( it );
1534 return iterator( value_.map_->end() );
1547 PathArgument::PathArgument()
1553 PathArgument::PathArgument( Value::UInt index )
1555 , kind_( kindIndex )
1560 PathArgument::PathArgument(
const char *key )
1567 PathArgument::PathArgument(
const std::string &key )
1568 : key_( key.c_str() )
1576 Path::Path(
const std::string &path,
1577 const PathArgument &a1,
1578 const PathArgument &a2,
1579 const PathArgument &a3,
1580 const PathArgument &a4,
1581 const PathArgument &a5 )
1584 in.push_back( &a1 );
1585 in.push_back( &a2 );
1586 in.push_back( &a3 );
1587 in.push_back( &a4 );
1588 in.push_back( &a5 );
1589 makePath( path, in );
1594 Path::makePath(
const std::string &path,
1597 const char *current = path.c_str();
1598 const char *end = current + path.length();
1599 InArgs::const_iterator itInArg = in.begin();
1600 while ( current != end )
1602 if ( *current ==
'[' )
1605 if ( *current ==
'%' )
1606 addPathInArg( path, in, itInArg, PathArgument::kindIndex );
1609 Value::UInt index = 0;
1610 for ( ; current != end && *current >=
'0' && *current <=
'9'; ++current )
1611 index = index * 10 + Value::UInt(*current -
'0');
1612 args_.push_back( index );
1614 if ( current == end || *current++ !=
']' )
1615 invalidPath( path,
int(current - path.c_str()) );
1617 else if ( *current ==
'%' )
1619 addPathInArg( path, in, itInArg, PathArgument::kindKey );
1622 else if ( *current ==
'.' )
1628 const char *beginName = current;
1629 while ( current != end && !strchr(
"[.", *current ) )
1631 args_.push_back( std::string( beginName, current ) );
1638 Path::addPathInArg(
const std::string &,
1640 InArgs::const_iterator &itInArg,
1641 PathArgument::Kind kind )
1643 if ( itInArg == in.end() )
1647 else if ( (*itInArg)->kind_ != kind )
1653 args_.push_back( **itInArg );
1659 Path::invalidPath(
const std::string &,
1667 Path::resolve(
const Value &root )
const
1669 const Value *node = &root;
1670 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1672 const PathArgument &arg = *it;
1673 if ( arg.kind_ == PathArgument::kindIndex )
1675 if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
1679 node = &((*node)[arg.index_]);
1681 else if ( arg.kind_ == PathArgument::kindKey )
1683 if ( !node->isObject() )
1687 node = &((*node)[arg.key_]);
1688 if ( node == &Value::null )
1699 Path::resolve(
const Value &root,
1700 const Value &defaultValue )
const
1702 const Value *node = &root;
1703 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1705 const PathArgument &arg = *it;
1706 if ( arg.kind_ == PathArgument::kindIndex )
1708 if ( !node->isArray() || node->isValidIndex( arg.index_ ) )
1709 return defaultValue;
1710 node = &((*node)[arg.index_]);
1712 else if ( arg.kind_ == PathArgument::kindKey )
1714 if ( !node->isObject() )
1715 return defaultValue;
1716 node = &((*node)[arg.key_]);
1717 if ( node == &Value::null )
1718 return defaultValue;
1728 Value *node = &root;
1729 for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1732 if ( arg.kind_ == PathArgument::kindIndex )
1734 if ( !node->isArray() )
1738 node = &((*node)[arg.index_]);
1740 else if ( arg.kind_ == PathArgument::kindKey )
1742 if ( !node->isObject() )
1746 node = &((*node)[arg.key_]);