]> git.donarmstrong.com Git - bamtools.git/blob - src/third_party/jsoncpp/json_value.cpp
Minor version bump - 2.3.0
[bamtools.git] / src / third_party / jsoncpp / json_value.cpp
1 // Copyright 2007-2010 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
5
6 #include <iostream>
7 #include "json_value.h"
8 #include "json_writer.h"
9 #include <utility>
10 #include <stdexcept>
11 #include <cstring>
12 #include <cassert>
13 #ifdef JSON_USE_CPPTL
14 # include <cpptl/conststring.h>
15 #endif
16 #include <cstddef>    // size_t
17 #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
18 # include "json_batchallocator.h"
19 #endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
20
21 #define JSON_ASSERT_UNREACHABLE assert( false )
22 #define JSON_ASSERT( condition ) assert( condition );  // @todo <= change this into an exception throw
23 #define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) throw std::runtime_error( message );
24
25 namespace Json {
26
27 const Value Value::null;
28 const Int Value::minInt = Int( ~(UInt(-1)/2) );
29 const Int Value::maxInt = Int( UInt(-1)/2 );
30 const UInt Value::maxUInt = UInt(-1);
31
32 /// Unknown size marker
33 enum { unknown = (unsigned)-1 };
34
35
36 /** Duplicates the specified string value.
37  * @param value Pointer to the string to duplicate. Must be zero-terminated if
38  *              length is "unknown".
39  * @param length Length of the value. if equals to unknown, then it will be
40  *               computed using strlen(value).
41  * @return Pointer on the duplicate instance of string.
42  */
43 static inline char *
44 duplicateStringValue( const char *value, 
45                       unsigned int length = unknown )
46 {
47    if ( length == unknown )
48       length = (unsigned int)strlen(value);
49    char *newString = static_cast<char *>( malloc( length + 1 ) );
50    memcpy( newString, value, length );
51    newString[length] = 0;
52    return newString;
53 }
54
55
56 /** Free the string duplicated by duplicateStringValue().
57  */
58 static inline void 
59 releaseStringValue( char *value )
60 {
61    if ( value )
62       free( value );
63 }
64
65
66
67 // //////////////////////////////////////////////////////////////////
68 // //////////////////////////////////////////////////////////////////
69 // //////////////////////////////////////////////////////////////////
70 // ValueInternals...
71 // //////////////////////////////////////////////////////////////////
72 // //////////////////////////////////////////////////////////////////
73 // //////////////////////////////////////////////////////////////////
74 #ifdef JSON_VALUE_USE_INTERNAL_MAP
75 # include "json_internalarray.inl"
76 # include "json_internalmap.inl"
77 #endif // JSON_VALUE_USE_INTERNAL_MAP
78
79 # include "json_valueiterator.inl"
80
81
82 // //////////////////////////////////////////////////////////////////
83 // //////////////////////////////////////////////////////////////////
84 // //////////////////////////////////////////////////////////////////
85 // class Value::CommentInfo
86 // //////////////////////////////////////////////////////////////////
87 // //////////////////////////////////////////////////////////////////
88 // //////////////////////////////////////////////////////////////////
89
90
91 Value::CommentInfo::CommentInfo()
92    : comment_( 0 )
93 {
94 }
95
96 Value::CommentInfo::~CommentInfo()
97 {
98    if ( comment_ )
99       releaseStringValue( comment_ );
100 }
101
102
103 void 
104 Value::CommentInfo::setComment( const char *text )
105 {
106    if ( comment_ )
107       releaseStringValue( comment_ );
108    JSON_ASSERT( text );
109    JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "Comments must start with /");
110    // It seems that /**/ style comments are acceptable as well.
111    comment_ = duplicateStringValue( text );
112 }
113
114
115 // //////////////////////////////////////////////////////////////////
116 // //////////////////////////////////////////////////////////////////
117 // //////////////////////////////////////////////////////////////////
118 // class Value::CZString
119 // //////////////////////////////////////////////////////////////////
120 // //////////////////////////////////////////////////////////////////
121 // //////////////////////////////////////////////////////////////////
122 # ifndef JSON_VALUE_USE_INTERNAL_MAP
123
124 // Notes: index_ indicates if the string was allocated when
125 // a string is stored.
126
127 Value::CZString::CZString( ArrayIndex index )
128    : cstr_( 0 )
129    , index_( index )
130 {
131 }
132
133 Value::CZString::CZString( const char *cstr, DuplicationPolicy allocate )
134    : cstr_( allocate == duplicate ? duplicateStringValue(cstr) 
135                                   : cstr )
136    , index_( allocate )
137 {
138 }
139
140 Value::CZString::CZString( const CZString &other )
141 : cstr_( other.index_ != noDuplication &&  other.cstr_ != 0
142                 ?  duplicateStringValue( other.cstr_ )
143                 : other.cstr_ )
144    , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
145                          : other.index_ )
146 {
147 }
148
149 Value::CZString::~CZString()
150 {
151    if ( cstr_  &&  index_ == duplicate )
152       releaseStringValue( const_cast<char *>( cstr_ ) );
153 }
154
155 void 
156 Value::CZString::swap( CZString &other )
157 {
158    std::swap( cstr_, other.cstr_ );
159    std::swap( index_, other.index_ );
160 }
161
162 Value::CZString &
163 Value::CZString::operator =( const CZString &other )
164 {
165    CZString temp( other );
166    swap( temp );
167    return *this;
168 }
169
170 bool 
171 Value::CZString::operator<( const CZString &other ) const 
172 {
173    if ( cstr_ )
174       return strcmp( cstr_, other.cstr_ ) < 0;
175    return index_ < other.index_;
176 }
177
178 bool 
179 Value::CZString::operator==( const CZString &other ) const 
180 {
181    if ( cstr_ )
182       return strcmp( cstr_, other.cstr_ ) == 0;
183    return index_ == other.index_;
184 }
185
186
187 ArrayIndex 
188 Value::CZString::index() const
189 {
190    return index_;
191 }
192
193
194 const char *
195 Value::CZString::c_str() const
196 {
197    return cstr_;
198 }
199
200 bool 
201 Value::CZString::isStaticString() const
202 {
203    return index_ == noDuplication;
204 }
205
206 #endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
207
208
209 // //////////////////////////////////////////////////////////////////
210 // //////////////////////////////////////////////////////////////////
211 // //////////////////////////////////////////////////////////////////
212 // class Value::Value
213 // //////////////////////////////////////////////////////////////////
214 // //////////////////////////////////////////////////////////////////
215 // //////////////////////////////////////////////////////////////////
216
217 /*! \internal Default constructor initialization must be equivalent to:
218  * memset( this, 0, sizeof(Value) )
219  * This optimization is used in ValueInternalMap fast allocator.
220  */
221 Value::Value( ValueType type )
222    : type_( type )
223    , allocated_( 0 )
224    , comments_( 0 )
225 # ifdef JSON_VALUE_USE_INTERNAL_MAP
226    , itemIsUsed_( 0 )
227 #endif
228 {
229    switch ( type )
230    {
231    case nullValue:
232       break;
233    case intValue:
234    case uintValue:
235       value_.int_ = 0;
236       break;
237    case realValue:
238       value_.real_ = 0.0;
239       break;
240    case stringValue:
241       value_.string_ = 0;
242       break;
243 #ifndef JSON_VALUE_USE_INTERNAL_MAP
244    case arrayValue:
245    case objectValue:
246       value_.map_ = new ObjectValues();
247       break;
248 #else
249    case arrayValue:
250       value_.array_ = arrayAllocator()->newArray();
251       break;
252    case objectValue:
253       value_.map_ = mapAllocator()->newMap();
254       break;
255 #endif
256    case booleanValue:
257       value_.bool_ = false;
258       break;
259    default:
260       JSON_ASSERT_UNREACHABLE;
261    }
262 }
263
264
265 #if !defined(JSON_NO_INT64)
266 Value::Value( ArrayIndex value )
267    : type_( uintValue )
268    , comments_( 0 )
269 # ifdef JSON_VALUE_USE_INTERNAL_MAP
270    , itemIsUsed_( 0 )
271 #endif
272 {
273    value_.uint_ = value;
274 }
275
276 Value::Value( int value )
277    : type_( intValue )
278    , comments_( 0 )
279 # ifdef JSON_VALUE_USE_INTERNAL_MAP
280    , itemIsUsed_( 0 )
281 #endif
282 {
283    value_.int_ = value;
284 }
285
286 #endif // if !defined(JSON_NO_INT64)
287
288
289 Value::Value( Int value )
290    : type_( intValue )
291    , comments_( 0 )
292 # ifdef JSON_VALUE_USE_INTERNAL_MAP
293    , itemIsUsed_( 0 )
294 #endif
295 {
296    value_.int_ = value;
297 }
298
299
300 Value::Value( UInt value )
301    : type_( uintValue )
302    , comments_( 0 )
303 # ifdef JSON_VALUE_USE_INTERNAL_MAP
304    , itemIsUsed_( 0 )
305 #endif
306 {
307    value_.uint_ = value;
308 }
309
310 Value::Value( double value )
311    : type_( realValue )
312    , comments_( 0 )
313 # ifdef JSON_VALUE_USE_INTERNAL_MAP
314    , itemIsUsed_( 0 )
315 #endif
316 {
317    value_.real_ = value;
318 }
319
320 Value::Value( const char *value )
321    : type_( stringValue )
322    , allocated_( true )
323    , comments_( 0 )
324 # ifdef JSON_VALUE_USE_INTERNAL_MAP
325    , itemIsUsed_( 0 )
326 #endif
327 {
328    value_.string_ = duplicateStringValue( value );
329 }
330
331
332 Value::Value( const char *beginValue, 
333               const char *endValue )
334    : type_( stringValue )
335    , allocated_( true )
336    , comments_( 0 )
337 # ifdef JSON_VALUE_USE_INTERNAL_MAP
338    , itemIsUsed_( 0 )
339 #endif
340 {
341    value_.string_ = duplicateStringValue( beginValue, 
342                                           (unsigned int)(endValue - beginValue) );
343 }
344
345
346 Value::Value( const std::string &value )
347    : type_( stringValue )
348    , allocated_( true )
349    , comments_( 0 )
350 # ifdef JSON_VALUE_USE_INTERNAL_MAP
351    , itemIsUsed_( 0 )
352 #endif
353 {
354    value_.string_ = duplicateStringValue( value.c_str(), 
355                                           (unsigned int)value.length() );
356
357 }
358
359 Value::Value( const StaticString &value )
360    : type_( stringValue )
361    , allocated_( false )
362    , comments_( 0 )
363 # ifdef JSON_VALUE_USE_INTERNAL_MAP
364    , itemIsUsed_( 0 )
365 #endif
366 {
367    value_.string_ = const_cast<char *>( value.c_str() );
368 }
369
370
371 # ifdef JSON_USE_CPPTL
372 Value::Value( const CppTL::ConstString &value )
373    : type_( stringValue )
374    , allocated_( true )
375    , comments_( 0 )
376 # ifdef JSON_VALUE_USE_INTERNAL_MAP
377    , itemIsUsed_( 0 )
378 #endif
379 {
380    value_.string_ = duplicateStringValue( value, value.length() );
381 }
382 # endif
383
384 Value::Value( bool value )
385    : type_( booleanValue )
386    , comments_( 0 )
387 # ifdef JSON_VALUE_USE_INTERNAL_MAP
388    , itemIsUsed_( 0 )
389 #endif
390 {
391    value_.bool_ = value;
392 }
393
394
395 Value::Value( const Value &other )
396    : type_( other.type_ )
397    , comments_( 0 )
398 # ifdef JSON_VALUE_USE_INTERNAL_MAP
399    , itemIsUsed_( 0 )
400 #endif
401 {
402    switch ( type_ )
403    {
404    case nullValue:
405    case intValue:
406    case uintValue:
407    case realValue:
408    case booleanValue:
409       value_ = other.value_;
410       break;
411    case stringValue:
412       if ( other.value_.string_ )
413       {
414          value_.string_ = duplicateStringValue( other.value_.string_ );
415          allocated_ = true;
416       }
417       else
418          value_.string_ = 0;
419       break;
420 #ifndef JSON_VALUE_USE_INTERNAL_MAP
421    case arrayValue:
422    case objectValue:
423       value_.map_ = new ObjectValues( *other.value_.map_ );
424       break;
425 #else
426    case arrayValue:
427       value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ );
428       break;
429    case objectValue:
430       value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ );
431       break;
432 #endif
433    default:
434       JSON_ASSERT_UNREACHABLE;
435    }
436    if ( other.comments_ )
437    {
438       comments_ = new CommentInfo[numberOfCommentPlacement];
439       for ( int comment =0; comment < numberOfCommentPlacement; ++comment )
440       {
441          const CommentInfo &otherComment = other.comments_[comment];
442          if ( otherComment.comment_ )
443             comments_[comment].setComment( otherComment.comment_ );
444       }
445    }
446 }
447
448
449 Value::~Value()
450 {
451    switch ( type_ )
452    {
453    case nullValue:
454    case intValue:
455    case uintValue:
456    case realValue:
457    case booleanValue:
458       break;
459    case stringValue:
460       if ( allocated_ )
461          releaseStringValue( value_.string_ );
462       break;
463 #ifndef JSON_VALUE_USE_INTERNAL_MAP
464    case arrayValue:
465    case objectValue:
466       delete value_.map_;
467       break;
468 #else
469    case arrayValue:
470       arrayAllocator()->destructArray( value_.array_ );
471       break;
472    case objectValue:
473       mapAllocator()->destructMap( value_.map_ );
474       break;
475 #endif
476    default:
477       JSON_ASSERT_UNREACHABLE;
478    }
479
480    if ( comments_ )
481       delete[] comments_;
482 }
483
484 Value &
485 Value::operator=( const Value &other )
486 {
487    Value temp( other );
488    swap( temp );
489    return *this;
490 }
491
492 void 
493 Value::swap( Value &other )
494 {
495    ValueType temp = type_;
496    type_ = other.type_;
497    other.type_ = temp;
498    std::swap( value_, other.value_ );
499    int temp2 = allocated_;
500    allocated_ = other.allocated_;
501    other.allocated_ = temp2;
502 }
503
504 ValueType 
505 Value::type() const
506 {
507    return type_;
508 }
509
510
511 int 
512 Value::compare( const Value &other )
513 {
514    /*
515    int typeDelta = other.type_ - type_;
516    switch ( type_ )
517    {
518    case nullValue:
519
520       return other.type_ == type_;
521    case intValue:
522       if ( other.type_.isNumeric()
523    case uintValue:
524    case realValue:
525    case booleanValue:
526       break;
527    case stringValue,
528       break;
529    case arrayValue:
530       delete value_.array_;
531       break;
532    case objectValue:
533       delete value_.map_;
534    default:
535       JSON_ASSERT_UNREACHABLE;
536    }
537    */
538    return 0;  // unreachable
539 }
540
541 bool 
542 Value::operator <( const Value &other ) const
543 {
544    int typeDelta = type_ - other.type_;
545    if ( typeDelta )
546       return typeDelta < 0 ? true : false;
547    switch ( type_ )
548    {
549    case nullValue:
550       return false;
551    case intValue:
552       return value_.int_ < other.value_.int_;
553    case uintValue:
554       return value_.uint_ < other.value_.uint_;
555    case realValue:
556       return value_.real_ < other.value_.real_;
557    case booleanValue:
558       return value_.bool_ < other.value_.bool_;
559    case stringValue:
560       return ( value_.string_ == 0  &&  other.value_.string_ )
561              || ( other.value_.string_  
562                   &&  value_.string_  
563                   && strcmp( value_.string_, other.value_.string_ ) < 0 );
564 #ifndef JSON_VALUE_USE_INTERNAL_MAP
565    case arrayValue:
566    case objectValue:
567       {
568          int delta = int( value_.map_->size() - other.value_.map_->size() );
569          if ( delta )
570             return delta < 0;
571          return (*value_.map_) < (*other.value_.map_);
572       }
573 #else
574    case arrayValue:
575       return value_.array_->compare( *(other.value_.array_) ) < 0;
576    case objectValue:
577       return value_.map_->compare( *(other.value_.map_) ) < 0;
578 #endif
579    default:
580       JSON_ASSERT_UNREACHABLE;
581    }
582    return 0;  // unreachable
583 }
584
585 bool 
586 Value::operator <=( const Value &other ) const
587 {
588    return !(other > *this);
589 }
590
591 bool 
592 Value::operator >=( const Value &other ) const
593 {
594    return !(*this < other);
595 }
596
597 bool 
598 Value::operator >( const Value &other ) const
599 {
600    return other < *this;
601 }
602
603 bool 
604 Value::operator ==( const Value &other ) const
605 {
606    //if ( type_ != other.type_ )
607    // GCC 2.95.3 says:
608    // attempt to take address of bit-field structure member `Json::Value::type_'
609    // Beats me, but a temp solves the problem.
610    int temp = other.type_;
611    if ( type_ != temp )
612       return false;
613    switch ( type_ )
614    {
615    case nullValue:
616       return true;
617    case intValue:
618       return value_.int_ == other.value_.int_;
619    case uintValue:
620       return value_.uint_ == other.value_.uint_;
621    case realValue:
622       return value_.real_ == other.value_.real_;
623    case booleanValue:
624       return value_.bool_ == other.value_.bool_;
625    case stringValue:
626       return ( value_.string_ == other.value_.string_ )
627              || ( other.value_.string_  
628                   &&  value_.string_  
629                   && strcmp( value_.string_, other.value_.string_ ) == 0 );
630 #ifndef JSON_VALUE_USE_INTERNAL_MAP
631    case arrayValue:
632    case objectValue:
633       return value_.map_->size() == other.value_.map_->size()
634              && (*value_.map_) == (*other.value_.map_);
635 #else
636    case arrayValue:
637       return value_.array_->compare( *(other.value_.array_) ) == 0;
638    case objectValue:
639       return value_.map_->compare( *(other.value_.map_) ) == 0;
640 #endif
641    default:
642       JSON_ASSERT_UNREACHABLE;
643    }
644    return 0;  // unreachable
645 }
646
647 bool 
648 Value::operator !=( const Value &other ) const
649 {
650    return !( *this == other );
651 }
652
653 const char *
654 Value::asCString() const
655 {
656    JSON_ASSERT( type_ == stringValue );
657    return value_.string_;
658 }
659
660
661 std::string 
662 Value::asString() const
663 {
664    switch ( type_ )
665    {
666    case nullValue:
667       return "";
668    case stringValue:
669       return value_.string_ ? value_.string_ : "";
670    case booleanValue:
671       return value_.bool_ ? "true" : "false";
672    case intValue:
673    case uintValue:
674    case realValue:
675    case arrayValue:
676    case objectValue:
677       JSON_ASSERT_MESSAGE( false, "Type is not convertible to string" );
678    default:
679       JSON_ASSERT_UNREACHABLE;
680    }
681    return ""; // unreachable
682 }
683
684 # ifdef JSON_USE_CPPTL
685 CppTL::ConstString 
686 Value::asConstString() const
687 {
688    return CppTL::ConstString( asString().c_str() );
689 }
690 # endif
691
692 Value::Int 
693 Value::asInt() const
694 {
695    switch ( type_ )
696    {
697    case nullValue:
698       return 0;
699    case intValue:
700       return value_.int_;
701    case uintValue:
702       JSON_ASSERT_MESSAGE( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" );
703       return value_.uint_;
704    case realValue:
705       JSON_ASSERT_MESSAGE( value_.real_ >= minInt  &&  value_.real_ <= maxInt, "Real out of signed integer range" );
706       return Int( value_.real_ );
707    case booleanValue:
708       return value_.bool_ ? 1 : 0;
709    case stringValue:
710    case arrayValue:
711    case objectValue:
712       JSON_ASSERT_MESSAGE( false, "Type is not convertible to int" );
713    default:
714       JSON_ASSERT_UNREACHABLE;
715    }
716    return 0; // unreachable;
717 }
718
719 Value::UInt 
720 Value::asUInt() const
721 {
722    switch ( type_ )
723    {
724    case nullValue:
725       return 0;
726    case intValue:
727       JSON_ASSERT_MESSAGE( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" );
728       return value_.int_;
729    case uintValue:
730       return value_.uint_;
731    case realValue:
732       JSON_ASSERT_MESSAGE( value_.real_ >= 0  &&  value_.real_ <= maxUInt,  "Real out of unsigned integer range" );
733       return UInt( value_.real_ );
734    case booleanValue:
735       return value_.bool_ ? 1 : 0;
736    case stringValue:
737    case arrayValue:
738    case objectValue:
739       JSON_ASSERT_MESSAGE( false, "Type is not convertible to uint" );
740    default:
741       JSON_ASSERT_UNREACHABLE;
742    }
743    return 0; // unreachable;
744 }
745
746 double 
747 Value::asDouble() const
748 {
749    switch ( type_ )
750    {
751    case nullValue:
752       return 0.0;
753    case intValue:
754       return static_cast<double>( value_.int_ );
755    case uintValue:
756 #if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
757       return static_cast<double>( value_.uint_ );
758 #else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
759       return static_cast<double>( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1);
760 #endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
761    case realValue:
762       return value_.real_;
763    case booleanValue:
764       return value_.bool_ ? 1.0 : 0.0;
765    case stringValue:
766    case arrayValue:
767    case objectValue:
768       JSON_ASSERT_MESSAGE( false, "Type is not convertible to double" );
769    default:
770       JSON_ASSERT_UNREACHABLE;
771    }
772    return 0; // unreachable;
773 }
774
775 bool 
776 Value::asBool() const
777 {
778    switch ( type_ )
779    {
780    case nullValue:
781       return false;
782    case intValue:
783    case uintValue:
784       return value_.int_ != 0;
785    case realValue:
786       return value_.real_ != 0.0;
787    case booleanValue:
788       return value_.bool_;
789    case stringValue:
790       return value_.string_  &&  value_.string_[0] != 0;
791    case arrayValue:
792    case objectValue:
793       return value_.map_->size() != 0;
794    default:
795       JSON_ASSERT_UNREACHABLE;
796    }
797    return false; // unreachable;
798 }
799
800
801 bool 
802 Value::isConvertibleTo( ValueType other ) const
803 {
804    switch ( type_ )
805    {
806    case nullValue:
807       return true;
808    case intValue:
809       return ( other == nullValue  &&  value_.int_ == 0 )
810              || other == intValue
811              || ( other == uintValue  && value_.int_ >= 0 )
812              || other == realValue
813              || other == stringValue
814              || other == booleanValue;
815    case uintValue:
816       return ( other == nullValue  &&  value_.uint_ == 0 )
817              || ( other == intValue  && value_.uint_ <= (unsigned)maxInt )
818              || other == uintValue
819              || other == realValue
820              || other == stringValue
821              || other == booleanValue;
822    case realValue:
823       return ( other == nullValue  &&  value_.real_ == 0.0 )
824              || ( other == intValue  &&  value_.real_ >= minInt  &&  value_.real_ <= maxInt )
825              || ( other == uintValue  &&  value_.real_ >= 0  &&  value_.real_ <= maxUInt )
826              || other == realValue
827              || other == stringValue
828              || other == booleanValue;
829    case booleanValue:
830       return ( other == nullValue  &&  value_.bool_ == false )
831              || other == intValue
832              || other == uintValue
833              || other == realValue
834              || other == stringValue
835              || other == booleanValue;
836    case stringValue:
837       return other == stringValue
838              || ( other == nullValue  &&  (!value_.string_  ||  value_.string_[0] == 0) );
839    case arrayValue:
840       return other == arrayValue
841              ||  ( other == nullValue  &&  value_.map_->size() == 0 );
842    case objectValue:
843       return other == objectValue
844              ||  ( other == nullValue  &&  value_.map_->size() == 0 );
845    default:
846       JSON_ASSERT_UNREACHABLE;
847    }
848    return false; // unreachable;
849 }
850
851
852 /// Number of values in array or object
853 ArrayIndex 
854 Value::size() const
855 {
856    switch ( type_ )
857    {
858    case nullValue:
859    case intValue:
860    case uintValue:
861    case realValue:
862    case booleanValue:
863    case stringValue:
864       return 0;
865 #ifndef JSON_VALUE_USE_INTERNAL_MAP
866    case arrayValue:  // size of the array is highest index + 1
867       if ( !value_.map_->empty() )
868       {
869          ObjectValues::const_iterator itLast = value_.map_->end();
870          --itLast;
871          return (*itLast).first.index()+1;
872       }
873       return 0;
874    case objectValue:
875       return ArrayIndex( value_.map_->size() );
876 #else
877    case arrayValue:
878       return Int( value_.array_->size() );
879    case objectValue:
880       return Int( value_.map_->size() );
881 #endif
882    default:
883       JSON_ASSERT_UNREACHABLE;
884    }
885    return 0; // unreachable;
886 }
887
888
889 bool 
890 Value::empty() const
891 {
892    if ( isNull() || isArray() || isObject() )
893       return size() == 0u;
894    else
895       return false;
896 }
897
898
899 bool
900 Value::operator!() const
901 {
902    return isNull();
903 }
904
905
906 void 
907 Value::clear()
908 {
909    JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue  || type_ == objectValue );
910
911    switch ( type_ )
912    {
913 #ifndef JSON_VALUE_USE_INTERNAL_MAP
914    case arrayValue:
915    case objectValue:
916       value_.map_->clear();
917       break;
918 #else
919    case arrayValue:
920       value_.array_->clear();
921       break;
922    case objectValue:
923       value_.map_->clear();
924       break;
925 #endif
926    default:
927       break;
928    }
929 }
930
931 void 
932 Value::resize( ArrayIndex newSize )
933 {
934    JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue );
935    if ( type_ == nullValue )
936       *this = Value( arrayValue );
937 #ifndef JSON_VALUE_USE_INTERNAL_MAP
938    ArrayIndex oldSize = size();
939    if ( newSize == 0 )
940       clear();
941    else if ( newSize > oldSize )
942       (*this)[ newSize - 1 ];
943    else
944    {
945       for ( ArrayIndex index = newSize; index < oldSize; ++index )
946       {
947          value_.map_->erase( index );
948       }
949       assert( size() == newSize );
950    }
951 #else
952    value_.array_->resize( newSize );
953 #endif
954 }
955
956
957 Value &
958 Value::operator[]( ArrayIndex index )
959 {
960    JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue );
961    if ( type_ == nullValue )
962       *this = Value( arrayValue );
963 #ifndef JSON_VALUE_USE_INTERNAL_MAP
964    CZString key( index );
965    ObjectValues::iterator it = value_.map_->lower_bound( key );
966    if ( it != value_.map_->end()  &&  (*it).first == key )
967       return (*it).second;
968
969    ObjectValues::value_type defaultValue( key, null );
970    it = value_.map_->insert( it, defaultValue );
971    return (*it).second;
972 #else
973    return value_.array_->resolveReference( index );
974 #endif
975 }
976
977
978 const Value &
979 Value::operator[]( ArrayIndex index ) const
980 {
981    JSON_ASSERT( type_ == nullValue  ||  type_ == arrayValue );
982    if ( type_ == nullValue )
983       return null;
984 #ifndef JSON_VALUE_USE_INTERNAL_MAP
985    CZString key( index );
986    ObjectValues::const_iterator it = value_.map_->find( key );
987    if ( it == value_.map_->end() )
988       return null;
989    return (*it).second;
990 #else
991    Value *value = value_.array_->find( index );
992    return value ? *value : null;
993 #endif
994 }
995
996
997 Value &
998 Value::operator[]( const char *key )
999 {
1000    return resolveReference( key, false );
1001 }
1002
1003
1004 Value &
1005 Value::resolveReference( const char *key, 
1006                          bool isStatic )
1007 {
1008    JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
1009    if ( type_ == nullValue )
1010       *this = Value( objectValue );
1011 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1012    CZString actualKey( key, isStatic ? CZString::noDuplication 
1013                                      : CZString::duplicateOnCopy );
1014    ObjectValues::iterator it = value_.map_->lower_bound( actualKey );
1015    if ( it != value_.map_->end()  &&  (*it).first == actualKey )
1016       return (*it).second;
1017
1018    ObjectValues::value_type defaultValue( actualKey, null );
1019    it = value_.map_->insert( it, defaultValue );
1020    Value &value = (*it).second;
1021    return value;
1022 #else
1023    return value_.map_->resolveReference( key, isStatic );
1024 #endif
1025 }
1026
1027
1028 Value 
1029 Value::get( ArrayIndex index, 
1030             const Value &defaultValue ) const
1031 {
1032    const Value *value = &((*this)[index]);
1033    return value == &null ? defaultValue : *value;
1034 }
1035
1036
1037 bool 
1038 Value::isValidIndex( ArrayIndex index ) const
1039 {
1040    return index < size();
1041 }
1042
1043
1044
1045 const Value &
1046 Value::operator[]( const char *key ) const
1047 {
1048    JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
1049    if ( type_ == nullValue )
1050       return null;
1051 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1052    CZString actualKey( key, CZString::noDuplication );
1053    ObjectValues::const_iterator it = value_.map_->find( actualKey );
1054    if ( it == value_.map_->end() )
1055       return null;
1056    return (*it).second;
1057 #else
1058    const Value *value = value_.map_->find( key );
1059    return value ? *value : null;
1060 #endif
1061 }
1062
1063
1064 Value &
1065 Value::operator[]( const std::string &key )
1066 {
1067    return (*this)[ key.c_str() ];
1068 }
1069
1070
1071 const Value &
1072 Value::operator[]( const std::string &key ) const
1073 {
1074    return (*this)[ key.c_str() ];
1075 }
1076
1077 Value &
1078 Value::operator[]( const StaticString &key )
1079 {
1080    return resolveReference( key, true );
1081 }
1082
1083
1084 # ifdef JSON_USE_CPPTL
1085 Value &
1086 Value::operator[]( const CppTL::ConstString &key )
1087 {
1088    return (*this)[ key.c_str() ];
1089 }
1090
1091
1092 const Value &
1093 Value::operator[]( const CppTL::ConstString &key ) const
1094 {
1095    return (*this)[ key.c_str() ];
1096 }
1097 # endif
1098
1099
1100 Value &
1101 Value::append( const Value &value )
1102 {
1103    return (*this)[size()] = value;
1104 }
1105
1106
1107 Value 
1108 Value::get( const char *key, 
1109             const Value &defaultValue ) const
1110 {
1111    const Value *value = &((*this)[key]);
1112    return value == &null ? defaultValue : *value;
1113 }
1114
1115
1116 Value 
1117 Value::get( const std::string &key,
1118             const Value &defaultValue ) const
1119 {
1120    return get( key.c_str(), defaultValue );
1121 }
1122
1123 Value
1124 Value::removeMember( const char* key )
1125 {
1126    JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
1127    if ( type_ == nullValue )
1128       return null;
1129 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1130    CZString actualKey( key, CZString::noDuplication );
1131    ObjectValues::iterator it = value_.map_->find( actualKey );
1132    if ( it == value_.map_->end() )
1133       return null;
1134    Value old(it->second);
1135    value_.map_->erase(it);
1136    return old;
1137 #else
1138    Value *value = value_.map_->find( key );
1139    if (value){
1140       Value old(*value);
1141       value_.map_.remove( key );
1142       return old;
1143    } else {
1144       return null;
1145    }
1146 #endif
1147 }
1148
1149 Value
1150 Value::removeMember( const std::string &key )
1151 {
1152    return removeMember( key.c_str() );
1153 }
1154
1155 # ifdef JSON_USE_CPPTL
1156 Value 
1157 Value::get( const CppTL::ConstString &key,
1158             const Value &defaultValue ) const
1159 {
1160    return get( key.c_str(), defaultValue );
1161 }
1162 # endif
1163
1164 bool 
1165 Value::isMember( const char *key ) const
1166 {
1167    const Value *value = &((*this)[key]);
1168    return value != &null;
1169 }
1170
1171
1172 bool 
1173 Value::isMember( const std::string &key ) const
1174 {
1175    return isMember( key.c_str() );
1176 }
1177
1178
1179 # ifdef JSON_USE_CPPTL
1180 bool 
1181 Value::isMember( const CppTL::ConstString &key ) const
1182 {
1183    return isMember( key.c_str() );
1184 }
1185 #endif
1186
1187 Value::Members 
1188 Value::getMemberNames() const
1189 {
1190    JSON_ASSERT( type_ == nullValue  ||  type_ == objectValue );
1191    if ( type_ == nullValue )
1192        return Value::Members();
1193    Members members;
1194    members.reserve( value_.map_->size() );
1195 #ifndef JSON_VALUE_USE_INTERNAL_MAP
1196    ObjectValues::const_iterator it = value_.map_->begin();
1197    ObjectValues::const_iterator itEnd = value_.map_->end();
1198    for ( ; it != itEnd; ++it )
1199       members.push_back( std::string( (*it).first.c_str() ) );
1200 #else
1201    ValueInternalMap::IteratorState it;
1202    ValueInternalMap::IteratorState itEnd;
1203    value_.map_->makeBeginIterator( it );
1204    value_.map_->makeEndIterator( itEnd );
1205    for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) )
1206       members.push_back( std::string( ValueInternalMap::key( it ) ) );
1207 #endif
1208    return members;
1209 }
1210 //
1211 //# ifdef JSON_USE_CPPTL
1212 //EnumMemberNames
1213 //Value::enumMemberNames() const
1214 //{
1215 //   if ( type_ == objectValue )
1216 //   {
1217 //      return CppTL::Enum::any(  CppTL::Enum::transform(
1218 //         CppTL::Enum::keys( *(value_.map_), CppTL::Type<const CZString &>() ),
1219 //         MemberNamesTransform() ) );
1220 //   }
1221 //   return EnumMemberNames();
1222 //}
1223 //
1224 //
1225 //EnumValues 
1226 //Value::enumValues() const
1227 //{
1228 //   if ( type_ == objectValue  ||  type_ == arrayValue )
1229 //      return CppTL::Enum::anyValues( *(value_.map_), 
1230 //                                     CppTL::Type<const Value &>() );
1231 //   return EnumValues();
1232 //}
1233 //
1234 //# endif
1235
1236
1237 bool
1238 Value::isNull() const
1239 {
1240    return type_ == nullValue;
1241 }
1242
1243
1244 bool 
1245 Value::isBool() const
1246 {
1247    return type_ == booleanValue;
1248 }
1249
1250
1251 bool 
1252 Value::isInt() const
1253 {
1254    return type_ == intValue;
1255 }
1256
1257
1258 bool 
1259 Value::isUInt() const
1260 {
1261    return type_ == uintValue;
1262 }
1263
1264
1265 bool 
1266 Value::isIntegral() const
1267 {
1268    return type_ == intValue  
1269           ||  type_ == uintValue  
1270           ||  type_ == booleanValue;
1271 }
1272
1273
1274 bool 
1275 Value::isDouble() const
1276 {
1277    return type_ == realValue;
1278 }
1279
1280
1281 bool 
1282 Value::isNumeric() const
1283 {
1284    return isIntegral() || isDouble();
1285 }
1286
1287
1288 bool 
1289 Value::isString() const
1290 {
1291    return type_ == stringValue;
1292 }
1293
1294
1295 bool 
1296 Value::isArray() const
1297 {
1298    return type_ == nullValue  ||  type_ == arrayValue;
1299 }
1300
1301
1302 bool 
1303 Value::isObject() const
1304 {
1305    return type_ == nullValue  ||  type_ == objectValue;
1306 }
1307
1308
1309 void 
1310 Value::setComment( const char *comment,
1311                    CommentPlacement placement )
1312 {
1313    if ( !comments_ )
1314       comments_ = new CommentInfo[numberOfCommentPlacement];
1315    comments_[placement].setComment( comment );
1316 }
1317
1318
1319 void 
1320 Value::setComment( const std::string &comment,
1321                    CommentPlacement placement )
1322 {
1323    setComment( comment.c_str(), placement );
1324 }
1325
1326
1327 bool 
1328 Value::hasComment( CommentPlacement placement ) const
1329 {
1330    return comments_ != 0  &&  comments_[placement].comment_ != 0;
1331 }
1332
1333 std::string 
1334 Value::getComment( CommentPlacement placement ) const
1335 {
1336    if ( hasComment(placement) )
1337       return comments_[placement].comment_;
1338    return "";
1339 }
1340
1341
1342 std::string 
1343 Value::toStyledString() const
1344 {
1345    StyledWriter writer;
1346    return writer.write( *this );
1347 }
1348
1349
1350 Value::const_iterator 
1351 Value::begin() const
1352 {
1353    switch ( type_ )
1354    {
1355 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1356    case arrayValue:
1357       if ( value_.array_ )
1358       {
1359          ValueInternalArray::IteratorState it;
1360          value_.array_->makeBeginIterator( it );
1361          return const_iterator( it );
1362       }
1363       break;
1364    case objectValue:
1365       if ( value_.map_ )
1366       {
1367          ValueInternalMap::IteratorState it;
1368          value_.map_->makeBeginIterator( it );
1369          return const_iterator( it );
1370       }
1371       break;
1372 #else
1373    case arrayValue:
1374    case objectValue:
1375       if ( value_.map_ )
1376          return const_iterator( value_.map_->begin() );
1377       break;
1378 #endif
1379    default:
1380       break;
1381    }
1382    return const_iterator();
1383 }
1384
1385 Value::const_iterator 
1386 Value::end() const
1387 {
1388    switch ( type_ )
1389    {
1390 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1391    case arrayValue:
1392       if ( value_.array_ )
1393       {
1394          ValueInternalArray::IteratorState it;
1395          value_.array_->makeEndIterator( it );
1396          return const_iterator( it );
1397       }
1398       break;
1399    case objectValue:
1400       if ( value_.map_ )
1401       {
1402          ValueInternalMap::IteratorState it;
1403          value_.map_->makeEndIterator( it );
1404          return const_iterator( it );
1405       }
1406       break;
1407 #else
1408    case arrayValue:
1409    case objectValue:
1410       if ( value_.map_ )
1411          return const_iterator( value_.map_->end() );
1412       break;
1413 #endif
1414    default:
1415       break;
1416    }
1417    return const_iterator();
1418 }
1419
1420
1421 Value::iterator 
1422 Value::begin()
1423 {
1424    switch ( type_ )
1425    {
1426 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1427    case arrayValue:
1428       if ( value_.array_ )
1429       {
1430          ValueInternalArray::IteratorState it;
1431          value_.array_->makeBeginIterator( it );
1432          return iterator( it );
1433       }
1434       break;
1435    case objectValue:
1436       if ( value_.map_ )
1437       {
1438          ValueInternalMap::IteratorState it;
1439          value_.map_->makeBeginIterator( it );
1440          return iterator( it );
1441       }
1442       break;
1443 #else
1444    case arrayValue:
1445    case objectValue:
1446       if ( value_.map_ )
1447          return iterator( value_.map_->begin() );
1448       break;
1449 #endif
1450    default:
1451       break;
1452    }
1453    return iterator();
1454 }
1455
1456 Value::iterator 
1457 Value::end()
1458 {
1459    switch ( type_ )
1460    {
1461 #ifdef JSON_VALUE_USE_INTERNAL_MAP
1462    case arrayValue:
1463       if ( value_.array_ )
1464       {
1465          ValueInternalArray::IteratorState it;
1466          value_.array_->makeEndIterator( it );
1467          return iterator( it );
1468       }
1469       break;
1470    case objectValue:
1471       if ( value_.map_ )
1472       {
1473          ValueInternalMap::IteratorState it;
1474          value_.map_->makeEndIterator( it );
1475          return iterator( it );
1476       }
1477       break;
1478 #else
1479    case arrayValue:
1480    case objectValue:
1481       if ( value_.map_ )
1482          return iterator( value_.map_->end() );
1483       break;
1484 #endif
1485    default:
1486       break;
1487    }
1488    return iterator();
1489 }
1490
1491
1492 // class PathArgument
1493 // //////////////////////////////////////////////////////////////////
1494
1495 PathArgument::PathArgument()
1496    : kind_( kindNone )
1497 {
1498 }
1499
1500
1501 PathArgument::PathArgument( ArrayIndex index )
1502    : index_( index )
1503    , kind_( kindIndex )
1504 {
1505 }
1506
1507
1508 PathArgument::PathArgument( const char *key )
1509    : key_( key )
1510    , kind_( kindKey )
1511 {
1512 }
1513
1514
1515 PathArgument::PathArgument( const std::string &key )
1516    : key_( key.c_str() )
1517    , kind_( kindKey )
1518 {
1519 }
1520
1521 // class Path
1522 // //////////////////////////////////////////////////////////////////
1523
1524 Path::Path( const std::string &path,
1525             const PathArgument &a1,
1526             const PathArgument &a2,
1527             const PathArgument &a3,
1528             const PathArgument &a4,
1529             const PathArgument &a5 )
1530 {
1531    InArgs in;
1532    in.push_back( &a1 );
1533    in.push_back( &a2 );
1534    in.push_back( &a3 );
1535    in.push_back( &a4 );
1536    in.push_back( &a5 );
1537    makePath( path, in );
1538 }
1539
1540
1541 void 
1542 Path::makePath( const std::string &path,
1543                 const InArgs &in )
1544 {
1545    const char *current = path.c_str();
1546    const char *end = current + path.length();
1547    InArgs::const_iterator itInArg = in.begin();
1548    while ( current != end )
1549    {
1550       if ( *current == '[' )
1551       {
1552          ++current;
1553          if ( *current == '%' )
1554             addPathInArg( path, in, itInArg, PathArgument::kindIndex );
1555          else
1556          {
1557             ArrayIndex index = 0;
1558             for ( ; current != end && *current >= '0'  &&  *current <= '9'; ++current )
1559                index = index * 10 + ArrayIndex(*current - '0');
1560             args_.push_back( index );
1561          }
1562          if ( current == end  ||  *current++ != ']' )
1563             invalidPath( path, int(current - path.c_str()) );
1564       }
1565       else if ( *current == '%' )
1566       {
1567          addPathInArg( path, in, itInArg, PathArgument::kindKey );
1568          ++current;
1569       }
1570       else if ( *current == '.' )
1571       {
1572          ++current;
1573       }
1574       else
1575       {
1576          const char *beginName = current;
1577          while ( current != end  &&  !strchr( "[.", *current ) )
1578             ++current;
1579          args_.push_back( std::string( beginName, current ) );
1580       }
1581    }
1582 }
1583
1584
1585 void 
1586 Path::addPathInArg( const std::string &path, 
1587                     const InArgs &in, 
1588                     InArgs::const_iterator &itInArg, 
1589                     PathArgument::Kind kind )
1590 {
1591    if ( itInArg == in.end() )
1592    {
1593       // Error: missing argument %d
1594    }
1595    else if ( (*itInArg)->kind_ != kind )
1596    {
1597       // Error: bad argument type
1598    }
1599    else
1600    {
1601       args_.push_back( **itInArg );
1602    }
1603 }
1604
1605
1606 void 
1607 Path::invalidPath( const std::string &path, 
1608                    int location )
1609 {
1610    // Error: invalid path.
1611 }
1612
1613
1614 const Value &
1615 Path::resolve( const Value &root ) const
1616 {
1617    const Value *node = &root;
1618    for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1619    {
1620       const PathArgument &arg = *it;
1621       if ( arg.kind_ == PathArgument::kindIndex )
1622       {
1623          if ( !node->isArray()  ||  node->isValidIndex( arg.index_ ) )
1624          {
1625             // Error: unable to resolve path (array value expected at position...
1626          }
1627          node = &((*node)[arg.index_]);
1628       }
1629       else if ( arg.kind_ == PathArgument::kindKey )
1630       {
1631          if ( !node->isObject() )
1632          {
1633             // Error: unable to resolve path (object value expected at position...)
1634          }
1635          node = &((*node)[arg.key_]);
1636          if ( node == &Value::null )
1637          {
1638             // Error: unable to resolve path (object has no member named '' at position...)
1639          }
1640       }
1641    }
1642    return *node;
1643 }
1644
1645
1646 Value 
1647 Path::resolve( const Value &root, 
1648                const Value &defaultValue ) const
1649 {
1650    const Value *node = &root;
1651    for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1652    {
1653       const PathArgument &arg = *it;
1654       if ( arg.kind_ == PathArgument::kindIndex )
1655       {
1656          if ( !node->isArray()  ||  node->isValidIndex( arg.index_ ) )
1657             return defaultValue;
1658          node = &((*node)[arg.index_]);
1659       }
1660       else if ( arg.kind_ == PathArgument::kindKey )
1661       {
1662          if ( !node->isObject() )
1663             return defaultValue;
1664          node = &((*node)[arg.key_]);
1665          if ( node == &Value::null )
1666             return defaultValue;
1667       }
1668    }
1669    return *node;
1670 }
1671
1672
1673 Value &
1674 Path::make( Value &root ) const
1675 {
1676    Value *node = &root;
1677    for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it )
1678    {
1679       const PathArgument &arg = *it;
1680       if ( arg.kind_ == PathArgument::kindIndex )
1681       {
1682          if ( !node->isArray() )
1683          {
1684             // Error: node is not an array at position ...
1685          }
1686          node = &((*node)[arg.index_]);
1687       }
1688       else if ( arg.kind_ == PathArgument::kindKey )
1689       {
1690          if ( !node->isObject() )
1691          {
1692             // Error: node is not an object at position...
1693          }
1694          node = &((*node)[arg.key_]);
1695       }
1696    }
1697    return *node;
1698 }
1699
1700
1701 } // namespace Json