JsonCpp project page JsonCpp home page

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