1 // Copyright 2011 Baptiste Lepilleur
2 // Distributed under MIT license, or public domain if desired and
3 // recognized in your jurisdiction.
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
6 #if !defined(JSON_IS_AMALGAMATION)
7 # include <json/assertions.h>
8 # include <json/value.h>
9 # include <json/writer.h>
10 # ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
11 # include "json_batchallocator.h"
12 # endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
13 #endif // if !defined(JSON_IS_AMALGAMATION)
21 # include <cpptl/conststring.h>
23 #include <cstddef> // size_t
25 #define JSON_ASSERT_UNREACHABLE assert( false )
29 // This is a walkaround to avoid the static initialization of Value::null.
30 // kNull must be word-aligned to avoid crashing on ARM. We use an alignment of
31 // 8 (instead of 4) as a bit of future-proofing.
32 #if defined(__ARMEL__)
33 #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
35 #define ALIGNAS(byte_alignment)
37 static const unsigned char ALIGNAS(8) kNull
[sizeof(Value
)] = {0};
38 const Value
& Value::null
= reinterpret_cast<const Value
&>(kNull
);
40 const Int
Value::minInt
= Int( ~(UInt(-1)/2) );
41 const Int
Value::maxInt
= Int( UInt(-1)/2 );
42 const UInt
Value::maxUInt
= UInt(-1);
43 # if defined(JSON_HAS_INT64)
44 const Int64
Value::minInt64
= Int64( ~(UInt64(-1)/2) );
45 const Int64
Value::maxInt64
= Int64( UInt64(-1)/2 );
46 const UInt64
Value::maxUInt64
= UInt64(-1);
47 // The constant is hard-coded because some compiler have trouble
48 // converting Value::maxUInt64 to a double correctly (AIX/xlC).
49 // Assumes that UInt64 is a 64 bits integer.
50 static const double maxUInt64AsDouble
= 18446744073709551615.0;
51 #endif // defined(JSON_HAS_INT64)
52 const LargestInt
Value::minLargestInt
= LargestInt( ~(LargestUInt(-1)/2) );
53 const LargestInt
Value::maxLargestInt
= LargestInt( LargestUInt(-1)/2 );
54 const LargestUInt
Value::maxLargestUInt
= LargestUInt(-1);
57 /// Unknown size marker
58 static const unsigned int unknown
= (unsigned)-1;
60 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
61 template <typename T
, typename U
>
62 static inline bool InRange(double d
, T min
, U max
) {
63 return d
>= min
&& d
<= max
;
65 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
66 static inline double integerToDouble( Json::UInt64 value
)
68 return static_cast<double>( Int64(value
/2) ) * 2.0 + Int64(value
& 1);
72 static inline double integerToDouble( T value
)
74 return static_cast<double>( value
);
77 template <typename T
, typename U
>
78 static inline bool InRange(double d
, T min
, U max
) {
79 return d
>= integerToDouble(min
) && d
<= integerToDouble(max
);
81 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
84 /** Duplicates the specified string value.
85 * @param value Pointer to the string to duplicate. Must be zero-terminated if
86 * length is "unknown".
87 * @param length Length of the value. if equals to unknown, then it will be
88 * computed using strlen(value).
89 * @return Pointer on the duplicate instance of string.
92 duplicateStringValue( const char *value
,
93 unsigned int length
= unknown
)
95 if ( length
== unknown
)
96 length
= (unsigned int)strlen(value
);
98 // Avoid an integer overflow in the call to malloc below by limiting length
100 if (length
>= (unsigned)Value::maxInt
)
101 length
= Value::maxInt
- 1;
103 char *newString
= static_cast<char *>( malloc( length
+ 1 ) );
104 JSON_ASSERT_MESSAGE( newString
!= 0, "Failed to allocate string value buffer" );
105 memcpy( newString
, value
, length
);
106 newString
[length
] = 0;
111 /** Free the string duplicated by duplicateStringValue().
114 releaseStringValue( char *value
)
123 // //////////////////////////////////////////////////////////////////
124 // //////////////////////////////////////////////////////////////////
125 // //////////////////////////////////////////////////////////////////
127 // //////////////////////////////////////////////////////////////////
128 // //////////////////////////////////////////////////////////////////
129 // //////////////////////////////////////////////////////////////////
130 #if !defined(JSON_IS_AMALGAMATION)
131 # ifdef JSON_VALUE_USE_INTERNAL_MAP
132 # include "json_internalarray.inl"
133 # include "json_internalmap.inl"
134 # endif // JSON_VALUE_USE_INTERNAL_MAP
136 # include "json_valueiterator.inl"
137 #endif // if !defined(JSON_IS_AMALGAMATION)
141 // //////////////////////////////////////////////////////////////////
142 // //////////////////////////////////////////////////////////////////
143 // //////////////////////////////////////////////////////////////////
144 // class Value::CommentInfo
145 // //////////////////////////////////////////////////////////////////
146 // //////////////////////////////////////////////////////////////////
147 // //////////////////////////////////////////////////////////////////
150 Value::CommentInfo::CommentInfo()
155 Value::CommentInfo::~CommentInfo()
158 releaseStringValue( comment_
);
163 Value::CommentInfo::setComment( const char *text
)
166 releaseStringValue( comment_
);
167 JSON_ASSERT( text
!= 0 );
168 JSON_ASSERT_MESSAGE( text
[0]=='\0' || text
[0]=='/', "Comments must start with /");
169 // It seems that /**/ style comments are acceptable as well.
170 comment_
= duplicateStringValue( text
);
174 // //////////////////////////////////////////////////////////////////
175 // //////////////////////////////////////////////////////////////////
176 // //////////////////////////////////////////////////////////////////
177 // class Value::CZString
178 // //////////////////////////////////////////////////////////////////
179 // //////////////////////////////////////////////////////////////////
180 // //////////////////////////////////////////////////////////////////
181 # ifndef JSON_VALUE_USE_INTERNAL_MAP
183 // Notes: index_ indicates if the string was allocated when
184 // a string is stored.
186 Value::CZString::CZString( ArrayIndex index
)
192 Value::CZString::CZString( const char *cstr
, DuplicationPolicy allocate
)
193 : cstr_( allocate
== duplicate
? duplicateStringValue(cstr
)
199 Value::CZString::CZString( const CZString
&other
)
200 : cstr_( other
.index_
!= noDuplication
&& other
.cstr_
!= 0
201 ? duplicateStringValue( other
.cstr_
)
203 , index_( other
.cstr_
? (other
.index_
== noDuplication
? noDuplication
: duplicate
)
208 Value::CZString::~CZString()
210 if ( cstr_
&& index_
== duplicate
)
211 releaseStringValue( const_cast<char *>( cstr_
) );
215 Value::CZString::swap( CZString
&other
)
217 std::swap( cstr_
, other
.cstr_
);
218 std::swap( index_
, other
.index_
);
222 Value::CZString::operator =( const CZString
&other
)
224 CZString
temp( other
);
230 Value::CZString::operator<( const CZString
&other
) const
233 return strcmp( cstr_
, other
.cstr_
) < 0;
234 return index_
< other
.index_
;
238 Value::CZString::operator==( const CZString
&other
) const
241 return strcmp( cstr_
, other
.cstr_
) == 0;
242 return index_
== other
.index_
;
247 Value::CZString::index() const
254 Value::CZString::c_str() const
260 Value::CZString::isStaticString() const
262 return index_
== noDuplication
;
265 #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
268 // //////////////////////////////////////////////////////////////////
269 // //////////////////////////////////////////////////////////////////
270 // //////////////////////////////////////////////////////////////////
271 // class Value::Value
272 // //////////////////////////////////////////////////////////////////
273 // //////////////////////////////////////////////////////////////////
274 // //////////////////////////////////////////////////////////////////
276 /*! \internal Default constructor initialization must be equivalent to:
277 * memset( this, 0, sizeof(Value) )
278 * This optimization is used in ValueInternalMap fast allocator.
280 Value::Value( ValueType type
)
282 , allocated_( false )
283 # ifdef JSON_VALUE_USE_INTERNAL_MAP
302 #ifndef JSON_VALUE_USE_INTERNAL_MAP
305 value_
.map_
= new ObjectValues();
309 value_
.array_
= arrayAllocator()->newArray();
312 value_
.map_
= mapAllocator()->newMap();
316 value_
.bool_
= false;
319 JSON_ASSERT_UNREACHABLE
;
324 Value::Value( UInt value
)
326 , allocated_( false )
327 # ifdef JSON_VALUE_USE_INTERNAL_MAP
332 value_
.uint_
= value
;
335 Value::Value( Int value
)
337 , allocated_( false )
338 # ifdef JSON_VALUE_USE_INTERNAL_MAP
347 # if defined(JSON_HAS_INT64)
348 Value::Value( Int64 value
)
350 , allocated_( false )
351 # ifdef JSON_VALUE_USE_INTERNAL_MAP
360 Value::Value( UInt64 value
)
362 , allocated_( false )
363 # ifdef JSON_VALUE_USE_INTERNAL_MAP
368 value_
.uint_
= value
;
370 #endif // defined(JSON_HAS_INT64)
372 Value::Value( double value
)
374 , allocated_( false )
375 # ifdef JSON_VALUE_USE_INTERNAL_MAP
380 value_
.real_
= value
;
383 Value::Value( const char *value
)
384 : type_( stringValue
)
386 # ifdef JSON_VALUE_USE_INTERNAL_MAP
391 value_
.string_
= duplicateStringValue( value
);
395 Value::Value( const char *beginValue
,
396 const char *endValue
)
397 : type_( stringValue
)
399 # ifdef JSON_VALUE_USE_INTERNAL_MAP
404 value_
.string_
= duplicateStringValue( beginValue
,
405 (unsigned int)(endValue
- beginValue
) );
409 Value::Value( const std::string
&value
)
410 : type_( stringValue
)
412 # ifdef JSON_VALUE_USE_INTERNAL_MAP
417 value_
.string_
= duplicateStringValue( value
.c_str(),
418 (unsigned int)value
.length() );
422 Value::Value( const StaticString
&value
)
423 : type_( stringValue
)
424 , allocated_( false )
425 # ifdef JSON_VALUE_USE_INTERNAL_MAP
430 value_
.string_
= const_cast<char *>( value
.c_str() );
434 # ifdef JSON_USE_CPPTL
435 Value::Value( const CppTL::ConstString
&value
)
436 : type_( stringValue
)
438 # ifdef JSON_VALUE_USE_INTERNAL_MAP
443 value_
.string_
= duplicateStringValue( value
, value
.length() );
447 Value::Value( bool value
)
448 : type_( booleanValue
)
449 , allocated_( false )
450 # ifdef JSON_VALUE_USE_INTERNAL_MAP
455 value_
.bool_
= value
;
459 Value::Value( const Value
&other
)
460 : type_( other
.type_
)
461 , allocated_( false )
462 # ifdef JSON_VALUE_USE_INTERNAL_MAP
474 value_
= other
.value_
;
477 if ( other
.value_
.string_
)
479 value_
.string_
= duplicateStringValue( other
.value_
.string_
);
485 #ifndef JSON_VALUE_USE_INTERNAL_MAP
488 value_
.map_
= new ObjectValues( *other
.value_
.map_
);
492 value_
.array_
= arrayAllocator()->newArrayCopy( *other
.value_
.array_
);
495 value_
.map_
= mapAllocator()->newMapCopy( *other
.value_
.map_
);
499 JSON_ASSERT_UNREACHABLE
;
501 if ( other
.comments_
)
503 comments_
= new CommentInfo
[numberOfCommentPlacement
];
504 for ( int comment
=0; comment
< numberOfCommentPlacement
; ++comment
)
506 const CommentInfo
&otherComment
= other
.comments_
[comment
];
507 if ( otherComment
.comment_
)
508 comments_
[comment
].setComment( otherComment
.comment_
);
526 releaseStringValue( value_
.string_
);
528 #ifndef JSON_VALUE_USE_INTERNAL_MAP
535 arrayAllocator()->destructArray( value_
.array_
);
538 mapAllocator()->destructMap( value_
.map_
);
542 JSON_ASSERT_UNREACHABLE
;
550 Value::operator=( const Value
&other
)
558 Value::swap( Value
&other
)
560 ValueType temp
= type_
;
563 std::swap( value_
, other
.value_
);
564 int temp2
= allocated_
;
565 allocated_
= other
.allocated_
;
566 other
.allocated_
= temp2
;
577 Value::compare( const Value
&other
) const
588 Value::operator <( const Value
&other
) const
590 int typeDelta
= type_
- other
.type_
;
592 return typeDelta
< 0 ? true : false;
598 return value_
.int_
< other
.value_
.int_
;
600 return value_
.uint_
< other
.value_
.uint_
;
602 return value_
.real_
< other
.value_
.real_
;
604 return value_
.bool_
< other
.value_
.bool_
;
606 return ( value_
.string_
== 0 && other
.value_
.string_
)
607 || ( other
.value_
.string_
609 && strcmp( value_
.string_
, other
.value_
.string_
) < 0 );
610 #ifndef JSON_VALUE_USE_INTERNAL_MAP
614 int delta
= int( value_
.map_
->size() - other
.value_
.map_
->size() );
617 return (*value_
.map_
) < (*other
.value_
.map_
);
621 return value_
.array_
->compare( *(other
.value_
.array_
) ) < 0;
623 return value_
.map_
->compare( *(other
.value_
.map_
) ) < 0;
626 JSON_ASSERT_UNREACHABLE
;
628 return false; // unreachable
632 Value::operator <=( const Value
&other
) const
634 return !(other
< *this);
638 Value::operator >=( const Value
&other
) const
640 return !(*this < other
);
644 Value::operator >( const Value
&other
) const
646 return other
< *this;
650 Value::operator ==( const Value
&other
) const
652 //if ( type_ != other.type_ )
654 // attempt to take address of bit-field structure member `Json::Value::type_'
655 // Beats me, but a temp solves the problem.
656 int temp
= other
.type_
;
664 return value_
.int_
== other
.value_
.int_
;
666 return value_
.uint_
== other
.value_
.uint_
;
668 return value_
.real_
== other
.value_
.real_
;
670 return value_
.bool_
== other
.value_
.bool_
;
672 return ( value_
.string_
== other
.value_
.string_
)
673 || ( other
.value_
.string_
675 && strcmp( value_
.string_
, other
.value_
.string_
) == 0 );
676 #ifndef JSON_VALUE_USE_INTERNAL_MAP
679 return value_
.map_
->size() == other
.value_
.map_
->size()
680 && (*value_
.map_
) == (*other
.value_
.map_
);
683 return value_
.array_
->compare( *(other
.value_
.array_
) ) == 0;
685 return value_
.map_
->compare( *(other
.value_
.map_
) ) == 0;
688 JSON_ASSERT_UNREACHABLE
;
690 return false; // unreachable
694 Value::operator !=( const Value
&other
) const
696 return !( *this == other
);
700 Value::asCString() const
702 JSON_ASSERT( type_
== stringValue
);
703 return value_
.string_
;
708 Value::asString() const
715 return value_
.string_
? value_
.string_
: "";
717 return value_
.bool_
? "true" : "false";
719 return valueToString( value_
.int_
);
721 return valueToString( value_
.uint_
);
723 return valueToString( value_
.real_
);
725 JSON_FAIL_MESSAGE( "Type is not convertible to string" );
729 # ifdef JSON_USE_CPPTL
731 Value::asConstString() const
733 return CppTL::ConstString( asString().c_str() );
744 JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range");
745 return Int(value_
.int_
);
747 JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range");
748 return Int(value_
.uint_
);
750 JSON_ASSERT_MESSAGE(InRange(value_
.real_
, minInt
, maxInt
), "double out of Int range");
751 return Int(value_
.real_
);
755 return value_
.bool_
? 1 : 0;
759 JSON_FAIL_MESSAGE("Value is not convertible to Int.");
764 Value::asUInt() const
769 JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range");
770 return UInt(value_
.int_
);
772 JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range");
773 return UInt(value_
.uint_
);
775 JSON_ASSERT_MESSAGE(InRange(value_
.real_
, 0, maxUInt
), "double out of UInt range");
776 return UInt( value_
.real_
);
780 return value_
.bool_
? 1 : 0;
784 JSON_FAIL_MESSAGE("Value is not convertible to UInt.");
788 # if defined(JSON_HAS_INT64)
791 Value::asInt64() const
796 return Int64(value_
.int_
);
798 JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range");
799 return Int64(value_
.uint_
);
801 JSON_ASSERT_MESSAGE(InRange(value_
.real_
, minInt64
, maxInt64
), "double out of Int64 range");
802 return Int64(value_
.real_
);
806 return value_
.bool_
? 1 : 0;
810 JSON_FAIL_MESSAGE("Value is not convertible to Int64.");
815 Value::asUInt64() const
820 JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range");
821 return UInt64(value_
.int_
);
823 return UInt64(value_
.uint_
);
825 JSON_ASSERT_MESSAGE(InRange(value_
.real_
, 0, maxUInt64
), "double out of UInt64 range");
826 return UInt64( value_
.real_
);
830 return value_
.bool_
? 1 : 0;
834 JSON_FAIL_MESSAGE("Value is not convertible to UInt64.");
836 # endif // if defined(JSON_HAS_INT64)
840 Value::asLargestInt() const
842 #if defined(JSON_NO_INT64)
851 Value::asLargestUInt() const
853 #if defined(JSON_NO_INT64)
862 Value::asDouble() const
867 return static_cast<double>( value_
.int_
);
869 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
870 return static_cast<double>( value_
.uint_
);
871 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
872 return integerToDouble( value_
.uint_
);
873 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
879 return value_
.bool_
? 1.0 : 0.0;
883 JSON_FAIL_MESSAGE("Value is not convertible to double.");
887 Value::asFloat() const
892 return static_cast<float>( value_
.int_
);
894 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
895 return static_cast<float>( value_
.uint_
);
896 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
897 return integerToDouble( value_
.uint_
);
898 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
900 return static_cast<float>( value_
.real_
);
904 return value_
.bool_
? 1.0f
: 0.0f
;
908 JSON_FAIL_MESSAGE("Value is not convertible to float.");
912 Value::asBool() const
921 return value_
.int_
? true : false;
923 return value_
.uint_
? true : false;
925 return value_
.real_
? true : false;
929 JSON_FAIL_MESSAGE("Value is not convertible to bool.");
934 Value::isConvertibleTo( ValueType other
) const
939 return ( isNumeric() && asDouble() == 0.0 )
940 || ( type_
== booleanValue
&& value_
.bool_
== false )
941 || ( type_
== stringValue
&& asString() == "" )
942 || ( type_
== arrayValue
&& value_
.map_
->size() == 0 )
943 || ( type_
== objectValue
&& value_
.map_
->size() == 0 )
944 || type_
== nullValue
;
947 || (type_
== realValue
&& InRange(value_
.real_
, minInt
, maxInt
))
948 || type_
== booleanValue
949 || type_
== nullValue
;
952 || (type_
== realValue
&& InRange(value_
.real_
, 0, maxUInt
))
953 || type_
== booleanValue
954 || type_
== nullValue
;
957 || type_
== booleanValue
958 || type_
== nullValue
;
961 || type_
== booleanValue
962 || type_
== nullValue
;
965 || type_
== booleanValue
966 || type_
== stringValue
967 || type_
== nullValue
;
969 return type_
== arrayValue
970 || type_
== nullValue
;
972 return type_
== objectValue
973 || type_
== nullValue
;
975 JSON_ASSERT_UNREACHABLE
;
980 /// Number of values in array or object
993 #ifndef JSON_VALUE_USE_INTERNAL_MAP
994 case arrayValue
: // size of the array is highest index + 1
995 if ( !value_
.map_
->empty() )
997 ObjectValues::const_iterator itLast
= value_
.map_
->end();
999 return (*itLast
).first
.index()+1;
1003 return ArrayIndex( value_
.map_
->size() );
1006 return Int( value_
.array_
->size() );
1008 return Int( value_
.map_
->size() );
1011 JSON_ASSERT_UNREACHABLE
;
1012 return 0; // unreachable;
1017 Value::empty() const
1019 if ( isNull() || isArray() || isObject() )
1020 return size() == 0u;
1027 Value::operator!() const
1036 JSON_ASSERT( type_
== nullValue
|| type_
== arrayValue
|| type_
== objectValue
);
1040 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1043 value_
.map_
->clear();
1047 value_
.array_
->clear();
1050 value_
.map_
->clear();
1059 Value::resize( ArrayIndex newSize
)
1061 JSON_ASSERT( type_
== nullValue
|| type_
== arrayValue
);
1062 if ( type_
== nullValue
)
1063 *this = Value( arrayValue
);
1064 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1065 ArrayIndex oldSize
= size();
1068 else if ( newSize
> oldSize
)
1069 (*this)[ newSize
- 1 ];
1072 for ( ArrayIndex index
= newSize
; index
< oldSize
; ++index
)
1074 value_
.map_
->erase( index
);
1076 assert( size() == newSize
);
1079 value_
.array_
->resize( newSize
);
1085 Value::operator[]( ArrayIndex index
)
1087 JSON_ASSERT( type_
== nullValue
|| type_
== arrayValue
);
1088 if ( type_
== nullValue
)
1089 *this = Value( arrayValue
);
1090 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1091 CZString
key( index
);
1092 ObjectValues::iterator it
= value_
.map_
->lower_bound( key
);
1093 if ( it
!= value_
.map_
->end() && (*it
).first
== key
)
1094 return (*it
).second
;
1096 ObjectValues::value_type
defaultValue( key
, null
);
1097 it
= value_
.map_
->insert( it
, defaultValue
);
1098 return (*it
).second
;
1100 return value_
.array_
->resolveReference( index
);
1106 Value::operator[]( int index
)
1108 JSON_ASSERT( index
>= 0 );
1109 return (*this)[ ArrayIndex(index
) ];
1114 Value::operator[]( ArrayIndex index
) const
1116 JSON_ASSERT( type_
== nullValue
|| type_
== arrayValue
);
1117 if ( type_
== nullValue
)
1119 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1120 CZString
key( index
);
1121 ObjectValues::const_iterator it
= value_
.map_
->find( key
);
1122 if ( it
== value_
.map_
->end() )
1124 return (*it
).second
;
1126 Value
*value
= value_
.array_
->find( index
);
1127 return value
? *value
: null
;
1133 Value::operator[]( int index
) const
1135 JSON_ASSERT( index
>= 0 );
1136 return (*this)[ ArrayIndex(index
) ];
1141 Value::operator[]( const char *key
)
1143 return resolveReference( key
, false );
1148 Value::resolveReference( const char *key
,
1151 JSON_ASSERT( type_
== nullValue
|| type_
== objectValue
);
1152 if ( type_
== nullValue
)
1153 *this = Value( objectValue
);
1154 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1155 CZString
actualKey( key
, isStatic
? CZString::noDuplication
1156 : CZString::duplicateOnCopy
);
1157 ObjectValues::iterator it
= value_
.map_
->lower_bound( actualKey
);
1158 if ( it
!= value_
.map_
->end() && (*it
).first
== actualKey
)
1159 return (*it
).second
;
1161 ObjectValues::value_type
defaultValue( actualKey
, null
);
1162 it
= value_
.map_
->insert( it
, defaultValue
);
1163 Value
&value
= (*it
).second
;
1166 return value_
.map_
->resolveReference( key
, isStatic
);
1172 Value::get( ArrayIndex index
,
1173 const Value
&defaultValue
) const
1175 const Value
*value
= &((*this)[index
]);
1176 return value
== &null
? defaultValue
: *value
;
1181 Value::isValidIndex( ArrayIndex index
) const
1183 return index
< size();
1189 Value::operator[]( const char *key
) const
1191 JSON_ASSERT( type_
== nullValue
|| type_
== objectValue
);
1192 if ( type_
== nullValue
)
1194 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1195 CZString
actualKey( key
, CZString::noDuplication
);
1196 ObjectValues::const_iterator it
= value_
.map_
->find( actualKey
);
1197 if ( it
== value_
.map_
->end() )
1199 return (*it
).second
;
1201 const Value
*value
= value_
.map_
->find( key
);
1202 return value
? *value
: null
;
1208 Value::operator[]( const std::string
&key
)
1210 return (*this)[ key
.c_str() ];
1215 Value::operator[]( const std::string
&key
) const
1217 return (*this)[ key
.c_str() ];
1221 Value::operator[]( const StaticString
&key
)
1223 return resolveReference( key
, true );
1227 # ifdef JSON_USE_CPPTL
1229 Value::operator[]( const CppTL::ConstString
&key
)
1231 return (*this)[ key
.c_str() ];
1236 Value::operator[]( const CppTL::ConstString
&key
) const
1238 return (*this)[ key
.c_str() ];
1244 Value::append( const Value
&value
)
1246 return (*this)[size()] = value
;
1251 Value::get( const char *key
,
1252 const Value
&defaultValue
) const
1254 const Value
*value
= &((*this)[key
]);
1255 return value
== &null
? defaultValue
: *value
;
1260 Value::get( const std::string
&key
,
1261 const Value
&defaultValue
) const
1263 return get( key
.c_str(), defaultValue
);
1267 Value::removeMember( const char* key
)
1269 JSON_ASSERT( type_
== nullValue
|| type_
== objectValue
);
1270 if ( type_
== nullValue
)
1272 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1273 CZString
actualKey( key
, CZString::noDuplication
);
1274 ObjectValues::iterator it
= value_
.map_
->find( actualKey
);
1275 if ( it
== value_
.map_
->end() )
1277 Value
old(it
->second
);
1278 value_
.map_
->erase(it
);
1281 Value
*value
= value_
.map_
->find( key
);
1284 value_
.map_
.remove( key
);
1293 Value::removeMember( const std::string
&key
)
1295 return removeMember( key
.c_str() );
1298 # ifdef JSON_USE_CPPTL
1300 Value::get( const CppTL::ConstString
&key
,
1301 const Value
&defaultValue
) const
1303 return get( key
.c_str(), defaultValue
);
1308 Value::isMember( const char *key
) const
1310 const Value
*value
= &((*this)[key
]);
1311 return value
!= &null
;
1316 Value::isMember( const std::string
&key
) const
1318 return isMember( key
.c_str() );
1322 # ifdef JSON_USE_CPPTL
1324 Value::isMember( const CppTL::ConstString
&key
) const
1326 return isMember( key
.c_str() );
1331 Value::getMemberNames() const
1333 JSON_ASSERT( type_
== nullValue
|| type_
== objectValue
);
1334 if ( type_
== nullValue
)
1335 return Value::Members();
1337 members
.reserve( value_
.map_
->size() );
1338 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1339 ObjectValues::const_iterator it
= value_
.map_
->begin();
1340 ObjectValues::const_iterator itEnd
= value_
.map_
->end();
1341 for ( ; it
!= itEnd
; ++it
)
1342 members
.push_back( std::string( (*it
).first
.c_str() ) );
1344 ValueInternalMap::IteratorState it
;
1345 ValueInternalMap::IteratorState itEnd
;
1346 value_
.map_
->makeBeginIterator( it
);
1347 value_
.map_
->makeEndIterator( itEnd
);
1348 for ( ; !ValueInternalMap::equals( it
, itEnd
); ValueInternalMap::increment(it
) )
1349 members
.push_back( std::string( ValueInternalMap::key( it
) ) );
1354 //# ifdef JSON_USE_CPPTL
1356 //Value::enumMemberNames() const
1358 // if ( type_ == objectValue )
1360 // return CppTL::Enum::any( CppTL::Enum::transform(
1361 // CppTL::Enum::keys( *(value_.map_), CppTL::Type<const CZString &>() ),
1362 // MemberNamesTransform() ) );
1364 // return EnumMemberNames();
1369 //Value::enumValues() const
1371 // if ( type_ == objectValue || type_ == arrayValue )
1372 // return CppTL::Enum::anyValues( *(value_.map_),
1373 // CppTL::Type<const Value &>() );
1374 // return EnumValues();
1379 static bool IsIntegral(double d
) {
1380 double integral_part
;
1381 return modf(d
, &integral_part
) == 0.0;
1386 Value::isNull() const
1388 return type_
== nullValue
;
1393 Value::isBool() const
1395 return type_
== booleanValue
;
1400 Value::isInt() const
1405 return value_
.int_
>= minInt
&& value_
.int_
<= maxInt
;
1407 return value_
.uint_
<= UInt(maxInt
);
1409 return value_
.real_
>= minInt
&&
1410 value_
.real_
<= maxInt
&&
1411 IsIntegral(value_
.real_
);
1420 Value::isUInt() const
1425 return value_
.int_
>= 0 && LargestUInt(value_
.int_
) <= LargestUInt(maxUInt
);
1427 return value_
.uint_
<= maxUInt
;
1429 return value_
.real_
>= 0 &&
1430 value_
.real_
<= maxUInt
&&
1431 IsIntegral(value_
.real_
);
1439 Value::isInt64() const
1441 # if defined(JSON_HAS_INT64)
1447 return value_
.uint_
<= UInt64(maxInt64
);
1449 // Note that maxInt64 (= 2^63 - 1) is not exactly representable as a
1450 // double, so double(maxInt64) will be rounded up to 2^63. Therefore we
1451 // require the value to be strictly less than the limit.
1452 return value_
.real_
>= double(minInt64
) &&
1453 value_
.real_
< double(maxInt64
) &&
1454 IsIntegral(value_
.real_
);
1458 # endif // JSON_HAS_INT64
1463 Value::isUInt64() const
1465 # if defined(JSON_HAS_INT64)
1469 return value_
.int_
>= 0;
1473 // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a
1474 // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we
1475 // require the value to be strictly less than the limit.
1476 return value_
.real_
>= 0 &&
1477 value_
.real_
< maxUInt64AsDouble
&&
1478 IsIntegral(value_
.real_
);
1482 # endif // JSON_HAS_INT64
1488 Value::isIntegral() const
1490 #if defined(JSON_HAS_INT64)
1491 return isInt64() || isUInt64();
1493 return isInt() || isUInt();
1499 Value::isDouble() const
1501 return type_
== realValue
|| isIntegral();
1506 Value::isNumeric() const
1508 return isIntegral() || isDouble();
1513 Value::isString() const
1515 return type_
== stringValue
;
1520 Value::isArray() const
1522 return type_
== arrayValue
;
1527 Value::isObject() const
1529 return type_
== objectValue
;
1534 Value::setComment( const char *comment
,
1535 CommentPlacement placement
)
1538 comments_
= new CommentInfo
[numberOfCommentPlacement
];
1539 comments_
[placement
].setComment( comment
);
1544 Value::setComment( const std::string
&comment
,
1545 CommentPlacement placement
)
1547 setComment( comment
.c_str(), placement
);
1552 Value::hasComment( CommentPlacement placement
) const
1554 return comments_
!= 0 && comments_
[placement
].comment_
!= 0;
1558 Value::getComment( CommentPlacement placement
) const
1560 if ( hasComment(placement
) )
1561 return comments_
[placement
].comment_
;
1567 Value::toStyledString() const
1569 StyledWriter writer
;
1570 return writer
.write( *this );
1574 Value::const_iterator
1575 Value::begin() const
1579 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1581 if ( value_
.array_
)
1583 ValueInternalArray::IteratorState it
;
1584 value_
.array_
->makeBeginIterator( it
);
1585 return const_iterator( it
);
1591 ValueInternalMap::IteratorState it
;
1592 value_
.map_
->makeBeginIterator( it
);
1593 return const_iterator( it
);
1600 return const_iterator( value_
.map_
->begin() );
1606 return const_iterator();
1609 Value::const_iterator
1614 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1616 if ( value_
.array_
)
1618 ValueInternalArray::IteratorState it
;
1619 value_
.array_
->makeEndIterator( it
);
1620 return const_iterator( it
);
1626 ValueInternalMap::IteratorState it
;
1627 value_
.map_
->makeEndIterator( it
);
1628 return const_iterator( it
);
1635 return const_iterator( value_
.map_
->end() );
1641 return const_iterator();
1650 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1652 if ( value_
.array_
)
1654 ValueInternalArray::IteratorState it
;
1655 value_
.array_
->makeBeginIterator( it
);
1656 return iterator( it
);
1662 ValueInternalMap::IteratorState it
;
1663 value_
.map_
->makeBeginIterator( it
);
1664 return iterator( it
);
1671 return iterator( value_
.map_
->begin() );
1685 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1687 if ( value_
.array_
)
1689 ValueInternalArray::IteratorState it
;
1690 value_
.array_
->makeEndIterator( it
);
1691 return iterator( it
);
1697 ValueInternalMap::IteratorState it
;
1698 value_
.map_
->makeEndIterator( it
);
1699 return iterator( it
);
1706 return iterator( value_
.map_
->end() );
1716 // class PathArgument
1717 // //////////////////////////////////////////////////////////////////
1719 PathArgument::PathArgument()
1727 PathArgument::PathArgument( ArrayIndex index
)
1730 , kind_( kindIndex
)
1735 PathArgument::PathArgument( const char *key
)
1743 PathArgument::PathArgument( const std::string
&key
)
1744 : key_( key
.c_str() )
1751 // //////////////////////////////////////////////////////////////////
1753 Path::Path( const std::string
&path
,
1754 const PathArgument
&a1
,
1755 const PathArgument
&a2
,
1756 const PathArgument
&a3
,
1757 const PathArgument
&a4
,
1758 const PathArgument
&a5
)
1761 in
.push_back( &a1
);
1762 in
.push_back( &a2
);
1763 in
.push_back( &a3
);
1764 in
.push_back( &a4
);
1765 in
.push_back( &a5
);
1766 makePath( path
, in
);
1771 Path::makePath( const std::string
&path
,
1774 const char *current
= path
.c_str();
1775 const char *end
= current
+ path
.length();
1776 InArgs::const_iterator itInArg
= in
.begin();
1777 while ( current
!= end
)
1779 if ( *current
== '[' )
1782 if ( *current
== '%' )
1783 addPathInArg( path
, in
, itInArg
, PathArgument::kindIndex
);
1786 ArrayIndex index
= 0;
1787 for ( ; current
!= end
&& *current
>= '0' && *current
<= '9'; ++current
)
1788 index
= index
* 10 + ArrayIndex(*current
- '0');
1789 args_
.push_back( index
);
1791 if ( current
== end
|| *current
++ != ']' )
1792 invalidPath( path
, int(current
- path
.c_str()) );
1794 else if ( *current
== '%' )
1796 addPathInArg( path
, in
, itInArg
, PathArgument::kindKey
);
1799 else if ( *current
== '.' )
1805 const char *beginName
= current
;
1806 while ( current
!= end
&& !strchr( "[.", *current
) )
1808 args_
.push_back( std::string( beginName
, current
) );
1815 Path::addPathInArg( const std::string
&path
,
1817 InArgs::const_iterator
&itInArg
,
1818 PathArgument::Kind kind
)
1820 if ( itInArg
== in
.end() )
1822 // Error: missing argument %d
1824 else if ( (*itInArg
)->kind_
!= kind
)
1826 // Error: bad argument type
1830 args_
.push_back( **itInArg
);
1836 Path::invalidPath( const std::string
&path
,
1839 // Error: invalid path.
1844 Path::resolve( const Value
&root
) const
1846 const Value
*node
= &root
;
1847 for ( Args::const_iterator it
= args_
.begin(); it
!= args_
.end(); ++it
)
1849 const PathArgument
&arg
= *it
;
1850 if ( arg
.kind_
== PathArgument::kindIndex
)
1852 if ( !node
->isArray() || !node
->isValidIndex( arg
.index_
) )
1854 // Error: unable to resolve path (array value expected at position...
1856 node
= &((*node
)[arg
.index_
]);
1858 else if ( arg
.kind_
== PathArgument::kindKey
)
1860 if ( !node
->isObject() )
1862 // Error: unable to resolve path (object value expected at position...)
1864 node
= &((*node
)[arg
.key_
]);
1865 if ( node
== &Value::null
)
1867 // Error: unable to resolve path (object has no member named '' at position...)
1876 Path::resolve( const Value
&root
,
1877 const Value
&defaultValue
) const
1879 const Value
*node
= &root
;
1880 for ( Args::const_iterator it
= args_
.begin(); it
!= args_
.end(); ++it
)
1882 const PathArgument
&arg
= *it
;
1883 if ( arg
.kind_
== PathArgument::kindIndex
)
1885 if ( !node
->isArray() || !node
->isValidIndex( arg
.index_
) )
1886 return defaultValue
;
1887 node
= &((*node
)[arg
.index_
]);
1889 else if ( arg
.kind_
== PathArgument::kindKey
)
1891 if ( !node
->isObject() )
1892 return defaultValue
;
1893 node
= &((*node
)[arg
.key_
]);
1894 if ( node
== &Value::null
)
1895 return defaultValue
;
1903 Path::make( Value
&root
) const
1905 Value
*node
= &root
;
1906 for ( Args::const_iterator it
= args_
.begin(); it
!= args_
.end(); ++it
)
1908 const PathArgument
&arg
= *it
;
1909 if ( arg
.kind_
== PathArgument::kindIndex
)
1911 if ( !node
->isArray() )
1913 // Error: node is not an array at position ...
1915 node
= &((*node
)[arg
.index_
]);
1917 else if ( arg
.kind_
== PathArgument::kindKey
)
1919 if ( !node
->isObject() )
1921 // Error: node is not an object at position...
1923 node
= &((*node
)[arg
.key_
]);