JsonCpp project page JsonCpp home page

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

SourceForge Logo hosts this site. Send comments to:
Json-cpp Developers