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