cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / third_party / jsoncpp / overrides / src / lib_json / json_value.cpp
bloba2a4a6761d2cdb346c0764c5dd84266cc7619d3a
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)
14 #include <math.h>
15 #include <sstream>
16 #include <utility>
17 #include <stdexcept>
18 #include <cstring>
19 #include <cassert>
20 #ifdef JSON_USE_CPPTL
21 # include <cpptl/conststring.h>
22 #endif
23 #include <cstddef> // size_t
25 #define JSON_ASSERT_UNREACHABLE assert( false )
27 namespace Json {
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)))
34 #else
35 #define ALIGNAS(byte_alignment)
36 #endif
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);
71 template<typename T>
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.
91 static inline char *
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
99 // to a sane value.
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;
107 return newString;
111 /** Free the string duplicated by duplicateStringValue().
113 static inline void
114 releaseStringValue( char *value )
116 if ( value )
117 free( value );
120 } // namespace Json
123 // //////////////////////////////////////////////////////////////////
124 // //////////////////////////////////////////////////////////////////
125 // //////////////////////////////////////////////////////////////////
126 // ValueInternals...
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)
139 namespace Json {
141 // //////////////////////////////////////////////////////////////////
142 // //////////////////////////////////////////////////////////////////
143 // //////////////////////////////////////////////////////////////////
144 // class Value::CommentInfo
145 // //////////////////////////////////////////////////////////////////
146 // //////////////////////////////////////////////////////////////////
147 // //////////////////////////////////////////////////////////////////
150 Value::CommentInfo::CommentInfo()
151 : comment_( 0 )
155 Value::CommentInfo::~CommentInfo()
157 if ( comment_ )
158 releaseStringValue( comment_ );
162 void
163 Value::CommentInfo::setComment( const char *text )
165 if ( comment_ )
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 )
187 : cstr_( 0 )
188 , index_( index )
192 Value::CZString::CZString( const char *cstr, DuplicationPolicy allocate )
193 : cstr_( allocate == duplicate ? duplicateStringValue(cstr)
194 : cstr )
195 , index_( allocate )
199 Value::CZString::CZString( const CZString &other )
200 : cstr_( other.index_ != noDuplication && other.cstr_ != 0
201 ? duplicateStringValue( other.cstr_ )
202 : other.cstr_ )
203 , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
204 : other.index_ )
208 Value::CZString::~CZString()
210 if ( cstr_ && index_ == duplicate )
211 releaseStringValue( const_cast<char *>( cstr_ ) );
214 void
215 Value::CZString::swap( CZString &other )
217 std::swap( cstr_, other.cstr_ );
218 std::swap( index_, other.index_ );
221 Value::CZString &
222 Value::CZString::operator =( const CZString &other )
224 CZString temp( other );
225 swap( temp );
226 return *this;
229 bool
230 Value::CZString::operator<( const CZString &other ) const
232 if ( cstr_ )
233 return strcmp( cstr_, other.cstr_ ) < 0;
234 return index_ < other.index_;
237 bool
238 Value::CZString::operator==( const CZString &other ) const
240 if ( cstr_ )
241 return strcmp( cstr_, other.cstr_ ) == 0;
242 return index_ == other.index_;
246 ArrayIndex
247 Value::CZString::index() const
249 return index_;
253 const char *
254 Value::CZString::c_str() const
256 return cstr_;
259 bool
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 )
281 : type_( type )
282 , allocated_( false )
283 # ifdef JSON_VALUE_USE_INTERNAL_MAP
284 , itemIsUsed_( 0 )
285 #endif
286 , comments_( 0 )
288 switch ( type )
290 case nullValue:
291 break;
292 case intValue:
293 case uintValue:
294 value_.int_ = 0;
295 break;
296 case realValue:
297 value_.real_ = 0.0;
298 break;
299 case stringValue:
300 value_.string_ = 0;
301 break;
302 #ifndef JSON_VALUE_USE_INTERNAL_MAP
303 case arrayValue:
304 case objectValue:
305 value_.map_ = new ObjectValues();
306 break;
307 #else
308 case arrayValue:
309 value_.array_ = arrayAllocator()->newArray();
310 break;
311 case objectValue:
312 value_.map_ = mapAllocator()->newMap();
313 break;
314 #endif
315 case booleanValue:
316 value_.bool_ = false;
317 break;
318 default:
319 JSON_ASSERT_UNREACHABLE;
324 Value::Value( UInt value )
325 : type_( uintValue )
326 , allocated_( false )
327 # ifdef JSON_VALUE_USE_INTERNAL_MAP
328 , itemIsUsed_( 0 )
329 #endif
330 , comments_( 0 )
332 value_.uint_ = value;
335 Value::Value( Int value )
336 : type_( intValue )
337 , allocated_( false )
338 # ifdef JSON_VALUE_USE_INTERNAL_MAP
339 , itemIsUsed_( 0 )
340 #endif
341 , comments_( 0 )
343 value_.int_ = value;
347 # if defined(JSON_HAS_INT64)
348 Value::Value( Int64 value )
349 : type_( intValue )
350 , allocated_( false )
351 # ifdef JSON_VALUE_USE_INTERNAL_MAP
352 , itemIsUsed_( 0 )
353 #endif
354 , comments_( 0 )
356 value_.int_ = value;
360 Value::Value( UInt64 value )
361 : type_( uintValue )
362 , allocated_( false )
363 # ifdef JSON_VALUE_USE_INTERNAL_MAP
364 , itemIsUsed_( 0 )
365 #endif
366 , comments_( 0 )
368 value_.uint_ = value;
370 #endif // defined(JSON_HAS_INT64)
372 Value::Value( double value )
373 : type_( realValue )
374 , allocated_( false )
375 # ifdef JSON_VALUE_USE_INTERNAL_MAP
376 , itemIsUsed_( 0 )
377 #endif
378 , comments_( 0 )
380 value_.real_ = value;
383 Value::Value( const char *value )
384 : type_( stringValue )
385 , allocated_( true )
386 # ifdef JSON_VALUE_USE_INTERNAL_MAP
387 , itemIsUsed_( 0 )
388 #endif
389 , comments_( 0 )
391 value_.string_ = duplicateStringValue( value );
395 Value::Value( const char *beginValue,
396 const char *endValue )
397 : type_( stringValue )
398 , allocated_( true )
399 # ifdef JSON_VALUE_USE_INTERNAL_MAP
400 , itemIsUsed_( 0 )
401 #endif
402 , comments_( 0 )
404 value_.string_ = duplicateStringValue( beginValue,
405 (unsigned int)(endValue - beginValue) );
409 Value::Value( const std::string &value )
410 : type_( stringValue )
411 , allocated_( true )
412 # ifdef JSON_VALUE_USE_INTERNAL_MAP
413 , itemIsUsed_( 0 )
414 #endif
415 , comments_( 0 )
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
426 , itemIsUsed_( 0 )
427 #endif
428 , comments_( 0 )
430 value_.string_ = const_cast<char *>( value.c_str() );
434 # ifdef JSON_USE_CPPTL
435 Value::Value( const CppTL::ConstString &value )
436 : type_( stringValue )
437 , allocated_( true )
438 # ifdef JSON_VALUE_USE_INTERNAL_MAP
439 , itemIsUsed_( 0 )
440 #endif
441 , comments_( 0 )
443 value_.string_ = duplicateStringValue( value, value.length() );
445 # endif
447 Value::Value( bool value )
448 : type_( booleanValue )
449 , allocated_( false )
450 # ifdef JSON_VALUE_USE_INTERNAL_MAP
451 , itemIsUsed_( 0 )
452 #endif
453 , comments_( 0 )
455 value_.bool_ = value;
459 Value::Value( const Value &other )
460 : type_( other.type_ )
461 , allocated_( false )
462 # ifdef JSON_VALUE_USE_INTERNAL_MAP
463 , itemIsUsed_( 0 )
464 #endif
465 , comments_( 0 )
467 switch ( type_ )
469 case nullValue:
470 case intValue:
471 case uintValue:
472 case realValue:
473 case booleanValue:
474 value_ = other.value_;
475 break;
476 case stringValue:
477 if ( other.value_.string_ )
479 value_.string_ = duplicateStringValue( other.value_.string_ );
480 allocated_ = true;
482 else
483 value_.string_ = 0;
484 break;
485 #ifndef JSON_VALUE_USE_INTERNAL_MAP
486 case arrayValue:
487 case objectValue:
488 value_.map_ = new ObjectValues( *other.value_.map_ );
489 break;
490 #else
491 case arrayValue:
492 value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ );
493 break;
494 case objectValue:
495 value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ );
496 break;
497 #endif
498 default:
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_ );
514 Value::~Value()
516 switch ( type_ )
518 case nullValue:
519 case intValue:
520 case uintValue:
521 case realValue:
522 case booleanValue:
523 break;
524 case stringValue:
525 if ( allocated_ )
526 releaseStringValue( value_.string_ );
527 break;
528 #ifndef JSON_VALUE_USE_INTERNAL_MAP
529 case arrayValue:
530 case objectValue:
531 delete value_.map_;
532 break;
533 #else
534 case arrayValue:
535 arrayAllocator()->destructArray( value_.array_ );
536 break;
537 case objectValue:
538 mapAllocator()->destructMap( value_.map_ );
539 break;
540 #endif
541 default:
542 JSON_ASSERT_UNREACHABLE;
545 if ( comments_ )
546 delete[] comments_;
549 Value &
550 Value::operator=( const Value &other )
552 Value temp( other );
553 swap( temp );
554 return *this;
557 void
558 Value::swap( Value &other )
560 ValueType temp = type_;
561 type_ = other.type_;
562 other.type_ = temp;
563 std::swap( value_, other.value_ );
564 int temp2 = allocated_;
565 allocated_ = other.allocated_;
566 other.allocated_ = temp2;
569 ValueType
570 Value::type() const
572 return type_;
576 int
577 Value::compare( const Value &other ) const
579 if ( *this < other )
580 return -1;
581 if ( *this > other )
582 return 1;
583 return 0;
587 bool
588 Value::operator <( const Value &other ) const
590 int typeDelta = type_ - other.type_;
591 if ( typeDelta )
592 return typeDelta < 0 ? true : false;
593 switch ( type_ )
595 case nullValue:
596 return false;
597 case intValue:
598 return value_.int_ < other.value_.int_;
599 case uintValue:
600 return value_.uint_ < other.value_.uint_;
601 case realValue:
602 return value_.real_ < other.value_.real_;
603 case booleanValue:
604 return value_.bool_ < other.value_.bool_;
605 case stringValue:
606 return ( value_.string_ == 0 && other.value_.string_ )
607 || ( other.value_.string_
608 && value_.string_
609 && strcmp( value_.string_, other.value_.string_ ) < 0 );
610 #ifndef JSON_VALUE_USE_INTERNAL_MAP
611 case arrayValue:
612 case objectValue:
614 int delta = int( value_.map_->size() - other.value_.map_->size() );
615 if ( delta )
616 return delta < 0;
617 return (*value_.map_) < (*other.value_.map_);
619 #else
620 case arrayValue:
621 return value_.array_->compare( *(other.value_.array_) ) < 0;
622 case objectValue:
623 return value_.map_->compare( *(other.value_.map_) ) < 0;
624 #endif
625 default:
626 JSON_ASSERT_UNREACHABLE;
628 return false; // unreachable
631 bool
632 Value::operator <=( const Value &other ) const
634 return !(other < *this);
637 bool
638 Value::operator >=( const Value &other ) const
640 return !(*this < other);
643 bool
644 Value::operator >( const Value &other ) const
646 return other < *this;
649 bool
650 Value::operator ==( const Value &other ) const
652 //if ( type_ != other.type_ )
653 // GCC 2.95.3 says:
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_;
657 if ( type_ != temp )
658 return false;
659 switch ( type_ )
661 case nullValue:
662 return true;
663 case intValue:
664 return value_.int_ == other.value_.int_;
665 case uintValue:
666 return value_.uint_ == other.value_.uint_;
667 case realValue:
668 return value_.real_ == other.value_.real_;
669 case booleanValue:
670 return value_.bool_ == other.value_.bool_;
671 case stringValue:
672 return ( value_.string_ == other.value_.string_ )
673 || ( other.value_.string_
674 && value_.string_
675 && strcmp( value_.string_, other.value_.string_ ) == 0 );
676 #ifndef JSON_VALUE_USE_INTERNAL_MAP
677 case arrayValue:
678 case objectValue:
679 return value_.map_->size() == other.value_.map_->size()
680 && (*value_.map_) == (*other.value_.map_);
681 #else
682 case arrayValue:
683 return value_.array_->compare( *(other.value_.array_) ) == 0;
684 case objectValue:
685 return value_.map_->compare( *(other.value_.map_) ) == 0;
686 #endif
687 default:
688 JSON_ASSERT_UNREACHABLE;
690 return false; // unreachable
693 bool
694 Value::operator !=( const Value &other ) const
696 return !( *this == other );
699 const char *
700 Value::asCString() const
702 JSON_ASSERT( type_ == stringValue );
703 return value_.string_;
707 std::string
708 Value::asString() const
710 switch ( type_ )
712 case nullValue:
713 return "";
714 case stringValue:
715 return value_.string_ ? value_.string_ : "";
716 case booleanValue:
717 return value_.bool_ ? "true" : "false";
718 case intValue:
719 return valueToString( value_.int_ );
720 case uintValue:
721 return valueToString( value_.uint_ );
722 case realValue:
723 return valueToString( value_.real_ );
724 default:
725 JSON_FAIL_MESSAGE( "Type is not convertible to string" );
729 # ifdef JSON_USE_CPPTL
730 CppTL::ConstString
731 Value::asConstString() const
733 return CppTL::ConstString( asString().c_str() );
735 # endif
738 Value::Int
739 Value::asInt() const
741 switch ( type_ )
743 case intValue:
744 JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range");
745 return Int(value_.int_);
746 case uintValue:
747 JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range");
748 return Int(value_.uint_);
749 case realValue:
750 JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt), "double out of Int range");
751 return Int(value_.real_);
752 case nullValue:
753 return 0;
754 case booleanValue:
755 return value_.bool_ ? 1 : 0;
756 default:
757 break;
759 JSON_FAIL_MESSAGE("Value is not convertible to Int.");
763 Value::UInt
764 Value::asUInt() const
766 switch ( type_ )
768 case intValue:
769 JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range");
770 return UInt(value_.int_);
771 case uintValue:
772 JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range");
773 return UInt(value_.uint_);
774 case realValue:
775 JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt), "double out of UInt range");
776 return UInt( value_.real_ );
777 case nullValue:
778 return 0;
779 case booleanValue:
780 return value_.bool_ ? 1 : 0;
781 default:
782 break;
784 JSON_FAIL_MESSAGE("Value is not convertible to UInt.");
788 # if defined(JSON_HAS_INT64)
790 Value::Int64
791 Value::asInt64() const
793 switch ( type_ )
795 case intValue:
796 return Int64(value_.int_);
797 case uintValue:
798 JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range");
799 return Int64(value_.uint_);
800 case realValue:
801 JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt64, maxInt64), "double out of Int64 range");
802 return Int64(value_.real_);
803 case nullValue:
804 return 0;
805 case booleanValue:
806 return value_.bool_ ? 1 : 0;
807 default:
808 break;
810 JSON_FAIL_MESSAGE("Value is not convertible to Int64.");
814 Value::UInt64
815 Value::asUInt64() const
817 switch ( type_ )
819 case intValue:
820 JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range");
821 return UInt64(value_.int_);
822 case uintValue:
823 return UInt64(value_.uint_);
824 case realValue:
825 JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64), "double out of UInt64 range");
826 return UInt64( value_.real_ );
827 case nullValue:
828 return 0;
829 case booleanValue:
830 return value_.bool_ ? 1 : 0;
831 default:
832 break;
834 JSON_FAIL_MESSAGE("Value is not convertible to UInt64.");
836 # endif // if defined(JSON_HAS_INT64)
839 LargestInt
840 Value::asLargestInt() const
842 #if defined(JSON_NO_INT64)
843 return asInt();
844 #else
845 return asInt64();
846 #endif
850 LargestUInt
851 Value::asLargestUInt() const
853 #if defined(JSON_NO_INT64)
854 return asUInt();
855 #else
856 return asUInt64();
857 #endif
861 double
862 Value::asDouble() const
864 switch ( type_ )
866 case intValue:
867 return static_cast<double>( value_.int_ );
868 case uintValue:
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)
874 case realValue:
875 return value_.real_;
876 case nullValue:
877 return 0.0;
878 case booleanValue:
879 return value_.bool_ ? 1.0 : 0.0;
880 default:
881 break;
883 JSON_FAIL_MESSAGE("Value is not convertible to double.");
886 float
887 Value::asFloat() const
889 switch ( type_ )
891 case intValue:
892 return static_cast<float>( value_.int_ );
893 case uintValue:
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)
899 case realValue:
900 return static_cast<float>( value_.real_ );
901 case nullValue:
902 return 0.0;
903 case booleanValue:
904 return value_.bool_ ? 1.0f : 0.0f;
905 default:
906 break;
908 JSON_FAIL_MESSAGE("Value is not convertible to float.");
911 bool
912 Value::asBool() const
914 switch ( type_ )
916 case booleanValue:
917 return value_.bool_;
918 case nullValue:
919 return false;
920 case intValue:
921 return value_.int_ ? true : false;
922 case uintValue:
923 return value_.uint_ ? true : false;
924 case realValue:
925 return value_.real_ ? true : false;
926 default:
927 break;
929 JSON_FAIL_MESSAGE("Value is not convertible to bool.");
933 bool
934 Value::isConvertibleTo( ValueType other ) const
936 switch ( other )
938 case nullValue:
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;
945 case intValue:
946 return isInt()
947 || (type_ == realValue && InRange(value_.real_, minInt, maxInt))
948 || type_ == booleanValue
949 || type_ == nullValue;
950 case uintValue:
951 return isUInt()
952 || (type_ == realValue && InRange(value_.real_, 0, maxUInt))
953 || type_ == booleanValue
954 || type_ == nullValue;
955 case realValue:
956 return isNumeric()
957 || type_ == booleanValue
958 || type_ == nullValue;
959 case booleanValue:
960 return isNumeric()
961 || type_ == booleanValue
962 || type_ == nullValue;
963 case stringValue:
964 return isNumeric()
965 || type_ == booleanValue
966 || type_ == stringValue
967 || type_ == nullValue;
968 case arrayValue:
969 return type_ == arrayValue
970 || type_ == nullValue;
971 case objectValue:
972 return type_ == objectValue
973 || type_ == nullValue;
975 JSON_ASSERT_UNREACHABLE;
976 return false;
980 /// Number of values in array or object
981 ArrayIndex
982 Value::size() const
984 switch ( type_ )
986 case nullValue:
987 case intValue:
988 case uintValue:
989 case realValue:
990 case booleanValue:
991 case stringValue:
992 return 0;
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();
998 --itLast;
999 return (*itLast).first.index()+1;
1001 return 0;
1002 case objectValue:
1003 return ArrayIndex( value_.map_->size() );
1004 #else
1005 case arrayValue:
1006 return Int( value_.array_->size() );
1007 case objectValue:
1008 return Int( value_.map_->size() );
1009 #endif
1011 JSON_ASSERT_UNREACHABLE;
1012 return 0; // unreachable;
1016 bool
1017 Value::empty() const
1019 if ( isNull() || isArray() || isObject() )
1020 return size() == 0u;
1021 else
1022 return false;
1026 bool
1027 Value::operator!() const
1029 return isNull();
1033 void
1034 Value::clear()
1036 JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue );
1038 switch ( type_ )
1040 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1041 case arrayValue:
1042 case objectValue:
1043 value_.map_->clear();
1044 break;
1045 #else
1046 case arrayValue:
1047 value_.array_->clear();
1048 break;
1049 case objectValue:
1050 value_.map_->clear();
1051 break;
1052 #endif
1053 default:
1054 break;
1058 void
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();
1066 if ( newSize == 0 )
1067 clear();
1068 else if ( newSize > oldSize )
1069 (*this)[ newSize - 1 ];
1070 else
1072 for ( ArrayIndex index = newSize; index < oldSize; ++index )
1074 value_.map_->erase( index );
1076 assert( size() == newSize );
1078 #else
1079 value_.array_->resize( newSize );
1080 #endif
1084 Value &
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;
1099 #else
1100 return value_.array_->resolveReference( index );
1101 #endif
1105 Value &
1106 Value::operator[]( int index )
1108 JSON_ASSERT( index >= 0 );
1109 return (*this)[ ArrayIndex(index) ];
1113 const Value &
1114 Value::operator[]( ArrayIndex index ) const
1116 JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
1117 if ( type_ == nullValue )
1118 return null;
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() )
1123 return null;
1124 return (*it).second;
1125 #else
1126 Value *value = value_.array_->find( index );
1127 return value ? *value : null;
1128 #endif
1132 const Value &
1133 Value::operator[]( int index ) const
1135 JSON_ASSERT( index >= 0 );
1136 return (*this)[ ArrayIndex(index) ];
1140 Value &
1141 Value::operator[]( const char *key )
1143 return resolveReference( key, false );
1147 Value &
1148 Value::resolveReference( const char *key,
1149 bool isStatic )
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;
1164 return value;
1165 #else
1166 return value_.map_->resolveReference( key, isStatic );
1167 #endif
1171 Value
1172 Value::get( ArrayIndex index,
1173 const Value &defaultValue ) const
1175 const Value *value = &((*this)[index]);
1176 return value == &null ? defaultValue : *value;
1180 bool
1181 Value::isValidIndex( ArrayIndex index ) const
1183 return index < size();
1188 const Value &
1189 Value::operator[]( const char *key ) const
1191 JSON_ASSERT( type_ == nullValue || type_ == objectValue );
1192 if ( type_ == nullValue )
1193 return null;
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() )
1198 return null;
1199 return (*it).second;
1200 #else
1201 const Value *value = value_.map_->find( key );
1202 return value ? *value : null;
1203 #endif
1207 Value &
1208 Value::operator[]( const std::string &key )
1210 return (*this)[ key.c_str() ];
1214 const Value &
1215 Value::operator[]( const std::string &key ) const
1217 return (*this)[ key.c_str() ];
1220 Value &
1221 Value::operator[]( const StaticString &key )
1223 return resolveReference( key, true );
1227 # ifdef JSON_USE_CPPTL
1228 Value &
1229 Value::operator[]( const CppTL::ConstString &key )
1231 return (*this)[ key.c_str() ];
1235 const Value &
1236 Value::operator[]( const CppTL::ConstString &key ) const
1238 return (*this)[ key.c_str() ];
1240 # endif
1243 Value &
1244 Value::append( const Value &value )
1246 return (*this)[size()] = value;
1250 Value
1251 Value::get( const char *key,
1252 const Value &defaultValue ) const
1254 const Value *value = &((*this)[key]);
1255 return value == &null ? defaultValue : *value;
1259 Value
1260 Value::get( const std::string &key,
1261 const Value &defaultValue ) const
1263 return get( key.c_str(), defaultValue );
1266 Value
1267 Value::removeMember( const char* key )
1269 JSON_ASSERT( type_ == nullValue || type_ == objectValue );
1270 if ( type_ == nullValue )
1271 return null;
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() )
1276 return null;
1277 Value old(it->second);
1278 value_.map_->erase(it);
1279 return old;
1280 #else
1281 Value *value = value_.map_->find( key );
1282 if (value){
1283 Value old(*value);
1284 value_.map_.remove( key );
1285 return old;
1286 } else {
1287 return null;
1289 #endif
1292 Value
1293 Value::removeMember( const std::string &key )
1295 return removeMember( key.c_str() );
1298 # ifdef JSON_USE_CPPTL
1299 Value
1300 Value::get( const CppTL::ConstString &key,
1301 const Value &defaultValue ) const
1303 return get( key.c_str(), defaultValue );
1305 # endif
1307 bool
1308 Value::isMember( const char *key ) const
1310 const Value *value = &((*this)[key]);
1311 return value != &null;
1315 bool
1316 Value::isMember( const std::string &key ) const
1318 return isMember( key.c_str() );
1322 # ifdef JSON_USE_CPPTL
1323 bool
1324 Value::isMember( const CppTL::ConstString &key ) const
1326 return isMember( key.c_str() );
1328 #endif
1330 Value::Members
1331 Value::getMemberNames() const
1333 JSON_ASSERT( type_ == nullValue || type_ == objectValue );
1334 if ( type_ == nullValue )
1335 return Value::Members();
1336 Members 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() ) );
1343 #else
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 ) ) );
1350 #endif
1351 return members;
1354 //# ifdef JSON_USE_CPPTL
1355 //EnumMemberNames
1356 //Value::enumMemberNames() const
1358 // if ( type_ == objectValue )
1359 // {
1360 // return CppTL::Enum::any( CppTL::Enum::transform(
1361 // CppTL::Enum::keys( *(value_.map_), CppTL::Type<const CZString &>() ),
1362 // MemberNamesTransform() ) );
1363 // }
1364 // return EnumMemberNames();
1368 //EnumValues
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();
1377 //# endif
1379 static bool IsIntegral(double d) {
1380 double integral_part;
1381 return modf(d, &integral_part) == 0.0;
1385 bool
1386 Value::isNull() const
1388 return type_ == nullValue;
1392 bool
1393 Value::isBool() const
1395 return type_ == booleanValue;
1399 bool
1400 Value::isInt() const
1402 switch ( type_ )
1404 case intValue:
1405 return value_.int_ >= minInt && value_.int_ <= maxInt;
1406 case uintValue:
1407 return value_.uint_ <= UInt(maxInt);
1408 case realValue:
1409 return value_.real_ >= minInt &&
1410 value_.real_ <= maxInt &&
1411 IsIntegral(value_.real_);
1412 default:
1413 break;
1415 return false;
1419 bool
1420 Value::isUInt() const
1422 switch ( type_ )
1424 case intValue:
1425 return value_.int_ >= 0 && LargestUInt(value_.int_) <= LargestUInt(maxUInt);
1426 case uintValue:
1427 return value_.uint_ <= maxUInt;
1428 case realValue:
1429 return value_.real_ >= 0 &&
1430 value_.real_ <= maxUInt &&
1431 IsIntegral(value_.real_);
1432 default:
1433 break;
1435 return false;
1438 bool
1439 Value::isInt64() const
1441 # if defined(JSON_HAS_INT64)
1442 switch ( type_ )
1444 case intValue:
1445 return true;
1446 case uintValue:
1447 return value_.uint_ <= UInt64(maxInt64);
1448 case realValue:
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_);
1455 default:
1456 break;
1458 # endif // JSON_HAS_INT64
1459 return false;
1462 bool
1463 Value::isUInt64() const
1465 # if defined(JSON_HAS_INT64)
1466 switch ( type_ )
1468 case intValue:
1469 return value_.int_ >= 0;
1470 case uintValue:
1471 return true;
1472 case realValue:
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_);
1479 default:
1480 break;
1482 # endif // JSON_HAS_INT64
1483 return false;
1487 bool
1488 Value::isIntegral() const
1490 #if defined(JSON_HAS_INT64)
1491 return isInt64() || isUInt64();
1492 #else
1493 return isInt() || isUInt();
1494 #endif
1498 bool
1499 Value::isDouble() const
1501 return type_ == realValue || isIntegral();
1505 bool
1506 Value::isNumeric() const
1508 return isIntegral() || isDouble();
1512 bool
1513 Value::isString() const
1515 return type_ == stringValue;
1519 bool
1520 Value::isArray() const
1522 return type_ == arrayValue;
1526 bool
1527 Value::isObject() const
1529 return type_ == objectValue;
1533 void
1534 Value::setComment( const char *comment,
1535 CommentPlacement placement )
1537 if ( !comments_ )
1538 comments_ = new CommentInfo[numberOfCommentPlacement];
1539 comments_[placement].setComment( comment );
1543 void
1544 Value::setComment( const std::string &comment,
1545 CommentPlacement placement )
1547 setComment( comment.c_str(), placement );
1551 bool
1552 Value::hasComment( CommentPlacement placement ) const
1554 return comments_ != 0 && comments_[placement].comment_ != 0;
1557 std::string
1558 Value::getComment( CommentPlacement placement ) const
1560 if ( hasComment(placement) )
1561 return comments_[placement].comment_;
1562 return "";
1566 std::string
1567 Value::toStyledString() const
1569 StyledWriter writer;
1570 return writer.write( *this );
1574 Value::const_iterator
1575 Value::begin() const
1577 switch ( type_ )
1579 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1580 case arrayValue:
1581 if ( value_.array_ )
1583 ValueInternalArray::IteratorState it;
1584 value_.array_->makeBeginIterator( it );
1585 return const_iterator( it );
1587 break;
1588 case objectValue:
1589 if ( value_.map_ )
1591 ValueInternalMap::IteratorState it;
1592 value_.map_->makeBeginIterator( it );
1593 return const_iterator( it );
1595 break;
1596 #else
1597 case arrayValue:
1598 case objectValue:
1599 if ( value_.map_ )
1600 return const_iterator( value_.map_->begin() );
1601 break;
1602 #endif
1603 default:
1604 break;
1606 return const_iterator();
1609 Value::const_iterator
1610 Value::end() const
1612 switch ( type_ )
1614 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1615 case arrayValue:
1616 if ( value_.array_ )
1618 ValueInternalArray::IteratorState it;
1619 value_.array_->makeEndIterator( it );
1620 return const_iterator( it );
1622 break;
1623 case objectValue:
1624 if ( value_.map_ )
1626 ValueInternalMap::IteratorState it;
1627 value_.map_->makeEndIterator( it );
1628 return const_iterator( it );
1630 break;
1631 #else
1632 case arrayValue:
1633 case objectValue:
1634 if ( value_.map_ )
1635 return const_iterator( value_.map_->end() );
1636 break;
1637 #endif
1638 default:
1639 break;
1641 return const_iterator();
1645 Value::iterator
1646 Value::begin()
1648 switch ( type_ )
1650 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1651 case arrayValue:
1652 if ( value_.array_ )
1654 ValueInternalArray::IteratorState it;
1655 value_.array_->makeBeginIterator( it );
1656 return iterator( it );
1658 break;
1659 case objectValue:
1660 if ( value_.map_ )
1662 ValueInternalMap::IteratorState it;
1663 value_.map_->makeBeginIterator( it );
1664 return iterator( it );
1666 break;
1667 #else
1668 case arrayValue:
1669 case objectValue:
1670 if ( value_.map_ )
1671 return iterator( value_.map_->begin() );
1672 break;
1673 #endif
1674 default:
1675 break;
1677 return iterator();
1680 Value::iterator
1681 Value::end()
1683 switch ( type_ )
1685 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1686 case arrayValue:
1687 if ( value_.array_ )
1689 ValueInternalArray::IteratorState it;
1690 value_.array_->makeEndIterator( it );
1691 return iterator( it );
1693 break;
1694 case objectValue:
1695 if ( value_.map_ )
1697 ValueInternalMap::IteratorState it;
1698 value_.map_->makeEndIterator( it );
1699 return iterator( it );
1701 break;
1702 #else
1703 case arrayValue:
1704 case objectValue:
1705 if ( value_.map_ )
1706 return iterator( value_.map_->end() );
1707 break;
1708 #endif
1709 default:
1710 break;
1712 return iterator();
1716 // class PathArgument
1717 // //////////////////////////////////////////////////////////////////
1719 PathArgument::PathArgument()
1720 : key_()
1721 , index_()
1722 , kind_( kindNone )
1727 PathArgument::PathArgument( ArrayIndex index )
1728 : key_()
1729 , index_( index )
1730 , kind_( kindIndex )
1735 PathArgument::PathArgument( const char *key )
1736 : key_( key )
1737 , index_()
1738 , kind_( kindKey )
1743 PathArgument::PathArgument( const std::string &key )
1744 : key_( key.c_str() )
1745 , index_()
1746 , kind_( kindKey )
1750 // class Path
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 )
1760 InArgs in;
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 );
1770 void
1771 Path::makePath( const std::string &path,
1772 const InArgs &in )
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 == '[' )
1781 ++current;
1782 if ( *current == '%' )
1783 addPathInArg( path, in, itInArg, PathArgument::kindIndex );
1784 else
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 );
1797 ++current;
1799 else if ( *current == '.' )
1801 ++current;
1803 else
1805 const char *beginName = current;
1806 while ( current != end && !strchr( "[.", *current ) )
1807 ++current;
1808 args_.push_back( std::string( beginName, current ) );
1814 void
1815 Path::addPathInArg( const std::string &path,
1816 const InArgs &in,
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
1828 else
1830 args_.push_back( **itInArg );
1835 void
1836 Path::invalidPath( const std::string &path,
1837 int location )
1839 // Error: invalid path.
1843 const Value &
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...)
1871 return *node;
1875 Value
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;
1898 return *node;
1902 Value &
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_]);
1926 return *node;
1930 } // namespace Json