All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
document.h
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_DOCUMENT_H_
22 #define RAPIDJSON_DOCUMENT_H_
23 
24 #include "reader.h"
25 #include "internal/strfunc.h"
26 #include <new> // placement new
27 
28 #ifdef _MSC_VER
29 RAPIDJSON_DIAG_PUSH
30 RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
31 #elif defined(__GNUC__)
32 RAPIDJSON_DIAG_PUSH
33 RAPIDJSON_DIAG_OFF(effc++)
34 #endif
35 
36 #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
37 #include "internal/meta.h"
38 #include <iterator> // std::iterator, std::random_access_iterator_tag
39 #endif
40 
41 namespace rapidjson {
42 
43 // Forward declaration.
44 template <typename Encoding, typename Allocator>
46 
47 //! Name-value pair in a JSON object value.
48 /*!
49  This class was internal to GenericValue. It used to be a inner struct.
50  But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct.
51  https://code.google.com/p/rapidjson/issues/detail?id=64
52 */
53 template <typename Encoding, typename Allocator>
54 struct GenericMember {
55  GenericValue<Encoding, Allocator> name; //!< name of member (must be a string)
57 };
58 
59 #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
60 
61 //! (Constant) member iterator for a JSON object value
62 /*!
63  \tparam Const Is this a constant iterator?
64  \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
65  \tparam Allocator Allocator type for allocating memory of object, array and string.
66 
67  This class implements a Random Access Iterator for GenericMember elements
68  of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements].
69 
70  \note This iterator implementation is mainly intended to avoid implicit
71  conversions from iterator values to \c NULL,
72  e.g. from GenericValue::FindMember.
73 
74  \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a
75  pointer-based implementation, if your platform doesn't provide
76  the C++ <iterator> header.
77 
78  \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator
79  */
80 template <bool Const, typename Encoding, typename Allocator>
82  : public std::iterator<std::random_access_iterator_tag
83  , typename internal::MaybeAddConst<Const,GenericMember<Encoding,Allocator> >::Type> {
84 
85  friend class GenericValue<Encoding,Allocator>;
86  template <bool, typename, typename> friend class GenericMemberIterator;
87 
89  typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType;
90  typedef std::iterator<std::random_access_iterator_tag,ValueType> BaseType;
91 
92 public:
93  //! Iterator type itself
95  //! Constant iterator type
97  //! Non-constant iterator type
99 
100  //! Pointer to (const) GenericMember
101  typedef typename BaseType::pointer Pointer;
102  //! Reference to (const) GenericMember
103  typedef typename BaseType::reference Reference;
104  //! Signed integer type (e.g. \c ptrdiff_t)
105  typedef typename BaseType::difference_type DifferenceType;
106 
107  //! Default constructor (singular value)
108  /*! Creates an iterator pointing to no element.
109  \note All operations, except for comparisons, are undefined on such values.
110  */
111  GenericMemberIterator() : ptr_() {}
112 
113  //! Iterator conversions to more const
114  /*!
115  \param it (Non-const) iterator to copy from
116 
117  Allows the creation of an iterator from another GenericMemberIterator
118  that is "less const". Especially, creating a non-constant iterator
119  from a constant iterator are disabled:
120  \li const -> non-const (not ok)
121  \li const -> const (ok)
122  \li non-const -> const (ok)
123  \li non-const -> non-const (ok)
124 
125  \note If the \c Const template parameter is already \c false, this
126  constructor effectively defines a regular copy-constructor.
127  Otherwise, the copy constructor is implicitly defined.
128  */
129  GenericMemberIterator(const NonConstType & it) : ptr_( it.ptr_ ) {}
130 
131  //! @name stepping
132  //@{
133  Iterator& operator++(){ ++ptr_; return *this; }
134  Iterator& operator--(){ --ptr_; return *this; }
135  Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; }
136  Iterator operator--(int){ Iterator old(*this); --ptr_; return old; }
137  //@}
138 
139  //! @name increment/decrement
140  //@{
141  Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); }
142  Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); }
143 
144  Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; }
145  Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; }
146  //@}
147 
148  //! @name relations
149  //@{
150  bool operator==(Iterator that) const { return ptr_ == that.ptr_; }
151  bool operator!=(Iterator that) const { return ptr_ != that.ptr_; }
152  bool operator<=(Iterator that) const { return ptr_ <= that.ptr_; }
153  bool operator>=(Iterator that) const { return ptr_ >= that.ptr_; }
154  bool operator< (Iterator that) const { return ptr_ < that.ptr_; }
155  bool operator> (Iterator that) const { return ptr_ > that.ptr_; }
156  //@}
157 
158  //! @name dereference
159  //@{
160  Reference operator*() const { return *ptr_; }
161  Pointer operator->() const { return ptr_; }
162  Reference operator[](DifferenceType n) const { return ptr_[n]; }
163  //@}
164 
165  //! Distance
166  DifferenceType operator-(Iterator that) const { return ptr_-that.ptr_; }
167 
168 private:
169  //! Internal constructor from plain pointer
170  explicit GenericMemberIterator(Pointer p) : ptr_(p) {}
171 
172  Pointer ptr_; //!< raw pointer
173 };
174 
175 #else // RAPIDJSON_NOMEMBERITERATORCLASS
176 
177 // class-based member iterator implementation disabled, use plain pointers
178 
179 template <bool Const, typename Encoding, typename Allocator>
180 struct GenericMemberIterator;
181 
182 //! non-const GenericMemberIterator
183 template <typename Encoding, typename Allocator>
184 struct GenericMemberIterator<false,Encoding,Allocator> {
185  //! use plain pointer as iterator type
186  typedef GenericMember<Encoding,Allocator>* Iterator;
187 };
188 //! const GenericMemberIterator
189 template <typename Encoding, typename Allocator>
190 struct GenericMemberIterator<true,Encoding,Allocator> {
191  //! use plain const pointer as iterator type
192  typedef const GenericMember<Encoding,Allocator>* Iterator;
193 };
194 
195 #endif // RAPIDJSON_NOMEMBERITERATORCLASS
196 
197 ///////////////////////////////////////////////////////////////////////////////
198 // GenericStringRef
199 
200 //! Reference to a constant string (not taking a copy)
201 /*!
202  \tparam CharType character type of the string
203 
204  This helper class is used to automatically infer constant string
205  references for string literals, especially from \c const \b (!)
206  character arrays.
207 
208  The main use is for creating JSON string values without copying the
209  source string via an \ref Allocator. This requires that the referenced
210  string pointers have a sufficient lifetime, which exceeds the lifetime
211  of the associated GenericValue.
212 
213  \b Example
214  \code
215  Value v("foo"); // ok, no need to copy & calculate length
216  const char foo[] = "foo";
217  v.SetString(foo); // ok
218 
219  const char* bar = foo;
220  // Value x(bar); // not ok, can't rely on bar's lifetime
221  Value x(StringRef(bar)); // lifetime explicitly guaranteed by user
222  Value y(StringRef(bar, 3)); // ok, explicitly pass length
223  \endcode
224 
225  \see StringRef, GenericValue::SetString
226 */
227 template<typename CharType>
229  typedef CharType Ch; //!< character type of the string
230 
231  //! Create string reference from \c const character array
232  /*!
233  This constructor implicitly creates a constant string reference from
234  a \c const character array. It has better performance than
235  \ref StringRef(const CharType*) by inferring the string \ref length
236  from the array length, and also supports strings containing null
237  characters.
238 
239  \tparam N length of the string, automatically inferred
240 
241  \param str Constant character array, lifetime assumed to be longer
242  than the use of the string in e.g. a GenericValue
243 
244  \post \ref s == str
245 
246  \note Constant complexity.
247  \note There is a hidden, private overload to disallow references to
248  non-const character arrays to be created via this constructor.
249  By this, e.g. function-scope arrays used to be filled via
250  \c snprintf are excluded from consideration.
251  In such cases, the referenced string should be \b copied to the
252  GenericValue instead.
253  */
254  template<SizeType N>
255  GenericStringRef(const CharType (&str)[N])
256  : s(str), length(N-1) {}
257 
258  //! Explicitly create string reference from \c const character pointer
259  /*!
260  This constructor can be used to \b explicitly create a reference to
261  a constant string pointer.
262 
263  \see StringRef(const CharType*)
264 
265  \param str Constant character pointer, lifetime assumed to be longer
266  than the use of the string in e.g. a GenericValue
267 
268  \post \ref s == str
269 
270  \note There is a hidden, private overload to disallow references to
271  non-const character arrays to be created via this constructor.
272  By this, e.g. function-scope arrays used to be filled via
273  \c snprintf are excluded from consideration.
274  In such cases, the referenced string should be \b copied to the
275  GenericValue instead.
276  */
277  explicit GenericStringRef(const CharType* str)
278  : s(str), length(internal::StrLen(str)){}
279 
280  //! Create constant string reference from pointer and length
281  /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
282  \param len length of the string, excluding the trailing NULL terminator
283 
284  \post \ref s == str && \ref length == len
285  \note Constant complexity.
286  */
287  GenericStringRef(const CharType* str, SizeType len)
288  : s(str), length(len) { RAPIDJSON_ASSERT(s != NULL); }
289 
290  //! implicit conversion to plain CharType pointer
291  operator const Ch *() const { return s; }
292 
293  const Ch* const s; //!< plain CharType pointer
294  const SizeType length; //!< length of the string (excluding the trailing NULL terminator)
295 
296 private:
297  //! Disallow copy-assignment
298  GenericStringRef operator=(const GenericStringRef&);
299  //! Disallow construction from non-const array
300  template<SizeType N>
301  GenericStringRef(CharType (&str)[N]) /* = delete */;
302 };
303 
304 //! Mark a character pointer as constant string
305 /*! Mark a plain character pointer as a "string literal". This function
306  can be used to avoid copying a character string to be referenced as a
307  value in a JSON GenericValue object, if the string's lifetime is known
308  to be valid long enough.
309  \tparam CharType Character type of the string
310  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
311  \return GenericStringRef string reference object
312  \relatesalso GenericStringRef
313 
314  \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember
315 */
316 template<typename CharType>
317 inline GenericStringRef<CharType> StringRef(const CharType* str) {
318  return GenericStringRef<CharType>(str, internal::StrLen(str));
319 }
320 
321 //! Mark a character pointer as constant string
322 /*! Mark a plain character pointer as a "string literal". This function
323  can be used to avoid copying a character string to be referenced as a
324  value in a JSON GenericValue object, if the string's lifetime is known
325  to be valid long enough.
326 
327  This version has better performance with supplied length, and also
328  supports string containing null characters.
329 
330  \tparam CharType character type of the string
331  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
332  \param length The length of source string.
333  \return GenericStringRef string reference object
334  \relatesalso GenericStringRef
335 */
336 template<typename CharType>
337 inline GenericStringRef<CharType> StringRef(const CharType* str, size_t length) {
338  return GenericStringRef<CharType>(str, SizeType(length));
339 }
340 
341 ///////////////////////////////////////////////////////////////////////////////
342 // GenericValue
343 
344 //! Represents a JSON value. Use Value for UTF8 encoding and default allocator.
345 /*!
346  A JSON value can be one of 7 types. This class is a variant type supporting
347  these types.
348 
349  Use the Value if UTF8 and default allocator
350 
351  \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
352  \tparam Allocator Allocator type for allocating memory of object, array and string.
353 */
354 #pragma pack (push, 4)
355 template <typename Encoding, typename Allocator = MemoryPoolAllocator<> >
356 class GenericValue {
357 public:
358  //! Name-value pair in an object.
360  typedef Encoding EncodingType; //!< Encoding type from template parameter.
361  typedef Allocator AllocatorType; //!< Allocator type from template parameter.
362  typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
363  typedef GenericStringRef<Ch> StringRefType; //!< Reference to a constant string
364  typedef typename GenericMemberIterator<false,Encoding,Allocator>::Iterator MemberIterator; //!< Member iterator for iterating in object.
365  typedef typename GenericMemberIterator<true,Encoding,Allocator>::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object.
366  typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array.
367  typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array.
368 
369  //!@name Constructors and destructor.
370  //@{
371 
372  //! Default constructor creates a null value.
373  GenericValue() : data_(), flags_(kNullFlag) {}
374 
375 private:
376  //! Copy constructor is not permitted.
377  GenericValue(const GenericValue& rhs);
378 
379 public:
380 
381  //! Constructor with JSON value type.
382  /*! This creates a Value of specified type with default content.
383  \param type Type of the value.
384  \note Default content for number is zero.
385  */
386  GenericValue(Type type) : data_(), flags_() {
387  static const unsigned defaultFlags[7] = {
388  kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kConstStringFlag,
389  kNumberAnyFlag
390  };
391  RAPIDJSON_ASSERT(type <= kNumberType);
392  flags_ = defaultFlags[type];
393  }
394 
395  //! Explicit copy constructor (with allocator)
396  /*! Creates a copy of a Value by using the given Allocator
397  \tparam SourceAllocator allocator of \c rhs
398  \param rhs Value to copy from (read-only)
399  \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator().
400  \see CopyFrom()
401  */
402  template< typename SourceAllocator >
404 
405  //! Constructor for boolean value.
406  /*! \param b Boolean value
407  \note This constructor is limited to \em real boolean values and rejects
408  implicitly converted types like arbitrary pointers. Use an explicit cast
409  to \c bool, if you want to construct a boolean JSON value in such cases.
410  */
411 #ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen
412  template <typename T>
413  explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame<T,bool>)))
414 #else
415  explicit GenericValue(bool b)
416 #endif
417  : data_(), flags_(b ? kTrueFlag : kFalseFlag) {}
418 
419  //! Constructor for int value.
420  explicit GenericValue(int i) : data_(), flags_(kNumberIntFlag) {
421  data_.n.i64 = i;
422  if (i >= 0)
423  flags_ |= kUintFlag | kUint64Flag;
424  }
425 
426  //! Constructor for unsigned value.
427  explicit GenericValue(unsigned u) : data_(), flags_(kNumberUintFlag) {
428  data_.n.u64 = u;
429  if (!(u & 0x80000000))
430  flags_ |= kIntFlag | kInt64Flag;
431  }
432 
433  //! Constructor for int64_t value.
434  explicit GenericValue(int64_t i64) : data_(), flags_(kNumberInt64Flag) {
435  data_.n.i64 = i64;
436  if (i64 >= 0) {
437  flags_ |= kNumberUint64Flag;
438  if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
439  flags_ |= kUintFlag;
440  if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
441  flags_ |= kIntFlag;
442  }
443  else if (i64 >= static_cast<int64_t>(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
444  flags_ |= kIntFlag;
445  }
446 
447  //! Constructor for uint64_t value.
448  explicit GenericValue(uint64_t u64) : data_(), flags_(kNumberUint64Flag) {
449  data_.n.u64 = u64;
450  if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000)))
451  flags_ |= kInt64Flag;
452  if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
453  flags_ |= kUintFlag;
454  if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
455  flags_ |= kIntFlag;
456  }
457 
458  //! Constructor for double value.
459  explicit GenericValue(double d) : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; }
460 
461  //! Constructor for constant string (i.e. do not make a copy of string)
462  GenericValue(const Ch* s, SizeType length) : data_(), flags_() { SetStringRaw(StringRef(s, length)); }
463 
464  //! Constructor for constant string (i.e. do not make a copy of string)
465  explicit GenericValue(StringRefType s) : data_(), flags_() { SetStringRaw(s); }
466 
467  //! Constructor for copy-string (i.e. do make a copy of string)
468  GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s, length), allocator); }
469 
470  //! Constructor for copy-string (i.e. do make a copy of string)
471  GenericValue(const Ch*s, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s), allocator); }
472 
473  //! Destructor.
474  /*! Need to destruct elements of array, members of object, or copy-string.
475  */
477  if (Allocator::kNeedFree) { // Shortcut by Allocator's trait
478  switch(flags_) {
479  case kArrayFlag:
480  for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v)
481  v->~GenericValue();
482  Allocator::Free(data_.a.elements);
483  break;
484 
485  case kObjectFlag:
486  for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
487  m->~GenericMember();
488  }
489  Allocator::Free(data_.o.members);
490  break;
491 
492  case kCopyStringFlag:
493  Allocator::Free(const_cast<Ch*>(data_.s.str));
494  break;
495 
496  default:
497  break; // Do nothing for other types.
498  }
499  }
500  }
501 
502  //@}
503 
504  //!@name Assignment operators
505  //@{
506 
507  //! Assignment with move semantics.
508  /*! \param rhs Source of the assignment. It will become a null value after assignment.
509  */
511  RAPIDJSON_ASSERT(this != &rhs);
512  this->~GenericValue();
513  RawAssign(rhs);
514  return *this;
515  }
516 
517  //! Assignment of constant string reference (no copy)
518  /*! \param str Constant string reference to be assigned
519  \note This overload is needed to avoid clashes with the generic primitive type assignment overload below.
520  \see GenericStringRef, operator=(T)
521  */
523  GenericValue s(str);
524  return *this = s;
525  }
526 
527  //! Assignment with primitive types.
528  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
529  \param value The value to be assigned.
530 
531  \note The source type \c T explicitly disallows all pointer types,
532  especially (\c const) \ref Ch*. This helps avoiding implicitly
533  referencing character strings with insufficient lifetime, use
534  \ref SetString(const Ch*, Allocator&) (for copying) or
535  \ref StringRef() (to explicitly mark the pointer as constant) instead.
536  All other pointer types would implicitly convert to \c bool,
537  use \ref SetBool() instead.
538  */
539  template <typename T>
540  RAPIDJSON_DISABLEIF_RETURN(internal::IsPointer<T>,GenericValue&)
541  operator=(T value) {
542  GenericValue v(value);
543  return *this = v;
544  }
545 
546  //! Deep-copy assignment from Value
547  /*! Assigns a \b copy of the Value to the current Value object
548  \tparam SourceAllocator Allocator type of \c rhs
549  \param rhs Value to copy from (read-only)
550  \param allocator Allocator to use for copying
551  */
552  template <typename SourceAllocator>
554  RAPIDJSON_ASSERT((void*)this != (void const*)&rhs);
555  this->~GenericValue();
556  new (this) GenericValue(rhs,allocator);
557  return *this;
558  }
559 
560  //! Exchange the contents of this value with those of other.
561  /*!
562  \param other Another value.
563  \note Constant complexity.
564  */
566  GenericValue temp;
567  temp.RawAssign(*this);
568  RawAssign(other);
569  other.RawAssign(temp);
570  return *this;
571  }
572 
573  //! Prepare Value for move semantics
574  /*! \return *this */
575  GenericValue& Move() { return *this; }
576  //@}
577 
578  //!@name Equal-to and not-equal-to operators
579  //@{
580  //! Equal-to operator
581  /*!
582  \note If an object contains duplicated named member, comparing equality with any object is always \c false.
583  \note Linear time complexity (number of all values in the subtree and total lengths of all strings).
584  */
585  bool operator==(const GenericValue& rhs) const {
586  if (GetType() != rhs.GetType())
587  return false;
588 
589  switch (GetType()) {
590  case kObjectType: // Warning: O(n^2) inner-loop
591  if (data_.o.size != rhs.data_.o.size)
592  return false;
593  for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
594  ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
595  if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
596  return false;
597  }
598  return true;
599 
600  case kArrayType:
601  if (data_.a.size != rhs.data_.a.size)
602  return false;
603  for (SizeType i = 0; i < data_.a.size; i++)
604  if ((*this)[i] != rhs[i])
605  return false;
606  return true;
607 
608  case kStringType:
609  return StringEqual(rhs);
610 
611  case kNumberType:
612  if (IsDouble() || rhs.GetDouble())
613  return GetDouble() == rhs.GetDouble(); // May convert one operand from integer to double.
614  else
615  return data_.n.u64 == rhs.data_.n.u64;
616 
617  default: // kTrueType, kFalseType, kNullType
618  return true;
619  }
620  }
621 
622  //! Not-equal-to operator
623  bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); }
624 
625  //! (Not-)Equal-to operator with const C-string pointer.
626  friend bool operator==(const GenericValue& lhs, const Ch* rhs) { return lhs == GenericValue(StringRef(rhs)); }
627  friend bool operator!=(const GenericValue& lhs, const Ch* rhs) { return !(lhs == rhs); }
628  friend bool operator==(const Ch* lhs, const GenericValue& rhs) { return GenericValue(StringRef(lhs)) == rhs; }
629  friend bool operator!=(const Ch* lhs, const GenericValue& rhs) { return !(lhs == rhs); }
630 
631  //! (Not-)Equal-to operator with non-const C-string pointer.
632  friend bool operator==(const GenericValue& lhs, Ch* rhs) { return lhs == GenericValue(StringRef(rhs)); }
633  friend bool operator!=(const GenericValue& lhs, Ch* rhs) { return !(lhs == rhs); }
634  friend bool operator==(Ch* lhs, const GenericValue& rhs) { return GenericValue(StringRef(lhs)) == rhs; }
635  friend bool operator!=(Ch* lhs, const GenericValue& rhs) { return !(lhs == rhs); }
636 
637  //! (Not-)Equal-to operator with primitive types.
638  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false
639  */
640  template <typename T> friend bool operator==(const GenericValue& lhs, const T& rhs) { return lhs == GenericValue(rhs); }
641  template <typename T> friend bool operator!=(const GenericValue& lhs, const T& rhs) { return !(lhs == rhs); }
642  template <typename T> friend bool operator==(const T& lhs, const GenericValue& rhs) { return GenericValue(lhs) == rhs; }
643  template <typename T> friend bool operator!=(const T& lhs, const GenericValue& rhs) { return !(lhs == rhs); }
644  //@}
645 
646  //!@name Type
647  //@{
648 
649  Type GetType() const { return static_cast<Type>(flags_ & kTypeMask); }
650  bool IsNull() const { return flags_ == kNullFlag; }
651  bool IsFalse() const { return flags_ == kFalseFlag; }
652  bool IsTrue() const { return flags_ == kTrueFlag; }
653  bool IsBool() const { return (flags_ & kBoolFlag) != 0; }
654  bool IsObject() const { return flags_ == kObjectFlag; }
655  bool IsArray() const { return flags_ == kArrayFlag; }
656  bool IsNumber() const { return (flags_ & kNumberFlag) != 0; }
657  bool IsInt() const { return (flags_ & kIntFlag) != 0; }
658  bool IsUint() const { return (flags_ & kUintFlag) != 0; }
659  bool IsInt64() const { return (flags_ & kInt64Flag) != 0; }
660  bool IsUint64() const { return (flags_ & kUint64Flag) != 0; }
661  bool IsDouble() const { return (flags_ & kDoubleFlag) != 0; }
662  bool IsString() const { return (flags_ & kStringFlag) != 0; }
663 
664  //@}
665 
666  //!@name Null
667  //@{
668 
669  GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; }
670 
671  //@}
672 
673  //!@name Bool
674  //@{
675 
676  bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return flags_ == kTrueFlag; }
677  //!< Set boolean value
678  /*! \post IsBool() == true */
679  GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; }
680 
681  //@}
682 
683  //!@name Object
684  //@{
685 
686  //! Set this value as an empty object.
687  /*! \post IsObject() == true */
688  GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; }
689 
690  //! Get the value associated with the name.
691  /*!
692  \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7.
693  Since 0.2, if the name is not correct, it will assert.
694  If user is unsure whether a member exists, user should use HasMember() first.
695  A better approach is to use FindMember().
696  \note Linear time complexity.
697  */
698  GenericValue& operator[](const Ch* name) {
699  GenericValue n(StringRef(name));
700  return (*this)[n];
701  }
702  const GenericValue& operator[](const Ch* name) const { return const_cast<GenericValue&>(*this)[name]; }
703 
704  // This version is faster because it does not need a StrLen().
705  // It can also handle string with null character.
706  GenericValue& operator[](const GenericValue& name) {
707  MemberIterator member = FindMember(name);
708  if (member != MemberEnd())
709  return member->value;
710  else {
711  RAPIDJSON_ASSERT(false); // see above note
712  static GenericValue NullValue;
713  return NullValue;
714  }
715  }
716  const GenericValue& operator[](const GenericValue& name) const { return const_cast<GenericValue&>(*this)[name]; }
717 
718  //! Const member iterator
719  /*! \pre IsObject() == true */
720  ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members); }
721  //! Const \em past-the-end member iterator
722  /*! \pre IsObject() == true */
723  ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members + data_.o.size); }
724  //! Member iterator
725  /*! \pre IsObject() == true */
726  MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members); }
727  //! \em Past-the-end member iterator
728  /*! \pre IsObject() == true */
729  MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members + data_.o.size); }
730 
731  //! Check whether a member exists in the object.
732  /*!
733  \param name Member name to be searched.
734  \pre IsObject() == true
735  \return Whether a member with that name exists.
736  \note It is better to use FindMember() directly if you need the obtain the value as well.
737  \note Linear time complexity.
738  */
739  bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); }
740 
741  //! Check whether a member exists in the object with GenericValue name.
742  /*!
743  This version is faster because it does not need a StrLen(). It can also handle string with null character.
744  \param name Member name to be searched.
745  \pre IsObject() == true
746  \return Whether a member with that name exists.
747  \note It is better to use FindMember() directly if you need the obtain the value as well.
748  \note Linear time complexity.
749  */
750  bool HasMember(const GenericValue& name) const { return FindMember(name) != MemberEnd(); }
751 
752  //! Find member by name.
753  /*!
754  \param name Member name to be searched.
755  \pre IsObject() == true
756  \return Iterator to member, if it exists.
757  Otherwise returns \ref MemberEnd().
758 
759  \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
760  the requested member doesn't exist. For consistency with e.g.
761  \c std::map, this has been changed to MemberEnd() now.
762  \note Linear time complexity.
763  */
764  MemberIterator FindMember(const Ch* name) {
765  GenericValue n(StringRef(name));
766  return FindMember(n);
767  }
768 
769  ConstMemberIterator FindMember(const Ch* name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
770 
771  //! Find member by name.
772  /*!
773  This version is faster because it does not need a StrLen(). It can also handle string with null character.
774  \param name Member name to be searched.
775  \pre IsObject() == true
776  \return Iterator to member, if it exists.
777  Otherwise returns \ref MemberEnd().
778 
779  \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
780  the requested member doesn't exist. For consistency with e.g.
781  \c std::map, this has been changed to MemberEnd() now.
782  \note Linear time complexity.
783  */
785  RAPIDJSON_ASSERT(IsObject());
786  RAPIDJSON_ASSERT(name.IsString());
787  MemberIterator member = MemberBegin();
788  for ( ; member != MemberEnd(); ++member)
789  if (name.StringEqual(member->name))
790  break;
791  return member;
792  }
793  ConstMemberIterator FindMember(const GenericValue& name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
794 
795  //! Add a member (name-value pair) to the object.
796  /*! \param name A string value as name of member.
797  \param value Value of any type.
798  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
799  \return The value itself for fluent API.
800  \note The ownership of \c name and \c value will be transferred to this object on success.
801  \pre IsObject() && name.IsString()
802  \post name.IsNull() && value.IsNull()
803  \note Amortized Constant time complexity.
804  */
806  RAPIDJSON_ASSERT(IsObject());
807  RAPIDJSON_ASSERT(name.IsString());
808 
809  Object& o = data_.o;
810  if (o.size >= o.capacity) {
811  if (o.capacity == 0) {
812  o.capacity = kDefaultObjectCapacity;
813  o.members = reinterpret_cast<Member*>(allocator.Malloc(o.capacity * sizeof(Member)));
814  }
815  else {
816  SizeType oldCapacity = o.capacity;
817  o.capacity *= 2;
818  o.members = reinterpret_cast<Member*>(allocator.Realloc(o.members, oldCapacity * sizeof(Member), o.capacity * sizeof(Member)));
819  }
820  }
821  o.members[o.size].name.RawAssign(name);
822  o.members[o.size].value.RawAssign(value);
823  o.size++;
824  return *this;
825  }
826 
827  //! Add a member (name-value pair) to the object.
828  /*! \param name A constant string reference as name of member.
829  \param value Value of any type.
830  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
831  \return The value itself for fluent API.
832  \note The ownership of \c value will be transferred to this object on success.
833  \pre IsObject()
834  \post value.IsNull()
835  \note Amortized Constant time complexity.
836  */
838  GenericValue n(name);
839  return AddMember(n, value, allocator);
840  }
841 
842  //! Add a constant string value as member (name-value pair) to the object.
843  /*! \param name A constant string reference as name of member.
844  \param value constant string reference as value of member.
845  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
846  \return The value itself for fluent API.
847  \pre IsObject()
848  \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below.
849  \note Amortized Constant time complexity.
850  */
852  GenericValue v(value);
853  return AddMember(name, v, allocator);
854  }
855 
856  //! Add any primitive value as member (name-value pair) to the object.
857  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
858  \param name A constant string reference as name of member.
859  \param value Value of primitive type \c T as value of member
860  \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator().
861  \return The value itself for fluent API.
862  \pre IsObject()
863 
864  \note The source type \c T explicitly disallows all pointer types,
865  especially (\c const) \ref Ch*. This helps avoiding implicitly
866  referencing character strings with insufficient lifetime, use
867  \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref
868  AddMember(StringRefType, StringRefType, Allocator&).
869  All other pointer types would implicitly convert to \c bool,
870  use an explicit cast instead, if needed.
871  \note Amortized Constant time complexity.
872  */
873  template <typename T>
874  RAPIDJSON_DISABLEIF_RETURN(internal::IsPointer<T>,GenericValue&)
875  AddMember(StringRefType name, T value, Allocator& allocator) {
876  GenericValue n(name);
877  GenericValue v(value);
878  return AddMember(n, v, allocator);
879  }
880 
881  //! Remove a member in object by its name.
882  /*! \param name Name of member to be removed.
883  \return Whether the member existed.
884  \note Removing member is implemented by moving the last member. So the ordering of members is changed.
885  \note Linear time complexity.
886  */
887  bool RemoveMember(const Ch* name) {
888  GenericValue n(StringRef(name));
889  return RemoveMember(n);
890  }
891 
892  bool RemoveMember(const GenericValue& name) {
893  MemberIterator m = FindMember(name);
894  if (m != MemberEnd()) {
895  RemoveMember(m);
896  return true;
897  }
898  else
899  return false;
900  }
901 
902  //! Remove a member in object by iterator.
903  /*! \param m member iterator (obtained by FindMember() or MemberBegin()).
904  \return the new iterator after removal.
905  \note Removing member is implemented by moving the last member. So the ordering of members is changed.
906  \note Use \ref EraseMember(ConstMemberIterator) instead, if you need to rely on a stable member ordering.
907  \note Constant time complexity.
908  */
910  RAPIDJSON_ASSERT(IsObject());
911  RAPIDJSON_ASSERT(data_.o.size > 0);
912  RAPIDJSON_ASSERT(data_.o.members != 0);
913  RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd());
914 
915  MemberIterator last(data_.o.members + (data_.o.size - 1));
916  if (data_.o.size > 1 && m != last) {
917  // Move the last one to this place
918  *m = *last;
919  }
920  else {
921  // Only one left, just destroy
922  m->~GenericMember();
923  }
924  --data_.o.size;
925  return m;
926  }
927 
928  //! Remove a member from an object by iterator.
929  /*! \param pos iterator to the member to remove
930  \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd()
931  \return Iterator following the removed element.
932  If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned.
933  \note Other than \ref RemoveMember(MemberIterator), this function preserves the ordering of the members.
934  \note Linear time complexity.
935  */
937  return EraseMember(pos, pos +1);
938  }
939 
940  //! Remove members in the range [first, last) from an object.
941  /*! \param first iterator to the first member to remove
942  \param last iterator following the last member to remove
943  \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd()
944  \return Iterator following the last removed element.
945  \note Other than \ref RemoveMember(MemberIterator), this function preserves the ordering of the members.
946  \note Linear time complexity.
947  */
949  RAPIDJSON_ASSERT(IsObject());
950  RAPIDJSON_ASSERT(data_.o.size > 0);
951  RAPIDJSON_ASSERT(data_.o.members != 0);
952  RAPIDJSON_ASSERT(first >= MemberBegin());
953  RAPIDJSON_ASSERT(first <= last);
954  RAPIDJSON_ASSERT(last <= MemberEnd());
955 
956  MemberIterator pos = MemberBegin() + (first - MemberBegin());
957  for (MemberIterator itr = pos; ConstMemberIterator(itr) != last; ++itr)
958  itr->~Member();
959  memmove(&*pos, &*last, (ConstMemberIterator(MemberEnd()) - last) * sizeof(Member));
960  data_.o.size -= (last - first);
961  return pos;
962  }
963 
964  //@}
965 
966  //!@name Array
967  //@{
968 
969  //! Set this value as an empty array.
970  /*! \post IsArray == true */
971  GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; }
972 
973  //! Get the number of elements in array.
974  SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; }
975 
976  //! Get the capacity of array.
977  SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; }
978 
979  //! Check whether the array is empty.
980  bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; }
981 
982  //! Remove all elements in the array.
983  /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged.
984  \note Linear time complexity.
985  */
986  void Clear() {
987  RAPIDJSON_ASSERT(IsArray());
988  for (SizeType i = 0; i < data_.a.size; ++i)
989  data_.a.elements[i].~GenericValue();
990  data_.a.size = 0;
991  }
992 
993  //! Get an element from array by index.
994  /*! \param index Zero-based index of element.
995 \code
996 Value a(kArrayType);
997 a.PushBack(123);
998 int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of const char* type.
999 int y = a[SizeType(0)].GetInt(); // Cast to SizeType will work.
1000 int z = a[0u].GetInt(); // This works too.
1001 \endcode
1002  */
1004  RAPIDJSON_ASSERT(IsArray());
1005  RAPIDJSON_ASSERT(index < data_.a.size);
1006  return data_.a.elements[index];
1007  }
1008  const GenericValue& operator[](SizeType index) const { return const_cast<GenericValue&>(*this)[index]; }
1009 
1010  //! Element iterator
1011  /*! \pre IsArray() == true */
1012  ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements; }
1013  //! \em Past-the-end element iterator
1014  /*! \pre IsArray() == true */
1015  ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements + data_.a.size; }
1016  //! Constant element iterator
1017  /*! \pre IsArray() == true */
1018  ConstValueIterator Begin() const { return const_cast<GenericValue&>(*this).Begin(); }
1019  //! Constant \em past-the-end element iterator
1020  /*! \pre IsArray() == true */
1021  ConstValueIterator End() const { return const_cast<GenericValue&>(*this).End(); }
1022 
1023  //! Request the array to have enough capacity to store elements.
1024  /*! \param newCapacity The capacity that the array at least need to have.
1025  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1026  \return The value itself for fluent API.
1027  \note Linear time complexity.
1028  */
1029  GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) {
1030  RAPIDJSON_ASSERT(IsArray());
1031  if (newCapacity > data_.a.capacity) {
1032  data_.a.elements = (GenericValue*)allocator.Realloc(data_.a.elements, data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue));
1033  data_.a.capacity = newCapacity;
1034  }
1035  return *this;
1036  }
1037 
1038  //! Append a GenericValue at the end of the array.
1039  /*! \param value Value to be appended.
1040  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1041  \pre IsArray() == true
1042  \post value.IsNull() == true
1043  \return The value itself for fluent API.
1044  \note The ownership of \c value will be transferred to this array on success.
1045  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1046  \note Amortized constant time complexity.
1047  */
1049  RAPIDJSON_ASSERT(IsArray());
1050  if (data_.a.size >= data_.a.capacity)
1051  Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : data_.a.capacity * 2, allocator);
1052  data_.a.elements[data_.a.size++].RawAssign(value);
1053  return *this;
1054  }
1055 
1056  //! Append a constant string reference at the end of the array.
1057  /*! \param value Constant string reference to be appended.
1058  \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator().
1059  \pre IsArray() == true
1060  \return The value itself for fluent API.
1061  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1062  \note Amortized constant time complexity.
1063  \see GenericStringRef
1064  */
1066  return (*this).template PushBack<StringRefType>(value, allocator);
1067  }
1068 
1069  //! Append a primitive value at the end of the array.
1070  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
1071  \param value Value of primitive type T to be appended.
1072  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1073  \pre IsArray() == true
1074  \return The value itself for fluent API.
1075  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1076 
1077  \note The source type \c T explicitly disallows all pointer types,
1078  especially (\c const) \ref Ch*. This helps avoiding implicitly
1079  referencing character strings with insufficient lifetime, use
1080  \ref PushBack(GenericValue&, Allocator&) or \ref
1081  PushBack(StringRefType, Allocator&).
1082  All other pointer types would implicitly convert to \c bool,
1083  use an explicit cast instead, if needed.
1084  \note Amortized constant time complexity.
1085  */
1086  template <typename T>
1087  RAPIDJSON_DISABLEIF_RETURN(internal::IsPointer<T>,GenericValue&)
1088  PushBack(T value, Allocator& allocator) {
1089  GenericValue v(value);
1090  return PushBack(v, allocator);
1091  }
1092 
1093  //! Remove the last element in the array.
1094  /*!
1095  \note Constant time complexity.
1096  */
1098  RAPIDJSON_ASSERT(IsArray());
1099  RAPIDJSON_ASSERT(!Empty());
1100  data_.a.elements[--data_.a.size].~GenericValue();
1101  return *this;
1102  }
1103 
1104  //! Remove an element of array by iterator.
1105  /*!
1106  \param pos iterator to the element to remove
1107  \pre IsArray() == true && \ref Begin() <= \c pos < \ref End()
1108  \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned.
1109  \note Linear time complexity.
1110  */
1112  return Erase(pos, pos + 1);
1113  }
1114 
1115  //! Remove elements in the range [first, last) of the array.
1116  /*!
1117  \param first iterator to the first element to remove
1118  \param last iterator following the last element to remove
1119  \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End()
1120  \return Iterator following the last removed element.
1121  \note Linear time complexity.
1122  */
1124  RAPIDJSON_ASSERT(IsArray());
1125  RAPIDJSON_ASSERT(data_.a.size > 0);
1126  RAPIDJSON_ASSERT(data_.a.elements != 0);
1127  RAPIDJSON_ASSERT(first >= Begin());
1128  RAPIDJSON_ASSERT(first <= last);
1129  RAPIDJSON_ASSERT(last <= End());
1130  ValueIterator pos = Begin() + (first - Begin());
1131  for (ValueIterator itr = pos; itr != last; ++itr)
1132  itr->~GenericValue();
1133  memmove(pos, last, (End() - last) * sizeof(GenericValue));
1134  data_.a.size -= (last - first);
1135  return pos;
1136  }
1137 
1138  //@}
1139 
1140  //!@name Number
1141  //@{
1142 
1143  int GetInt() const { RAPIDJSON_ASSERT(flags_ & kIntFlag); return data_.n.i.i; }
1144  unsigned GetUint() const { RAPIDJSON_ASSERT(flags_ & kUintFlag); return data_.n.u.u; }
1145  int64_t GetInt64() const { RAPIDJSON_ASSERT(flags_ & kInt64Flag); return data_.n.i64; }
1146  uint64_t GetUint64() const { RAPIDJSON_ASSERT(flags_ & kUint64Flag); return data_.n.u64; }
1147 
1148  double GetDouble() const {
1149  RAPIDJSON_ASSERT(IsNumber());
1150  if ((flags_ & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion.
1151  if ((flags_ & kIntFlag) != 0) return data_.n.i.i; // int -> double
1152  if ((flags_ & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double
1153  if ((flags_ & kInt64Flag) != 0) return (double)data_.n.i64; // int64_t -> double (may lose precision)
1154  RAPIDJSON_ASSERT((flags_ & kUint64Flag) != 0); return (double)data_.n.u64; // uint64_t -> double (may lose precision)
1155  }
1156 
1157  GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; }
1158  GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; }
1159  GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; }
1160  GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; }
1161  GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; }
1162 
1163  //@}
1164 
1165  //!@name String
1166  //@{
1167 
1168  const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return data_.s.str; }
1169 
1170  //! Get the length of string.
1171  /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength().
1172  */
1173  SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return data_.s.length; }
1174 
1175  //! Set this value as a string without copying source string.
1176  /*! This version has better performance with supplied length, and also support string containing null character.
1177  \param s source string pointer.
1178  \param length The length of source string, excluding the trailing null terminator.
1179  \return The value itself for fluent API.
1180  \post IsString() == true && GetString() == s && GetStringLength() == length
1181  \see SetString(StringRefType)
1182  */
1183  GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); }
1184 
1185  //! Set this value as a string without copying source string.
1186  /*! \param s source string reference
1187  \return The value itself for fluent API.
1188  \post IsString() == true && GetString() == s && GetStringLength() == s.length
1189  */
1190  GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; }
1191 
1192  //! Set this value as a string by copying from source string.
1193  /*! This version has better performance with supplied length, and also support string containing null character.
1194  \param s source string.
1195  \param length The length of source string, excluding the trailing null terminator.
1196  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1197  \return The value itself for fluent API.
1198  \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
1199  */
1200  GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; }
1201 
1202  //! Set this value as a string by copying from source string.
1203  /*! \param s source string.
1204  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1205  \return The value itself for fluent API.
1206  \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
1207  */
1208  GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); }
1209 
1210  //@}
1211 
1212  //! Generate events of this value to a Handler.
1213  /*! This function adopts the GoF visitor pattern.
1214  Typical usage is to output this JSON value as JSON text via Writer, which is a Handler.
1215  It can also be used to deep clone this value via GenericDocument, which is also a Handler.
1216  \tparam Handler type of handler.
1217  \param handler An object implementing concept Handler.
1218  */
1219  template <typename Handler>
1220  bool Accept(Handler& handler) const {
1221  switch(GetType()) {
1222  case kNullType: return handler.Null();
1223  case kFalseType: return handler.Bool(false);
1224  case kTrueType: return handler.Bool(true);
1225 
1226  case kObjectType:
1227  if (!handler.StartObject())
1228  return false;
1229  for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
1230  if (!handler.String(m->name.data_.s.str, m->name.data_.s.length, (m->name.flags_ & kCopyFlag) != 0))
1231  return false;
1232  if (!m->value.Accept(handler))
1233  return false;
1234  }
1235  return handler.EndObject(data_.o.size);
1236 
1237  case kArrayType:
1238  if (!handler.StartArray())
1239  return false;
1240  for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v)
1241  if (!v->Accept(handler))
1242  return false;
1243  return handler.EndArray(data_.a.size);
1244 
1245  case kStringType:
1246  return handler.String(data_.s.str, data_.s.length, (flags_ & kCopyFlag) != 0);
1247 
1248  case kNumberType:
1249  if (IsInt()) return handler.Int(data_.n.i.i);
1250  else if (IsUint()) return handler.Uint(data_.n.u.u);
1251  else if (IsInt64()) return handler.Int64(data_.n.i64);
1252  else if (IsUint64()) return handler.Uint64(data_.n.u64);
1253  else return handler.Double(data_.n.d);
1254 
1255  default:
1256  RAPIDJSON_ASSERT(false);
1257  }
1258  return false;
1259  }
1260 
1261 private:
1262  template <typename, typename>
1263  friend class GenericDocument;
1264 
1265  enum {
1266  kBoolFlag = 0x100,
1267  kNumberFlag = 0x200,
1268  kIntFlag = 0x400,
1269  kUintFlag = 0x800,
1270  kInt64Flag = 0x1000,
1271  kUint64Flag = 0x2000,
1272  kDoubleFlag = 0x4000,
1273  kStringFlag = 0x100000,
1274  kCopyFlag = 0x200000,
1275 
1276  // Initial flags of different types.
1277  kNullFlag = kNullType,
1278  kTrueFlag = kTrueType | kBoolFlag,
1279  kFalseFlag = kFalseType | kBoolFlag,
1280  kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag,
1281  kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag,
1282  kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag,
1283  kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag,
1284  kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag,
1285  kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag,
1286  kConstStringFlag = kStringType | kStringFlag,
1287  kCopyStringFlag = kStringType | kStringFlag | kCopyFlag,
1288  kObjectFlag = kObjectType,
1289  kArrayFlag = kArrayType,
1290 
1291  kTypeMask = 0xFF // bitwise-and with mask of 0xFF can be optimized by compiler
1292  };
1293 
1294  static const SizeType kDefaultArrayCapacity = 16;
1295  static const SizeType kDefaultObjectCapacity = 16;
1296 
1297  struct String {
1298  const Ch* str;
1299  SizeType length;
1300  unsigned hashcode; //!< reserved
1301  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1302 
1303  // By using proper binary layout, retrieval of different integer types do not need conversions.
1304  union Number {
1305 #if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN
1306  struct I {
1307  int i;
1308  char padding[4];
1309  }i;
1310  struct U {
1311  unsigned u;
1312  char padding2[4];
1313  }u;
1314 #else
1315  struct I {
1316  char padding[4];
1317  int i;
1318  }i;
1319  struct U {
1320  char padding2[4];
1321  unsigned u;
1322  }u;
1323 #endif
1324  int64_t i64;
1325  uint64_t u64;
1326  double d;
1327  }; // 8 bytes
1328 
1329  struct Object {
1330  Member* members;
1331  SizeType size;
1332  SizeType capacity;
1333  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1334 
1335  struct Array {
1336  GenericValue* elements;
1337  SizeType size;
1338  SizeType capacity;
1339  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1340 
1341  union Data {
1342  String s;
1343  Number n;
1344  Object o;
1345  Array a;
1346  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1347 
1348  // Initialize this value as array with initial data, without calling destructor.
1349  void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) {
1350  flags_ = kArrayFlag;
1351  data_.a.elements = (GenericValue*)allocator.Malloc(count * sizeof(GenericValue));
1352  memcpy(data_.a.elements, values, count * sizeof(GenericValue));
1353  data_.a.size = data_.a.capacity = count;
1354  }
1355 
1356  //! Initialize this value as object with initial data, without calling destructor.
1357  void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) {
1358  flags_ = kObjectFlag;
1359  data_.o.members = (Member*)allocator.Malloc(count * sizeof(Member));
1360  memcpy(data_.o.members, members, count * sizeof(Member));
1361  data_.o.size = data_.o.capacity = count;
1362  }
1363 
1364  //! Initialize this value as constant string, without calling destructor.
1365  void SetStringRaw(StringRefType s) {
1366  flags_ = kConstStringFlag;
1367  data_.s.str = s;
1368  data_.s.length = s.length;
1369  }
1370 
1371  //! Initialize this value as copy string with initial data, without calling destructor.
1372  void SetStringRaw(StringRefType s, Allocator& allocator) {
1373  flags_ = kCopyStringFlag;
1374  data_.s.str = (Ch *)allocator.Malloc((s.length + 1) * sizeof(Ch));
1375  data_.s.length = s.length;
1376  memcpy(const_cast<Ch*>(data_.s.str), s, s.length * sizeof(Ch));
1377  const_cast<Ch*>(data_.s.str)[s.length] = '\0';
1378  }
1379 
1380  //! Assignment without calling destructor
1381  void RawAssign(GenericValue& rhs) {
1382  data_ = rhs.data_;
1383  flags_ = rhs.flags_;
1384  rhs.flags_ = kNullFlag;
1385  }
1386 
1387  bool StringEqual(const GenericValue& rhs) const {
1388  RAPIDJSON_ASSERT(IsString());
1389  RAPIDJSON_ASSERT(rhs.IsString());
1390  return data_.s.length == rhs.data_.s.length &&
1391  (data_.s.str == rhs.data_.s.str // fast path for constant string
1392  || memcmp(data_.s.str, rhs.data_.s.str, sizeof(Ch) * data_.s.length) == 0);
1393  }
1394 
1395  Data data_;
1396  unsigned flags_;
1397 };
1398 #pragma pack (pop)
1399 
1400 //! GenericValue with UTF8 encoding
1402 
1403 ///////////////////////////////////////////////////////////////////////////////
1404 // GenericDocument
1405 
1406 //! A document for parsing JSON text as DOM.
1407 /*!
1408  \note implements Handler concept
1409  \tparam Encoding encoding for both parsing and string storage.
1410  \tparam Allocator allocator for allocating memory for the DOM, and the stack during parsing.
1411  \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructors. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue.
1412 */
1413 template <typename Encoding, typename Allocator = MemoryPoolAllocator<> >
1414 class GenericDocument : public GenericValue<Encoding, Allocator> {
1415 public:
1416  typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
1417  typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of the document.
1418  typedef Allocator AllocatorType; //!< Allocator type from template parameter.
1419 
1420  //! Constructor
1421  /*! \param allocator Optional allocator for allocating stack memory.
1422  \param stackCapacity Initial capacity of stack in bytes.
1423  */
1424  GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseResult_() {}
1425 
1426  //!@name Parse from stream
1427  //!@{
1428 
1429  //! Parse JSON text from an input stream (with Encoding conversion)
1430  /*! \tparam parseFlags Combination of \ref ParseFlag.
1431  \tparam SourceEncoding Encoding of input stream
1432  \tparam InputStream Type of input stream, implementing Stream concept
1433  \param is Input stream to be parsed.
1434  \return The document itself for fluent API.
1435  */
1436  template <unsigned parseFlags, typename SourceEncoding, typename InputStream>
1437  GenericDocument& ParseStream(InputStream& is) {
1438  ValueType::SetNull(); // Remove existing root if exist
1440  ClearStackOnExit scope(*this);
1441  parseResult_ = reader.template Parse<parseFlags>(is, *this);
1442  if (parseResult_) {
1443  RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object
1444  this->RawAssign(*stack_.template Pop<ValueType>(1)); // Add this-> to prevent issue 13.
1445  }
1446  return *this;
1447  }
1448 
1449  //! Parse JSON text from an input stream
1450  /*! \tparam parseFlags Combination of \ref ParseFlag.
1451  \tparam InputStream Type of input stream, implementing Stream concept
1452  \param is Input stream to be parsed.
1453  \return The document itself for fluent API.
1454  */
1455  template <unsigned parseFlags, typename InputStream>
1456  GenericDocument& ParseStream(InputStream& is) {
1457  return ParseStream<parseFlags,Encoding,InputStream>(is);
1458  }
1459 
1460  //! Parse JSON text from an input stream (with \ref kParseDefaultFlags)
1461  /*! \tparam InputStream Type of input stream, implementing Stream concept
1462  \param is Input stream to be parsed.
1463  \return The document itself for fluent API.
1464  */
1465  template <typename InputStream>
1466  GenericDocument& ParseStream(InputStream& is) {
1467  return ParseStream<kParseDefaultFlags, Encoding, InputStream>(is);
1468  }
1469  //!@}
1470 
1471  //!@name Parse in-place from mutable string
1472  //!@{
1473 
1474  //! Parse JSON text from a mutable string (with Encoding conversion)
1475  /*! \tparam parseFlags Combination of \ref ParseFlag.
1476  \tparam SourceEncoding Transcoding from input Encoding
1477  \param str Mutable zero-terminated string to be parsed.
1478  \return The document itself for fluent API.
1479  */
1480  template <unsigned parseFlags, typename SourceEncoding>
1483  return ParseStream<parseFlags | kParseInsituFlag, SourceEncoding>(s);
1484  }
1485 
1486  //! Parse JSON text from a mutable string
1487  /*! \tparam parseFlags Combination of \ref ParseFlag.
1488  \param str Mutable zero-terminated string to be parsed.
1489  \return The document itself for fluent API.
1490  */
1491  template <unsigned parseFlags>
1493  return ParseInsitu<parseFlags, Encoding>(str);
1494  }
1495 
1496  //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags)
1497  /*! \param str Mutable zero-terminated string to be parsed.
1498  \return The document itself for fluent API.
1499  */
1501  return ParseInsitu<kParseDefaultFlags, Encoding>(str);
1502  }
1503  //!@}
1504 
1505  //!@name Parse from read-only string
1506  //!@{
1507 
1508  //! Parse JSON text from a read-only string (with Encoding conversion)
1509  /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
1510  \tparam SourceEncoding Transcoding from input Encoding
1511  \param str Read-only zero-terminated string to be parsed.
1512  */
1513  template <unsigned parseFlags, typename SourceEncoding>
1514  GenericDocument& Parse(const Ch* str) {
1515  RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag));
1517  return ParseStream<parseFlags, SourceEncoding>(s);
1518  }
1519 
1520  //! Parse JSON text from a read-only string
1521  /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
1522  \param str Read-only zero-terminated string to be parsed.
1523  */
1524  template <unsigned parseFlags>
1525  GenericDocument& Parse(const Ch* str) {
1526  return Parse<parseFlags, Encoding>(str);
1527  }
1528 
1529  //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags)
1530  /*! \param str Read-only zero-terminated string to be parsed.
1531  */
1532  GenericDocument& Parse(const Ch* str) {
1533  return Parse<kParseDefaultFlags>(str);
1534  }
1535  //!@}
1536 
1537  //!@name Handling parse errors
1538  //!@{
1539 
1540  //! Whether a parse error has occured in the last parsing.
1541  bool HasParseError() const { return parseResult_.IsError(); }
1542 
1543  //! Get the \ref ParseErrorCode of last parsing.
1544  ParseErrorCode GetParseError() const { return parseResult_.Code(); }
1545 
1546  //! Get the position of last parsing error in input, 0 otherwise.
1547  size_t GetErrorOffset() const { return parseResult_.Offset(); }
1548 
1549  //!@}
1550 
1551  //! Get the allocator of this document.
1552  Allocator& GetAllocator() { return stack_.GetAllocator(); }
1553 
1554  //! Get the capacity of stack in bytes.
1555  size_t GetStackCapacity() const { return stack_.GetCapacity(); }
1556 
1557 private:
1558  // clear stack on any exit from ParseStream, e.g. due to exception
1559  struct ClearStackOnExit {
1560  explicit ClearStackOnExit(GenericDocument& d) : d_(d) {}
1561  ~ClearStackOnExit() { d_.ClearStack(); }
1562  private:
1563  ClearStackOnExit(const ClearStackOnExit&);
1564  ClearStackOnExit& operator=(const ClearStackOnExit&);
1565  GenericDocument& d_;
1566  };
1567 
1568  // callers of the following private Handler functions
1569  template <typename,typename,typename> friend class GenericReader; // for parsing
1570  friend class GenericValue<Encoding,Allocator>; // for deep copying
1571 
1572  // Implementation of Handler
1573  bool Null() { new (stack_.template Push<ValueType>()) ValueType(); return true; }
1574  bool Bool(bool b) { new (stack_.template Push<ValueType>()) ValueType(b); return true; }
1575  bool Int(int i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1576  bool Uint(unsigned i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1577  bool Int64(int64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1578  bool Uint64(uint64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1579  bool Double(double d) { new (stack_.template Push<ValueType>()) ValueType(d); return true; }
1580 
1581  bool String(const Ch* str, SizeType length, bool copy) {
1582  if (copy)
1583  new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator());
1584  else
1585  new (stack_.template Push<ValueType>()) ValueType(str, length);
1586  return true;
1587  }
1588 
1589  bool StartObject() { new (stack_.template Push<ValueType>()) ValueType(kObjectType); return true; }
1590 
1591  bool EndObject(SizeType memberCount) {
1592  typename ValueType::Member* members = stack_.template Pop<typename ValueType::Member>(memberCount);
1593  stack_.template Top<ValueType>()->SetObjectRaw(members, (SizeType)memberCount, GetAllocator());
1594  return true;
1595  }
1596 
1597  bool StartArray() { new (stack_.template Push<ValueType>()) ValueType(kArrayType); return true; }
1598 
1599  bool EndArray(SizeType elementCount) {
1600  ValueType* elements = stack_.template Pop<ValueType>(elementCount);
1601  stack_.template Top<ValueType>()->SetArrayRaw(elements, elementCount, GetAllocator());
1602  return true;
1603  }
1604 
1605 private:
1606  //! Prohibit assignment
1607  GenericDocument& operator=(const GenericDocument&);
1608 
1609  void ClearStack() {
1610  if (Allocator::kNeedFree)
1611  while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects)
1612  (stack_.template Pop<ValueType>(1))->~ValueType();
1613  else
1614  stack_.Clear();
1615  }
1616 
1617  static const size_t kDefaultStackCapacity = 1024;
1618  internal::Stack<Allocator> stack_;
1619  ParseResult parseResult_;
1620 };
1621 
1622 //! GenericDocument with UTF8 encoding
1624 
1625 // defined here due to the dependency on GenericDocument
1626 template <typename Encoding, typename Allocator>
1627 template <typename SourceAllocator>
1628 inline
1630 {
1632  rhs.Accept(d);
1633  RawAssign(*d.stack_.template Pop<GenericValue>(1));
1634 }
1635 
1636 } // namespace rapidjson
1637 
1638 #if defined(_MSC_VER) || defined(__GNUC__)
1639 RAPIDJSON_DIAG_POP
1640 #endif
1641 
1642 #endif // RAPIDJSON_DOCUMENT_H_
BaseType::difference_type DifferenceType
Signed integer type (e.g. ptrdiff_t)
Definition: document.h:105
true
Definition: rapidjson.h:423
ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)
Remove elements in the range [first, last) of the array.
Definition: document.h:1123
bool operator==(const GenericValue &rhs) const
Equal-to operator.
Definition: document.h:585
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string.
Definition: document.h:1525
Definition: document.h:1306
Read-only string stream.
Definition: rapidjson.h:349
Concept for receiving events from GenericReader upon parsing. The functions return true if no error o...
GenericValue & operator=(StringRefType str)
Assignment of constant string reference (no copy)
Definition: document.h:522
ValueIterator Begin()
Element iterator.
Definition: document.h:1012
GenericValue & Move()
Prepare Value for move semantics.
Definition: document.h:575
friend bool operator==(const GenericValue &lhs, const Ch *rhs)
(Not-)Equal-to operator with const C-string pointer.
Definition: document.h:626
GenericMemberIterator< true, Encoding, Allocator > ConstType
Constant iterator type.
Definition: document.h:96
GenericValue(uint64_t u64)
Constructor for uint64_t value.
Definition: document.h:448
friend bool operator==(const GenericValue &lhs, Ch *rhs)
(Not-)Equal-to operator with non-const C-string pointer.
Definition: document.h:632
GenericValue(const Ch *s, SizeType length, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:468
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
Definition: document.h:1544
~GenericValue()
Destructor.
Definition: document.h:476
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:138
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition: reader.h:353
GenericValue(const Ch *s, SizeType length)
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:462
GenericMemberIterator(const NonConstType &it)
Iterator conversions to more const.
Definition: document.h:129
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string.
Definition: document.h:1492
GenericValue & operator[](const Ch *name)
Get the value associated with the name.
Definition: document.h:698
GenericValue & SetObject()
Set this value as an empty object.
Definition: document.h:688
GenericStringRef< Ch > StringRefType
Reference to a constant string.
Definition: document.h:363
GenericMemberIterator Iterator
Iterator type itself.
Definition: document.h:94
ConstMemberIterator MemberBegin() const
Const member iterator.
Definition: document.h:720
ConstMemberIterator MemberEnd() const
Const past-the-end member iterator.
Definition: document.h:723
unsigned SizeType
Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
Definition: rapidjson.h:162
false
Definition: rapidjson.h:422
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string (with kParseDefaultFlags)
Definition: document.h:1532
GenericValue(const Ch *s, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:471
size_t GetStackCapacity() const
Get the capacity of stack in bytes.
Definition: document.h:1555
bool RemoveMember(const Ch *name)
Remove a member in object by its name.
Definition: document.h:887
bool Accept(Handler &handler) const
Generate events of this value to a Handler.
Definition: document.h:1220
GenericValue(int64_t i64)
Constructor for int64_t value.
Definition: document.h:434
GenericValue(double d)
Constructor for double value.
Definition: document.h:459
GenericValue & PopBack()
Remove the last element in the array.
Definition: document.h:1097
GenericValue & Reserve(SizeType newCapacity, Allocator &allocator)
Request the array to have enough capacity to store elements.
Definition: document.h:1029
ValueIterator End()
Past-the-end element iterator
Definition: document.h:1015
GenericStringRef(const CharType(&str)[N])
Create string reference from const character array.
Definition: document.h:255
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:1418
bool GetBool() const
Set boolean value.
Definition: document.h:676
MemberIterator RemoveMember(MemberIterator m)
Remove a member in object by iterator.
Definition: document.h:909
MemberIterator MemberBegin()
Member iterator.
Definition: document.h:726
bool operator!=(const GenericValue &rhs) const
Not-equal-to operator.
Definition: document.h:623
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:361
bool HasMember(const GenericValue &name) const
Check whether a member exists in the object with GenericValue name.
Definition: document.h:750
GenericValue & AddMember(StringRefType name, StringRefType value, Allocator &allocator)
Add a constant string value as member (name-value pair) to the object.
Definition: document.h:851
friend bool operator==(const GenericValue &lhs, const T &rhs)
(Not-)Equal-to operator with primitive types.
Definition: document.h:640
GenericValue & SetString(const Ch *s, SizeType length, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1200
MemberIterator EraseMember(ConstMemberIterator pos)
Remove a member from an object by iterator.
Definition: document.h:936
GenericStringRef(const CharType *str, SizeType len)
Create constant string reference from pointer and length.
Definition: document.h:287
Name-value pair in a JSON object value.
Definition: document.h:54
GenericValue & SetString(const Ch *s, SizeType length)
Set this value as a string without copying source string.
Definition: document.h:1183
GenericValue(Type type)
Constructor with JSON value type.
Definition: document.h:386
SizeType Size() const
Get the number of elements in array.
Definition: document.h:974
GenericMemberIterator< false, Encoding, Allocator > NonConstType
Non-constant iterator type.
Definition: document.h:98
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:294
BaseType::reference Reference
Reference to (const) GenericMember.
Definition: document.h:103
const GenericValue * ConstValueIterator
Constant value iterator for iterating in array.
Definition: document.h:367
Concept for encoding of Unicode characters.
GenericValue< Encoding, Allocator > ValueType
Value type of the document.
Definition: document.h:1417
GenericValue & AddMember(GenericValue &name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:805
MemberIterator FindMember(const Ch *name)
Find member by name.
Definition: document.h:764
ConstValueIterator Begin() const
Constant element iterator.
Definition: document.h:1018
void Clear()
Remove all elements in the array.
Definition: document.h:986
GenericStringRef< CharType > StringRef(const CharType *str)
Mark a character pointer as constant string.
Definition: document.h:317
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:175
GenericValue & operator=(GenericValue &rhs)
Assignment with move semantics.
Definition: document.h:510
GenericValue()
Default constructor creates a null value.
Definition: document.h:373
Type
Type of JSON value.
Definition: rapidjson.h:420
GenericValue & operator[](SizeType index)
Get an element from array by index.
Definition: document.h:1003
BaseType::pointer Pointer
Pointer to (const) GenericMember.
Definition: document.h:101
object
Definition: rapidjson.h:424
GenericValue & PushBack(GenericValue &value, Allocator &allocator)
Append a GenericValue at the end of the array.
Definition: document.h:1048
bool HasParseError() const
Whether a parse error has occured in the last parsing.
Definition: document.h:1541
MemberIterator MemberEnd()
Past-the-end member iterator
Definition: document.h:729
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream.
Definition: document.h:1456
A document for parsing JSON text as DOM.
Definition: document.h:1414
array
Definition: rapidjson.h:425
GenericValue * ValueIterator
Value iterator for iterating in array.
Definition: document.h:366
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string (with Encoding conversion)
Definition: document.h:1481
bool IsError() const
Whether the result is an error.
Definition: error.h:112
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:1416
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:362
Definition: document.h:1310
GenericMemberIterator< false, Encoding, Allocator >::Iterator MemberIterator
Member iterator for iterating in object.
Definition: document.h:364
GenericValue< Encoding, Allocator > value
value of member.
Definition: document.h:56
GenericStringRef(const CharType *str)
Explicitly create string reference from const character pointer.
Definition: document.h:277
null
Definition: rapidjson.h:421
ParseErrorCode Code() const
Get the error code.
Definition: error.h:105
GenericValue< Encoding, Allocator > name
name of member (must be a string)
Definition: document.h:55
string
Definition: rapidjson.h:426
Allocator & GetAllocator()
Get the allocator of this document.
Definition: document.h:1552
GenericMember< Encoding, Allocator > Member
Name-value pair in an object.
Definition: document.h:359
CharType Ch
character type of the string
Definition: document.h:229
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: document.h:1547
Encoding EncodingType
Encoding type from template parameter.
Definition: document.h:360
DifferenceType operator-(Iterator that) const
Distance.
Definition: document.h:166
GenericValue & SetString(StringRefType s)
Set this value as a string without copying source string.
Definition: document.h:1190
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string (with kParseDefaultFlags)
Definition: document.h:1500
MemberIterator FindMember(const GenericValue &name)
Find member by name.
Definition: document.h:784
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with kParseDefaultFlags)
Definition: document.h:1466
GenericValue(unsigned u)
Constructor for unsigned value.
Definition: document.h:427
In-situ(destructive) parsing.
Definition: reader.h:87
bool HasMember(const Ch *name) const
Check whether a member exists in the object.
Definition: document.h:739
ParseErrorCode
Error code of parsing.
Definition: error.h:56
bool Empty() const
Check whether the array is empty.
Definition: document.h:980
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
Definition: document.h:1514
GenericValue(int i)
Constructor for int value.
Definition: document.h:420
GenericValue(StringRefType s)
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:465
Reference to a constant string (not taking a copy)
Definition: document.h:228
GenericValue & SetBool(bool b)
Definition: document.h:679
GenericDocument(Allocator *allocator=0, size_t stackCapacity=kDefaultStackCapacity)
Constructor.
Definition: document.h:1424
Concept for allocating, resizing and freeing memory block.
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:45
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:1401
GenericValue & CopyFrom(const GenericValue< Encoding, SourceAllocator > &rhs, Allocator &allocator)
Deep-copy assignment from Value.
Definition: document.h:553
GenericValue & SetArray()
Set this value as an empty array.
Definition: document.h:971
(Constant) member iterator for a JSON object value
Definition: document.h:81
GenericValue & SetString(const Ch *s, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1208
GenericValue(bool b)
Constructor for boolean value.
Definition: document.h:415
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
Definition: error.h:107
MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last)
Remove members in the range [first, last) from an object.
Definition: document.h:948
const Ch *const s
plain CharType pointer
Definition: document.h:293
GenericMemberIterator< true, Encoding, Allocator >::Iterator ConstMemberIterator
Constant member iterator for iterating in object.
Definition: document.h:365
ConstValueIterator End() const
Constant past-the-end element iterator.
Definition: document.h:1021
GenericMemberIterator()
Default constructor (singular value)
Definition: document.h:111
SizeType Capacity() const
Get the capacity of array.
Definition: document.h:977
GenericValue & PushBack(StringRefType value, Allocator &allocator)
Append a constant string reference at the end of the array.
Definition: document.h:1065
GenericValue & AddMember(StringRefType name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:837
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition: document.h:1437
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:1623
number
Definition: rapidjson.h:427
GenericValue & Swap(GenericValue &other)
Exchange the contents of this value with those of other.
Definition: document.h:565
ValueIterator Erase(ConstValueIterator pos)
Remove an element of array by iterator.
Definition: document.h:1111
SizeType GetStringLength() const
Get the length of string.
Definition: document.h:1173
A read-write string stream.
Definition: rapidjson.h:383