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