rippled
json_value.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/contract.h>
21 #include <ripple/json/impl/json_assert.h>
22 #include <ripple/json/to_string.h>
23 #include <ripple/json/json_writer.h>
24 #include <ripple/beast/core/LexicalCast.h>
25 
26 namespace Json {
27 
28 const Value Value::null;
29 const Int Value::minInt = Int ( ~ (UInt (-1) / 2) );
30 const Int Value::maxInt = Int ( UInt (-1) / 2 );
31 const UInt Value::maxUInt = UInt (-1);
32 
34 {
35 public:
36  virtual ~DefaultValueAllocator() = default;
37 
38  char* makeMemberName ( const char* memberName ) override
39  {
40  return duplicateStringValue ( memberName );
41  }
42 
43  void releaseMemberName ( char* memberName ) override
44  {
45  releaseStringValue ( memberName );
46  }
47 
48  char* duplicateStringValue ( const char* value,
49  unsigned int length = unknown ) override
50  {
51  //@todo investigate this old optimization
52  //if ( !value || value[0] == 0 )
53  // return 0;
54 
55  if ( length == unknown )
56  length = value ? (unsigned int)strlen ( value ) : 0;
57 
58  char* newString = static_cast<char*> ( malloc ( length + 1 ) );
59  if ( value )
60  memcpy ( newString, value, length );
61  newString[length] = 0;
62  return newString;
63  }
64 
65  void releaseStringValue ( char* value ) override
66  {
67  if ( value )
68  free ( value );
69  }
70 };
71 
73 {
75  return valueAllocator;
76 }
77 
79 {
81  {
82  valueAllocator (); // ensure valueAllocator() statics are initialized before main().
83  }
85 
86 // //////////////////////////////////////////////////////////////////
87 // //////////////////////////////////////////////////////////////////
88 // //////////////////////////////////////////////////////////////////
89 // class Value::CZString
90 // //////////////////////////////////////////////////////////////////
91 // //////////////////////////////////////////////////////////////////
92 // //////////////////////////////////////////////////////////////////
93 
94 // Notes: index_ indicates if the string was allocated when
95 // a string is stored.
96 
98  : cstr_ ( 0 )
99  , index_ ( index )
100 {
101 }
102 
103 Value::CZString::CZString ( const char* cstr, DuplicationPolicy allocate )
104  : cstr_ ( allocate == duplicate ? valueAllocator ()->makeMemberName (cstr)
105  : cstr )
106  , index_ ( allocate )
107 {
108 }
109 
111  : cstr_ ( other.index_ != noDuplication&& other.cstr_ != 0
112  ? valueAllocator ()->makeMemberName ( other.cstr_ )
113  : other.cstr_ )
114  , index_ ( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
115  : other.index_ )
116 {
117 }
118 
120 {
121  if ( cstr_ && index_ == duplicate )
122  valueAllocator ()->releaseMemberName ( const_cast<char*> ( cstr_ ) );
123 }
124 
125 bool
127 {
128  if ( cstr_ )
129  return strcmp ( cstr_, other.cstr_ ) < 0;
130 
131  return index_ < other.index_;
132 }
133 
134 bool
136 {
137  if ( cstr_ )
138  return strcmp ( cstr_, other.cstr_ ) == 0;
139 
140  return index_ == other.index_;
141 }
142 
143 
144 int
146 {
147  return index_;
148 }
149 
150 
151 const char*
153 {
154  return cstr_;
155 }
156 
157 bool
159 {
160  return index_ == noDuplication;
161 }
162 
163 // //////////////////////////////////////////////////////////////////
164 // //////////////////////////////////////////////////////////////////
165 // //////////////////////////////////////////////////////////////////
166 // class Value::Value
167 // //////////////////////////////////////////////////////////////////
168 // //////////////////////////////////////////////////////////////////
169 // //////////////////////////////////////////////////////////////////
170 
176  : type_ ( type )
177  , allocated_ ( 0 )
178 {
179  switch ( type )
180  {
181  case nullValue:
182  break;
183 
184  case intValue:
185  case uintValue:
186  value_.int_ = 0;
187  break;
188 
189  case realValue:
190  value_.real_ = 0.0;
191  break;
192 
193  case stringValue:
194  value_.string_ = 0;
195  break;
196 
197  case arrayValue:
198  case objectValue:
199  value_.map_ = new ObjectValues ();
200  break;
201 
202  case booleanValue:
203  value_.bool_ = false;
204  break;
205 
206  default:
207  JSON_ASSERT_UNREACHABLE;
208  }
209 }
210 
211 
212 Value::Value ( Int value )
213  : type_ ( intValue )
214 {
215  value_.int_ = value;
216 }
217 
218 
220  : type_ ( uintValue )
221 {
222  value_.uint_ = value;
223 }
224 
225 Value::Value ( double value )
226  : type_ ( realValue )
227 {
228  value_.real_ = value;
229 }
230 
231 Value::Value ( const char* value )
232  : type_ ( stringValue )
233  , allocated_ ( true )
234 {
236 }
237 
238 Value::Value ( std::string const& value )
239  : type_ ( stringValue )
240  , allocated_ ( true )
241 {
243  (unsigned int)value.length () );
244 }
245 
246 Value::Value ( const StaticString& value )
247  : type_ ( stringValue )
248  , allocated_ ( false )
249 {
250  value_.string_ = const_cast<char*> ( value.c_str () );
251 }
252 
253 Value::Value ( bool value )
254  : type_ ( booleanValue )
255 {
256  value_.bool_ = value;
257 }
258 
259 
260 Value::Value ( const Value& other )
261  : type_ ( other.type_ )
262 {
263  switch ( type_ )
264  {
265  case nullValue:
266  case intValue:
267  case uintValue:
268  case realValue:
269  case booleanValue:
270  value_ = other.value_;
271  break;
272 
273  case stringValue:
274  if ( other.value_.string_ )
275  {
277  allocated_ = true;
278  }
279  else
280  value_.string_ = 0;
281 
282  break;
283 
284  case arrayValue:
285  case objectValue:
286  value_.map_ = new ObjectValues ( *other.value_.map_ );
287  break;
288 
289  default:
290  JSON_ASSERT_UNREACHABLE;
291  }
292 }
293 
294 
296 {
297  switch ( type_ )
298  {
299  case nullValue:
300  case intValue:
301  case uintValue:
302  case realValue:
303  case booleanValue:
304  break;
305 
306  case stringValue:
307  if ( allocated_ )
309 
310  break;
311 
312  case arrayValue:
313  case objectValue:
314  if (value_.map_)
315  delete value_.map_;
316  break;
317 
318  default:
319  JSON_ASSERT_UNREACHABLE;
320  }
321 }
322 
323 Value&
324 Value::operator=(Value const& other)
325 {
326  Value tmp(other);
327  swap(tmp);
328  return *this;
329 }
330 
331 Value::Value ( Value&& other ) noexcept
332  : value_ ( other.value_ )
333  , type_ ( other.type_ )
334  , allocated_ ( other.allocated_ )
335 {
336  other.type_ = nullValue;
337  other.allocated_ = 0;
338 }
339 
340 Value&
342 {
343  Value tmp(std::move(other));
344  swap(tmp);
345  return *this;
346 }
347 
348 void
349 Value::swap ( Value& other ) noexcept
350 {
351  std::swap ( value_, other.value_ );
352 
353  ValueType temp = type_;
354  type_ = other.type_;
355  other.type_ = temp;
356 
357  int temp2 = allocated_;
358  allocated_ = other.allocated_;
359  other.allocated_ = temp2;
360 }
361 
362 ValueType
363 Value::type () const
364 {
365  return type_;
366 }
367 
368 static
369 int integerCmp (Int i, UInt ui)
370 {
371  // All negative numbers are less than all unsigned numbers.
372  if (i < 0)
373  return -1;
374 
375  // Now we can safely compare.
376  return (i < ui) ? -1 : (i == ui) ? 0 : 1;
377 }
378 
379 bool operator < (const Value& x, const Value& y)
380 {
381  if (auto signum = x.type_ - y.type_)
382  {
383  if (x.type_ == intValue && y.type_ == uintValue)
384  signum = integerCmp (x.value_.int_, y.value_.uint_);
385  else if (x.type_ == uintValue && y.type_ == intValue)
386  signum = - integerCmp (y.value_.int_, x.value_.uint_);
387  return signum < 0;
388  }
389 
390  switch (x.type_)
391  {
392  case nullValue:
393  return false;
394 
395  case intValue:
396  return x.value_.int_ < y.value_.int_;
397 
398  case uintValue:
399  return x.value_.uint_ < y.value_.uint_;
400 
401  case realValue:
402  return x.value_.real_ < y.value_.real_;
403 
404  case booleanValue:
405  return x.value_.bool_ < y.value_.bool_;
406 
407  case stringValue:
408  return (x.value_.string_ == 0 && y.value_.string_)
409  || (y.value_.string_ && x.value_.string_ &&
410  strcmp (x.value_.string_, y.value_.string_) < 0);
411 
412  case arrayValue:
413  case objectValue:
414  {
415  if (int signum = int (x.value_.map_->size ()) - y.value_.map_->size ())
416  return signum < 0;
417 
418  return *x.value_.map_ < *y.value_.map_;
419  }
420 
421  default:
422  JSON_ASSERT_UNREACHABLE;
423  }
424 
425  return 0; // unreachable
426 }
427 
428 bool operator== (const Value& x, const Value& y)
429 {
430  if (x.type_ != y.type_)
431  {
432  if (x.type_ == intValue && y.type_ == uintValue)
433  return ! integerCmp (x.value_.int_, y.value_.uint_);
434  if (x.type_ == uintValue && y.type_ == intValue)
435  return ! integerCmp (y.value_.int_, x.value_.uint_);
436  return false;
437  }
438 
439  switch (x.type_)
440  {
441  case nullValue:
442  return true;
443 
444  case intValue:
445  return x.value_.int_ == y.value_.int_;
446 
447  case uintValue:
448  return x.value_.uint_ == y.value_.uint_;
449 
450  case realValue:
451  return x.value_.real_ == y.value_.real_;
452 
453  case booleanValue:
454  return x.value_.bool_ == y.value_.bool_;
455 
456  case stringValue:
457  return x.value_.string_ == y.value_.string_
458  || (y.value_.string_ && x.value_.string_ &&
459  ! strcmp (x.value_.string_, y.value_.string_));
460 
461  case arrayValue:
462  case objectValue:
463  return x.value_.map_->size () == y.value_.map_->size ()
464  && *x.value_.map_ == *y.value_.map_;
465 
466  default:
467  JSON_ASSERT_UNREACHABLE;
468  }
469 
470  return 0; // unreachable
471 }
472 
473 const char*
475 {
476  JSON_ASSERT ( type_ == stringValue );
477  return value_.string_;
478 }
479 
480 
483 {
484  switch ( type_ )
485  {
486  case nullValue:
487  return "";
488 
489  case stringValue:
490  return value_.string_ ? value_.string_ : "";
491 
492  case booleanValue:
493  return value_.bool_ ? "true" : "false";
494 
495  case intValue:
496  return beast::lexicalCastThrow <std::string> (value_.int_);
497 
498  case uintValue:
499  return beast::lexicalCastThrow <std::string> (value_.uint_);
500 
501  case realValue:
502  return beast::lexicalCastThrow <std::string> (value_.real_);
503 
504  case arrayValue:
505  case objectValue:
506  JSON_ASSERT_MESSAGE ( false, "Type is not convertible to string" );
507 
508  default:
509  JSON_ASSERT_UNREACHABLE;
510  }
511 
512  return ""; // unreachable
513 }
514 
516 Value::asInt () const
517 {
518  switch ( type_ )
519  {
520  case nullValue:
521  return 0;
522 
523  case intValue:
524  return value_.int_;
525 
526  case uintValue:
527  JSON_ASSERT_MESSAGE ( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" );
528  return value_.uint_;
529 
530  case realValue:
531  JSON_ASSERT_MESSAGE ( value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range" );
532  return Int ( value_.real_ );
533 
534  case booleanValue:
535  return value_.bool_ ? 1 : 0;
536 
537  case stringValue:
538  {
539  char const* const str {value_.string_ ? value_.string_ : ""};
540  return beast::lexicalCastThrow <int> (str);
541  }
542 
543  case arrayValue:
544  case objectValue:
545  JSON_ASSERT_MESSAGE ( false, "Type is not convertible to int" );
546 
547  default:
548  JSON_ASSERT_UNREACHABLE;
549  }
550 
551  return 0; // unreachable;
552 }
553 
556 {
557  switch ( type_ )
558  {
559  case nullValue:
560  return 0;
561 
562  case intValue:
563  JSON_ASSERT_MESSAGE ( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" );
564  return value_.int_;
565 
566  case uintValue:
567  return value_.uint_;
568 
569  case realValue:
570  JSON_ASSERT_MESSAGE ( value_.real_ >= 0 && value_.real_ <= maxUInt, "Real out of unsigned integer range" );
571  return UInt ( value_.real_ );
572 
573  case booleanValue:
574  return value_.bool_ ? 1 : 0;
575 
576  case stringValue:
577  {
578  char const* const str {value_.string_ ? value_.string_ : ""};
579  return beast::lexicalCastThrow <unsigned int> (str);
580  }
581 
582  case arrayValue:
583  case objectValue:
584  JSON_ASSERT_MESSAGE ( false, "Type is not convertible to uint" );
585 
586  default:
587  JSON_ASSERT_UNREACHABLE;
588  }
589 
590  return 0; // unreachable;
591 }
592 
593 double
595 {
596  switch ( type_ )
597  {
598  case nullValue:
599  return 0.0;
600 
601  case intValue:
602  return value_.int_;
603 
604  case uintValue:
605  return value_.uint_;
606 
607  case realValue:
608  return value_.real_;
609 
610  case booleanValue:
611  return value_.bool_ ? 1.0 : 0.0;
612 
613  case stringValue:
614  case arrayValue:
615  case objectValue:
616  JSON_ASSERT_MESSAGE ( false, "Type is not convertible to double" );
617 
618  default:
619  JSON_ASSERT_UNREACHABLE;
620  }
621 
622  return 0; // unreachable;
623 }
624 
625 bool
627 {
628  switch ( type_ )
629  {
630  case nullValue:
631  return false;
632 
633  case intValue:
634  case uintValue:
635  return value_.int_ != 0;
636 
637  case realValue:
638  return value_.real_ != 0.0;
639 
640  case booleanValue:
641  return value_.bool_;
642 
643  case stringValue:
644  return value_.string_ && value_.string_[0] != 0;
645 
646  case arrayValue:
647  case objectValue:
648  return value_.map_->size () != 0;
649 
650  default:
651  JSON_ASSERT_UNREACHABLE;
652  }
653 
654  return false; // unreachable;
655 }
656 
657 
658 bool
660 {
661  switch ( type_ )
662  {
663  case nullValue:
664  return true;
665 
666  case intValue:
667  return ( other == nullValue && value_.int_ == 0 )
668  || other == intValue
669  || ( other == uintValue && value_.int_ >= 0 )
670  || other == realValue
671  || other == stringValue
672  || other == booleanValue;
673 
674  case uintValue:
675  return ( other == nullValue && value_.uint_ == 0 )
676  || ( other == intValue && value_.uint_ <= (unsigned)maxInt )
677  || other == uintValue
678  || other == realValue
679  || other == stringValue
680  || other == booleanValue;
681 
682  case realValue:
683  return ( other == nullValue && value_.real_ == 0.0 )
684  || ( other == intValue && value_.real_ >= minInt && value_.real_ <= maxInt )
685  || ( other == uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt )
686  || other == realValue
687  || other == stringValue
688  || other == booleanValue;
689 
690  case booleanValue:
691  return ( other == nullValue && value_.bool_ == false )
692  || other == intValue
693  || other == uintValue
694  || other == realValue
695  || other == stringValue
696  || other == booleanValue;
697 
698  case stringValue:
699  return other == stringValue
700  || ( other == nullValue && (!value_.string_ || value_.string_[0] == 0) );
701 
702  case arrayValue:
703  return other == arrayValue
704  || ( other == nullValue && value_.map_->size () == 0 );
705 
706  case objectValue:
707  return other == objectValue
708  || ( other == nullValue && value_.map_->size () == 0 );
709 
710  default:
711  JSON_ASSERT_UNREACHABLE;
712  }
713 
714  return false; // unreachable;
715 }
716 
717 
720 Value::size () const
721 {
722  switch ( type_ )
723  {
724  case nullValue:
725  case intValue:
726  case uintValue:
727  case realValue:
728  case booleanValue:
729  case stringValue:
730  return 0;
731 
732  case arrayValue: // size of the array is highest index + 1
733  if ( !value_.map_->empty () )
734  {
735  ObjectValues::const_iterator itLast = value_.map_->end ();
736  --itLast;
737  return (*itLast).first.index () + 1;
738  }
739 
740  return 0;
741 
742  case objectValue:
743  return Int ( value_.map_->size () );
744 
745  default:
746  JSON_ASSERT_UNREACHABLE;
747  }
748 
749  return 0; // unreachable;
750 }
751 
752 
753 Value::operator bool () const
754 {
755  if (isNull ())
756  return false;
757 
758  if (isString ())
759  {
760  auto s = asCString();
761  return s && s[0];
762  }
763 
764  return ! (isArray() || isObject()) || size ();
765 }
766 
767 void
769 {
770  JSON_ASSERT ( type_ == nullValue || type_ == arrayValue || type_ == objectValue );
771 
772  switch ( type_ )
773  {
774  case arrayValue:
775  case objectValue:
776  value_.map_->clear ();
777  break;
778 
779  default:
780  break;
781  }
782 }
783 
784 Value&
786 {
787  JSON_ASSERT ( type_ == nullValue || type_ == arrayValue );
788 
789  if ( type_ == nullValue )
790  *this = Value ( arrayValue );
791 
792  CZString key ( index );
793  ObjectValues::iterator it = value_.map_->lower_bound ( key );
794 
795  if ( it != value_.map_->end () && (*it).first == key )
796  return (*it).second;
797 
798  ObjectValues::value_type defaultValue ( key, null );
799  it = value_.map_->insert ( it, defaultValue );
800  return (*it).second;
801 }
802 
803 
804 const Value&
805 Value::operator[] ( UInt index ) const
806 {
807  JSON_ASSERT ( type_ == nullValue || type_ == arrayValue );
808 
809  if ( type_ == nullValue )
810  return null;
811 
812  CZString key ( index );
813  ObjectValues::const_iterator it = value_.map_->find ( key );
814 
815  if ( it == value_.map_->end () )
816  return null;
817 
818  return (*it).second;
819 }
820 
821 
822 Value&
823 Value::operator[] ( const char* key )
824 {
825  return resolveReference ( key, false );
826 }
827 
828 
829 Value&
830 Value::resolveReference ( const char* key,
831  bool isStatic )
832 {
833  JSON_ASSERT ( type_ == nullValue || type_ == objectValue );
834 
835  if ( type_ == nullValue )
836  *this = Value ( objectValue );
837 
838  CZString actualKey ( key, isStatic ? CZString::noDuplication
840  ObjectValues::iterator it = value_.map_->lower_bound ( actualKey );
841 
842  if ( it != value_.map_->end () && (*it).first == actualKey )
843  return (*it).second;
844 
845  ObjectValues::value_type defaultValue ( actualKey, null );
846  it = value_.map_->insert ( it, defaultValue );
847  Value& value = (*it).second;
848  return value;
849 }
850 
851 
852 Value
853 Value::get ( UInt index,
854  const Value& defaultValue ) const
855 {
856  const Value* value = & ((*this)[index]);
857  return value == &null ? defaultValue : *value;
858 }
859 
860 
861 bool
862 Value::isValidIndex ( UInt index ) const
863 {
864  return index < size ();
865 }
866 
867 
868 
869 const Value&
870 Value::operator[] ( const char* key ) const
871 {
872  JSON_ASSERT ( type_ == nullValue || type_ == objectValue );
873 
874  if ( type_ == nullValue )
875  return null;
876 
877  CZString actualKey ( key, CZString::noDuplication );
878  ObjectValues::const_iterator it = value_.map_->find ( actualKey );
879 
880  if ( it == value_.map_->end () )
881  return null;
882 
883  return (*it).second;
884 }
885 
886 
887 Value&
889 {
890  return (*this)[ key.c_str () ];
891 }
892 
893 
894 const Value&
895 Value::operator[] ( std::string const& key ) const
896 {
897  return (*this)[ key.c_str () ];
898 }
899 
900 Value&
902 {
903  return resolveReference ( key, true );
904 }
905 
906 Value&
907 Value::append ( const Value& value )
908 {
909  return (*this)[size ()] = value;
910 }
911 
912 Value&
913 Value::append ( Value&& value )
914 {
915  return (*this)[size ()] = std::move(value);
916 }
917 
918 
919 Value
920 Value::get ( const char* key,
921  const Value& defaultValue ) const
922 {
923  const Value* value = & ((*this)[key]);
924  return value == &null ? defaultValue : *value;
925 }
926 
927 
928 Value
929 Value::get ( std::string const& key,
930  const Value& defaultValue ) const
931 {
932  return get ( key.c_str (), defaultValue );
933 }
934 
935 Value
936 Value::removeMember ( const char* key )
937 {
938  JSON_ASSERT ( type_ == nullValue || type_ == objectValue );
939 
940  if ( type_ == nullValue )
941  return null;
942 
943  CZString actualKey ( key, CZString::noDuplication );
944  ObjectValues::iterator it = value_.map_->find ( actualKey );
945 
946  if ( it == value_.map_->end () )
947  return null;
948 
949  Value old (it->second);
950  value_.map_->erase (it);
951  return old;
952 }
953 
954 Value
956 {
957  return removeMember ( key.c_str () );
958 }
959 
960 bool
961 Value::isMember ( const char* key ) const
962 {
963  if (type_ != objectValue)
964  return false;
965 
966  const Value* value = & ((*this)[key]);
967  return value != &null;
968 }
969 
970 
971 bool
972 Value::isMember ( std::string const& key ) const
973 {
974  return isMember ( key.c_str () );
975 }
976 
977 
980 {
981  JSON_ASSERT ( type_ == nullValue || type_ == objectValue );
982 
983  if ( type_ == nullValue )
984  return Value::Members ();
985 
986  Members members;
987  members.reserve ( value_.map_->size () );
988  ObjectValues::const_iterator it = value_.map_->begin ();
989  ObjectValues::const_iterator itEnd = value_.map_->end ();
990 
991  for ( ; it != itEnd; ++it )
992  members.push_back ( std::string ( (*it).first.c_str () ) );
993 
994  return members;
995 }
996 
997 bool
999 {
1000  return type_ == nullValue;
1001 }
1002 
1003 
1004 bool
1006 {
1007  return type_ == booleanValue;
1008 }
1009 
1010 
1011 bool
1013 {
1014  return type_ == intValue;
1015 }
1016 
1017 
1018 bool
1020 {
1021  return type_ == uintValue;
1022 }
1023 
1024 
1025 bool
1027 {
1028  return type_ == intValue
1029  || type_ == uintValue
1030  || type_ == booleanValue;
1031 }
1032 
1033 
1034 bool
1036 {
1037  return type_ == realValue;
1038 }
1039 
1040 
1041 bool
1043 {
1044  return isIntegral () || isDouble ();
1045 }
1046 
1047 
1048 bool
1050 {
1051  return type_ == stringValue;
1052 }
1053 
1054 
1055 bool
1057 {
1058  return type_ == arrayValue;
1059 }
1060 
1061 bool
1063 {
1064  return type_ == nullValue || type_ == arrayValue;
1065 }
1066 
1067 
1068 bool
1070 {
1071  return type_ == objectValue;
1072 }
1073 
1074 bool
1076 {
1077  return type_ == nullValue || type_ == objectValue;
1078 }
1079 
1082 {
1083  StyledWriter writer;
1084  return writer.write ( *this );
1085 }
1086 
1087 
1090 {
1091  switch ( type_ )
1092  {
1093  case arrayValue:
1094  case objectValue:
1095  if ( value_.map_ )
1096  return const_iterator ( value_.map_->begin () );
1097 
1098  break;
1099  default:
1100  break;
1101  }
1102 
1103  return const_iterator ();
1104 }
1105 
1107 Value::end () const
1108 {
1109  switch ( type_ )
1110  {
1111  case arrayValue:
1112  case objectValue:
1113  if ( value_.map_ )
1114  return const_iterator ( value_.map_->end () );
1115 
1116  break;
1117  default:
1118  break;
1119  }
1120 
1121  return const_iterator ();
1122 }
1123 
1124 
1126 Value::begin ()
1127 {
1128  switch ( type_ )
1129  {
1130  case arrayValue:
1131  case objectValue:
1132  if ( value_.map_ )
1133  return iterator ( value_.map_->begin () );
1134  break;
1135  default:
1136  break;
1137  }
1138 
1139  return iterator ();
1140 }
1141 
1143 Value::end ()
1144 {
1145  switch ( type_ )
1146  {
1147  case arrayValue:
1148  case objectValue:
1149  if ( value_.map_ )
1150  return iterator ( value_.map_->end () );
1151  break;
1152  default:
1153  break;
1154  }
1155 
1156  return iterator ();
1157 }
1158 
1159 } // namespace Json
Json::Value::isInt
bool isInt() const
Definition: json_value.cpp:1012
Json::Value::Int
Json::Int Int
Definition: json_value.h:150
Json::ValueIterator
Iterator for object and array value.
Definition: json_value.h:530
Json::operator<
bool operator<(const Value &x, const Value &y)
Definition: json_value.cpp:379
Json::DummyValueAllocatorInitializer
Definition: json_value.cpp:78
Json::Value::isObject
bool isObject() const
Definition: json_value.cpp:1069
std::string
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1049
Json::Value::isValidIndex
bool isValidIndex(UInt index) const
Return true if index < size().
Definition: json_value.cpp:862
Json::DefaultValueAllocator
Definition: json_value.cpp:33
Json::Value::allocated_
int allocated_
Definition: json_value.h:370
Json::Value::type_
ValueType type_
Definition: json_value.h:369
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
Json::Value::CZString::index
int index() const
Definition: json_value.cpp:145
Json::booleanValue
@ booleanValue
bool value
Definition: json_value.h:43
std::vector::reserve
T reserve(T... args)
Json::DefaultValueAllocator::releaseMemberName
void releaseMemberName(char *memberName) override
Definition: json_value.cpp:43
Json::UInt
unsigned int UInt
Definition: json_forwards.h:28
Json::Value::get
Value get(UInt index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
Definition: json_value.cpp:853
std::vector< std::string >
std::map::find
T find(T... args)
std::string::length
T length(T... args)
Json::Value::swap
void swap(Value &other) noexcept
Swap values.
Definition: json_value.cpp:349
Json::Value::isDouble
bool isDouble() const
Definition: json_value.cpp:1035
Json::realValue
@ realValue
double value
Definition: json_value.h:41
Json::Value::iterator
ValueIterator iterator
Definition: json_value.h:147
Json::Value::CZString::c_str
const char * c_str() const
Definition: json_value.cpp:152
Json::Value::isNull
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:998
Json::Value::toStyledString
std::string toStyledString() const
Definition: json_value.cpp:1081
Json::Value::end
const_iterator end() const
Definition: json_value.cpp:1107
Json::StyledWriter::write
std::string write(const Value &root) override
Serialize a Value in JSON format.
Definition: json_writer.cpp:277
Json::StaticString::c_str
constexpr const char * c_str() const
Definition: json_value.h:75
Json::integerCmp
static int integerCmp(Int i, UInt ui)
Definition: json_value.cpp:369
std::map::clear
T clear(T... args)
Json::ValueAllocator
Experimental do not use: Allocator to customize member name and string value memory management done b...
Definition: json_value.h:408
Json::Value::asBool
bool asBool() const
Definition: json_value.cpp:626
std::vector::push_back
T push_back(T... args)
Json::Value::ValueHolder::map_
ObjectValues * map_
Definition: json_value.h:367
Json::uintValue
@ uintValue
unsigned integer value
Definition: json_value.h:40
Json::Value::maxInt
static const Int maxInt
Definition: json_value.h:155
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:26
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
Json::Value::resolveReference
Value & resolveReference(const char *key, bool isStatic)
Definition: json_value.cpp:830
Json::DefaultValueAllocator::releaseStringValue
void releaseStringValue(char *value) override
Definition: json_value.cpp:65
Json::Value::CZString::cstr_
const char * cstr_
Definition: json_value.h:179
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
Json::DummyValueAllocatorInitializer::DummyValueAllocatorInitializer
DummyValueAllocatorInitializer()
Definition: json_value.cpp:80
Json::dummyValueAllocatorInitializer
static struct Json::DummyValueAllocatorInitializer dummyValueAllocatorInitializer
Json::StyledWriter
Writes a Value in JSON format in a human friendly way.
Definition: json_writer.h:80
Json::Value::value_
union Json::Value::ValueHolder value_
Json::Value::isConvertibleTo
bool isConvertibleTo(ValueType other) const
Definition: json_value.cpp:659
Json::Value::isArrayOrNull
bool isArrayOrNull() const
Definition: json_value.cpp:1062
Json::ValueConstIterator
const iterator for object and array value.
Definition: json_value.h:477
std::string::c_str
T c_str(T... args)
Json::Value::ObjectValues
std::map< CZString, Value > ObjectValues
Definition: json_value.h:184
Json::Value::ValueHolder::real_
double real_
Definition: json_value.h:364
Json::operator==
bool operator==(const Value &x, const Value &y)
Definition: json_value.cpp:428
Json::ValueAllocator::releaseStringValue
virtual void releaseStringValue(char *value)=0
Json::Value::size
UInt size() const
Number of values in array or object.
Definition: json_value.cpp:720
std::map::erase
T erase(T... args)
Json::Value::minInt
static const Int minInt
Definition: json_value.h:154
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:961
Json::stringValue
@ stringValue
UTF-8 string value.
Definition: json_value.h:42
Json::ValueType
ValueType
Type of the value held by a Value object.
Definition: json_value.h:36
Json::Value::asCString
const char * asCString() const
Definition: json_value.cpp:474
Json::DefaultValueAllocator::makeMemberName
char * makeMemberName(const char *memberName) override
Definition: json_value.cpp:38
Json::valueAllocator
static ValueAllocator *& valueAllocator()
Definition: json_value.cpp:72
Json::Int
int Int
Definition: json_forwards.h:27
Json::Value::CZString::operator<
bool operator<(const CZString &other) const
Definition: json_value.cpp:126
Json::Value::UInt
Json::UInt UInt
Definition: json_value.h:149
Json::Value::CZString::CZString
CZString(int index)
Definition: json_value.cpp:97
Json::DefaultValueAllocator::~DefaultValueAllocator
virtual ~DefaultValueAllocator()=default
std::swap
T swap(T... args)
Json::Value::isArray
bool isArray() const
Definition: json_value.cpp:1056
Json::Value::CZString::operator==
bool operator==(const CZString &other) const
Definition: json_value.cpp:135
Json::ValueAllocator::duplicateStringValue
virtual char * duplicateStringValue(const char *value, unsigned int length=unknown)=0
Json::Value::getMemberNames
Members getMemberNames() const
Return a list of the member names.
Definition: json_value.cpp:979
Json::Value::CZString::~CZString
~CZString()
Definition: json_value.cpp:119
Json::Value::operator=
Value & operator=(Value const &other)
Definition: json_value.cpp:324
std::map::lower_bound
T lower_bound(T... args)
Json::ValueAllocator::unknown
@ unknown
Definition: json_value.h:411
Json::Value::maxUInt
static const UInt maxUInt
Definition: json_value.h:156
Json::Value::removeMember
Value removeMember(const char *key)
Remove and return the named member.
Definition: json_value.cpp:936
Json::Value::isNumeric
bool isNumeric() const
Definition: json_value.cpp:1042
Json::ValueAllocator::releaseMemberName
virtual void releaseMemberName(char *memberName)=0
Json::Value::CZString::DuplicationPolicy
DuplicationPolicy
Definition: json_value.h:162
Json::Value::clear
void clear()
Remove all object members and array elements.
Definition: json_value.cpp:768
Json::Value::CZString
Definition: json_value.h:159
Json::Value::CZString::noDuplication
@ noDuplication
Definition: json_value.h:164
Json::Value::CZString::index_
int index_
Definition: json_value.h:180
std::map::begin
T begin(T... args)
Json::Value::null
static const Value null
Definition: json_value.h:153
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:62
Json::intValue
@ intValue
signed integer value
Definition: json_value.h:39
std::map::insert
T insert(T... args)
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:555
Json::Value::isUInt
bool isUInt() const
Definition: json_value.cpp:1019
Json::Value::isIntegral
bool isIntegral() const
Definition: json_value.cpp:1026
Json::Value::CZString::duplicateOnCopy
@ duplicateOnCopy
Definition: json_value.h:166
Json::nullValue
@ nullValue
'null' value
Definition: json_value.h:38
Json::Value::operator[]
Value & operator[](UInt index)
Access an array element (zero based index ).
Definition: json_value.cpp:785
Json::Value::ValueHolder::bool_
bool bool_
Definition: json_value.h:365
Json::Value::ValueHolder::string_
char * string_
Definition: json_value.h:366
std::map::empty
T empty(T... args)
Json::Value::Value
Value(ValueType type=nullValue)
Create a default Value of the given type.
Definition: json_value.cpp:175
Json::Value::isBool
bool isBool() const
Definition: json_value.cpp:1005
Json::Value::~Value
~Value()
Definition: json_value.cpp:295
Json::Value::asInt
Int asInt() const
Definition: json_value.cpp:516
std::map::end
T end(T... args)
Json::Value::Members
std::vector< std::string > Members
Definition: json_value.h:146
Json::DefaultValueAllocator::duplicateStringValue
char * duplicateStringValue(const char *value, unsigned int length=unknown) override
Definition: json_value.cpp:48
Json::Value::begin
const_iterator begin() const
Definition: json_value.cpp:1089
Json::Value::asDouble
double asDouble() const
Definition: json_value.cpp:594
Json::Value::type
ValueType type() const
Definition: json_value.cpp:363
Json::Value::CZString::isStaticString
bool isStaticString() const
Definition: json_value.cpp:158
Json::Value::ValueHolder::uint_
UInt uint_
Definition: json_value.h:363
Json::Value::isObjectOrNull
bool isObjectOrNull() const
Definition: json_value.cpp:1075
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::Value::ValueHolder::int_
Int int_
Definition: json_value.h:362
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482
Json::Value::const_iterator
ValueConstIterator const_iterator
Definition: json_value.h:148