15 #ifndef RAPIDJSON_DOCUMENT_H_
16 #define RAPIDJSON_DOCUMENT_H_
21 #include "internal/meta.h"
22 #include "internal/strfunc.h"
27 RAPIDJSON_DIAG_OFF(4127)
28 #elif defined(__GNUC__)
30 RAPIDJSON_DIAG_OFF(effc++)
36 #ifndef RAPIDJSON_HAS_STDSTRING
37 #ifdef RAPIDJSON_DOXYGEN_RUNNING
38 #define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation
40 #define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default
52 #endif // !defined(RAPIDJSON_HAS_STDSTRING)
54 #if RAPIDJSON_HAS_STDSTRING
56 #endif // RAPIDJSON_HAS_STDSTRING
58 #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
62 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
66 RAPIDJSON_NAMESPACE_BEGIN
69 template <
typename Encoding,
typename Allocator>
72 template <
typename Encoding,
typename Allocator,
typename StackAllocator>
81 template <
typename Encoding,
typename Allocator>
90 #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
111 template <
bool Const,
typename Encoding,
typename Allocator>
113 :
public std::iterator<std::random_access_iterator_tag
114 , typename internal::MaybeAddConst<Const,GenericMember<Encoding,Allocator> >::Type> {
121 typedef std::iterator<std::random_access_iterator_tag,ValueType> BaseType;
164 Iterator& operator++(){ ++ptr_;
return *
this; }
165 Iterator& operator--(){ --ptr_;
return *
this; }
166 Iterator operator++(
int){ Iterator old(*
this); ++ptr_;
return old; }
167 Iterator operator--(
int){ Iterator old(*
this); --ptr_;
return old; }
172 Iterator operator+(DifferenceType n)
const {
return Iterator(ptr_+n); }
173 Iterator operator-(DifferenceType n)
const {
return Iterator(ptr_-n); }
175 Iterator& operator+=(DifferenceType n) { ptr_+=n;
return *
this; }
176 Iterator& operator-=(DifferenceType n) { ptr_-=n;
return *
this; }
181 bool operator==(ConstIterator that)
const {
return ptr_ == that.ptr_; }
182 bool operator!=(ConstIterator that)
const {
return ptr_ != that.ptr_; }
183 bool operator<=(ConstIterator that)
const {
return ptr_ <= that.ptr_; }
184 bool operator>=(ConstIterator that)
const {
return ptr_ >= that.ptr_; }
185 bool operator< (ConstIterator that)
const {
return ptr_ < that.ptr_; }
186 bool operator> (ConstIterator that)
const {
return ptr_ > that.ptr_; }
191 Reference operator*()
const {
return *ptr_; }
192 Pointer operator->()
const {
return ptr_; }
193 Reference operator[](DifferenceType n)
const {
return ptr_[n]; }
206 #else // RAPIDJSON_NOMEMBERITERATORCLASS
210 template <
bool Const,
typename Encoding,
typename Allocator>
211 struct GenericMemberIterator;
214 template <
typename Encoding,
typename Allocator>
215 struct GenericMemberIterator<false,Encoding,Allocator> {
217 typedef GenericMember<Encoding,Allocator>* Iterator;
220 template <
typename Encoding,
typename Allocator>
221 struct GenericMemberIterator<true,Encoding,Allocator> {
223 typedef const GenericMember<Encoding,Allocator>* Iterator;
226 #endif // RAPIDJSON_NOMEMBERITERATORCLASS
258 template<
typename CharType>
287 : s(str), length(N-1) {}
322 operator const Ch *()
const {
return s; }
347 template<
typename CharType>
367 template<
typename CharType>
372 #if RAPIDJSON_HAS_STDSTRING
385 template<
typename CharType>
395 template <
typename T,
typename Encoding =
void,
typename Allocator =
void>
396 struct IsGenericValueImpl : FalseType {};
399 template <
typename T>
struct IsGenericValueImpl<T, typename Void<typename T::EncodingType>
::Type, typename Void<typename T::AllocatorType>
::Type>
400 : IsBaseOf<GenericValue<typename T::EncodingType, typename T::AllocatorType>, T>
::Type {};
403 template <
typename T>
struct IsGenericValue : IsGenericValueImpl<T>
::Type {};
420 template <
typename Encoding,
typename Allocator = MemoryPoolAllocator<> >
427 typedef typename Encoding::Ch
Ch;
441 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
444 rhs.flags_ = kNullFlag;
450 GenericValue(
const GenericValue& rhs);
452 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
454 template <
typename StackAllocator>
455 GenericValue(GenericDocument<Encoding,Allocator,StackAllocator>&& rhs);
458 template <
typename StackAllocator>
459 GenericValue& operator=(GenericDocument<Encoding,Allocator,StackAllocator>&& rhs);
470 static const unsigned defaultFlags[7] = {
471 kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
475 flags_ = defaultFlags[type];
479 data_.ss.SetLength(0);
489 template<
typename SourceAllocator >
498 #ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen
499 template <
typename T>
500 explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame<T,bool>))) RAPIDJSON_NOEXCEPT
504 : data_(), flags_(b ? kTrueFlag : kFalseFlag) {
510 explicit GenericValue(
int i) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberIntFlag) {
513 flags_ |= kUintFlag | kUint64Flag;
517 explicit GenericValue(
unsigned u) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUintFlag) {
519 if (!(u & 0x80000000))
520 flags_ |= kIntFlag | kInt64Flag;
524 explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberInt64Flag) {
527 flags_ |= kNumberUint64Flag;
538 explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUint64Flag) {
541 flags_ |= kInt64Flag;
549 explicit GenericValue(
double d) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; }
563 #if RAPIDJSON_HAS_STDSTRING
574 if (Allocator::kNeedFree) {
577 for (
GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v)
579 Allocator::Free(data_.a.elements);
585 Allocator::Free(data_.o.members);
588 case kCopyStringFlag:
589 Allocator::Free(const_cast<Ch*>(data_.s.str));
613 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
616 return *
this = rhs.
Move();
642 template <
typename T>
643 RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer<T>), (
GenericValue&))
655 template <
typename SourceAllocator>
670 temp.RawAssign(*
this);
672 other.RawAssign(temp);
702 template <
typename SourceAllocator>
705 if (GetType() != rhs.GetType())
710 if (data_.o.size != rhs.data_.o.size)
712 for (
ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
713 typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
714 if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
720 if (data_.a.size != rhs.data_.a.size)
722 for (
SizeType i = 0; i < data_.a.size; i++)
723 if ((*
this)[i] != rhs[i])
728 return StringEqual(rhs);
731 if (IsDouble() || rhs.IsDouble()) {
732 double a = GetDouble();
733 double b = rhs.GetDouble();
734 return a >= b && a <= b;
737 return data_.n.u64 == rhs.data_.n.u64;
747 #if RAPIDJSON_HAS_STDSTRING
757 template <
typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>,internal::IsGenericValue<T> >), (
bool)) operator==(const T& rhs)
const {
return *
this ==
GenericValue(rhs); }
762 template <
typename SourceAllocator>
771 template <
typename T> RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (
bool)) operator!=(const T& rhs)
const {
return !(*
this == rhs); }
776 template <
typename T>
friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (
bool)) operator==(const T& lhs, const
GenericValue& rhs) {
return rhs == lhs; }
781 template <
typename T>
friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (
bool)) operator!=(const T& lhs, const
GenericValue& rhs) {
return !(rhs == lhs); }
787 Type GetType()
const {
return static_cast<Type>(flags_ & kTypeMask); }
788 bool IsNull()
const {
return flags_ == kNullFlag; }
789 bool IsFalse()
const {
return flags_ == kFalseFlag; }
790 bool IsTrue()
const {
return flags_ == kTrueFlag; }
791 bool IsBool()
const {
return (flags_ & kBoolFlag) != 0; }
792 bool IsObject()
const {
return flags_ == kObjectFlag; }
793 bool IsArray()
const {
return flags_ == kArrayFlag; }
794 bool IsNumber()
const {
return (flags_ & kNumberFlag) != 0; }
795 bool IsInt()
const {
return (flags_ & kIntFlag) != 0; }
796 bool IsUint()
const {
return (flags_ & kUintFlag) != 0; }
797 bool IsInt64()
const {
return (flags_ & kInt64Flag) != 0; }
798 bool IsUint64()
const {
return (flags_ & kUint64Flag) != 0; }
799 bool IsDouble()
const {
return (flags_ & kDoubleFlag) != 0; }
800 bool IsString()
const {
return (flags_ & kStringFlag) != 0; }
807 GenericValue& SetNull() { this->~GenericValue();
new (
this) GenericValue();
return *
this; }
843 template <
typename T>
848 template <
typename T>
860 template <
typename SourceAllocator>
863 if (member != MemberEnd())
864 return member->value;
871 template <
typename SourceAllocator>
874 #if RAPIDJSON_HAS_STDSTRING
901 bool HasMember(
const Ch* name)
const {
return FindMember(name) != MemberEnd(); }
903 #if RAPIDJSON_HAS_STDSTRING
912 bool HasMember(
const std::basic_string<Ch>& name)
const {
return FindMember(name) != MemberEnd(); }
924 template <
typename SourceAllocator>
941 return FindMember(n);
944 ConstMemberIterator FindMember(
const Ch* name)
const {
return const_cast<GenericValue&
>(*this).
FindMember(name); }
959 template <
typename SourceAllocator>
964 for ( ; member != MemberEnd(); ++member)
965 if (name.StringEqual(member->name))
971 #if RAPIDJSON_HAS_STDSTRING
980 ConstMemberIterator FindMember(
const std::basic_string<Ch>& name)
const {
return FindMember(
StringRef(name)); }
998 if (o.size >= o.capacity) {
999 if (o.capacity == 0) {
1000 o.capacity = kDefaultObjectCapacity;
1001 o.members =
reinterpret_cast<Member*
>(allocator.Malloc(o.capacity *
sizeof(
Member)));
1005 o.capacity += (oldCapacity + 1) / 2;
1006 o.members =
reinterpret_cast<Member*
>(allocator.Realloc(o.members, oldCapacity *
sizeof(
Member), o.capacity *
sizeof(
Member)));
1009 o.members[o.size].
name.RawAssign(name);
1010 o.members[o.size].value.RawAssign(value);
1026 return AddMember(name, v, allocator);
1029 #if RAPIDJSON_HAS_STDSTRING
1041 return AddMember(name, v, allocator);
1062 template <
typename T>
1063 RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (
GenericValue&))
1066 return AddMember(name, v, allocator);
1069 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1071 return AddMember(name, value, allocator);
1073 GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) {
1074 return AddMember(name, value, allocator);
1076 GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) {
1077 return AddMember(name, value, allocator);
1079 GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) {
1080 GenericValue n(name);
1081 return AddMember(n, value, allocator);
1083 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
1098 return AddMember(n, value, allocator);
1112 return AddMember(name, v, allocator);
1132 template <
typename T>
1133 RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (
GenericValue&))
1136 return AddMember(n, value, allocator);
1160 return RemoveMember(n);
1163 #if RAPIDJSON_HAS_STDSTRING
1164 bool RemoveMember(
const std::basic_string<Ch>& name) {
return RemoveMember(
GenericValue(
StringRef(name))); }
1167 template <
typename SourceAllocator>
1168 bool RemoveMember(
const GenericValue<Encoding, SourceAllocator>& name) {
1169 MemberIterator m = FindMember(name);
1170 if (m != MemberEnd()) {
1193 if (data_.o.size > 1 && m != last) {
1215 return EraseMember(pos, pos +1);
1238 std::memmove(&*pos, &*last, (MemberEnd() - last) *
sizeof(
Member));
1239 data_.o.size -= (last - first);
1250 return EraseMember(n);
1253 #if RAPIDJSON_HAS_STDSTRING
1254 bool EraseMember(
const std::basic_string<Ch>& name) {
return EraseMember(
GenericValue(
StringRef(name))); }
1257 template <
typename SourceAllocator>
1258 bool EraseMember(
const GenericValue<Encoding, SourceAllocator>& name) {
1259 MemberIterator m = FindMember(name);
1260 if (m != MemberEnd()) {
1292 for (
SizeType i = 0; i < data_.a.size; ++i)
1293 data_.a.elements[i].~GenericValue();
1305 return data_.a.elements[index];
1330 if (newCapacity > data_.a.capacity) {
1332 data_.a.capacity = newCapacity;
1349 if (data_.a.size >= data_.a.capacity)
1350 Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator);
1351 data_.a.elements[data_.a.size++].RawAssign(value);
1355 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1357 return PushBack(value, allocator);
1359 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
1371 return (*this).template PushBack<StringRefType>(value, allocator);
1391 template <
typename T>
1392 RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (
GenericValue&))
1395 return PushBack(v, allocator);
1405 data_.a.elements[--data_.a.size].~GenericValue();
1417 return Erase(pos, pos + 1);
1438 std::memmove(pos, last, (End() - last) *
sizeof(
GenericValue));
1439 data_.a.size -= (last - first);
1448 int GetInt()
const {
RAPIDJSON_ASSERT(flags_ & kIntFlag);
return data_.n.i.i; }
1449 unsigned GetUint()
const {
RAPIDJSON_ASSERT(flags_ & kUintFlag);
return data_.n.u.u; }
1450 int64_t GetInt64()
const {
RAPIDJSON_ASSERT(flags_ & kInt64Flag);
return data_.n.i64; }
1451 uint64_t GetUint64()
const {
RAPIDJSON_ASSERT(flags_ & kUint64Flag);
return data_.n.u64; }
1453 double GetDouble()
const {
1455 if ((flags_ & kDoubleFlag) != 0)
return data_.n.d;
1456 if ((flags_ & kIntFlag) != 0)
return data_.n.i.i;
1457 if ((flags_ & kUintFlag) != 0)
return data_.n.u.u;
1458 if ((flags_ & kInt64Flag) != 0)
return (
double)data_.n.i64;
1462 GenericValue& SetInt(
int i) { this->~GenericValue();
new (
this) GenericValue(i);
return *
this; }
1463 GenericValue& SetUint(
unsigned u) { this->~GenericValue();
new (
this) GenericValue(u);
return *
this; }
1464 GenericValue& SetInt64(int64_t i64) { this->~GenericValue();
new (
this) GenericValue(i64);
return *
this; }
1465 GenericValue& SetUint64(uint64_t u64) { this->~GenericValue();
new (
this) GenericValue(u64);
return *
this; }
1466 GenericValue& SetDouble(
double d) { this->~GenericValue();
new (
this) GenericValue(d);
return *
this; }
1473 const Ch* GetString()
const {
RAPIDJSON_ASSERT(IsString());
return ((flags_ & kInlineStrFlag) ? data_.ss.str : data_.s.str); }
1515 #if RAPIDJSON_HAS_STDSTRING
1535 template <
typename Handler>
1540 case kTrueType:
return handler.Bool(
true);
1543 if (!handler.StartObject())
1547 if (!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.flags_ & kCopyFlag) != 0))
1549 if (!m->value.Accept(handler))
1552 return handler.EndObject(data_.o.size);
1555 if (!handler.StartArray())
1557 for (
GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v)
1558 if (!v->Accept(handler))
1560 return handler.EndArray(data_.a.size);
1563 return handler.String(GetString(), GetStringLength(), (flags_ & kCopyFlag) != 0);
1567 if (IsInt())
return handler.Int(data_.n.i.i);
1568 else if (IsUint())
return handler.Uint(data_.n.u.u);
1569 else if (IsInt64())
return handler.Int64(data_.n.i64);
1570 else if (IsUint64())
return handler.Uint64(data_.n.u64);
1571 else return handler.Double(data_.n.d);
1576 template <
typename,
typename>
friend class GenericValue;
1581 kNumberFlag = 0x200,
1584 kInt64Flag = 0x1000,
1585 kUint64Flag = 0x2000,
1586 kDoubleFlag = 0x4000,
1587 kStringFlag = 0x100000,
1588 kCopyFlag = 0x200000,
1589 kInlineStrFlag = 0x400000,
1595 kNumberIntFlag =
kNumberType | kNumberFlag | kIntFlag | kInt64Flag,
1596 kNumberUintFlag =
kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag,
1597 kNumberInt64Flag =
kNumberType | kNumberFlag | kInt64Flag,
1598 kNumberUint64Flag =
kNumberType | kNumberFlag | kUint64Flag,
1599 kNumberDoubleFlag =
kNumberType | kNumberFlag | kDoubleFlag,
1600 kNumberAnyFlag =
kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag,
1602 kCopyStringFlag =
kStringType | kStringFlag | kCopyFlag,
1603 kShortStringFlag =
kStringType | kStringFlag | kCopyFlag | kInlineStrFlag,
1610 static const SizeType kDefaultArrayCapacity = 16;
1611 static const SizeType kDefaultObjectCapacity = 16;
1627 struct ShortString {
1628 enum { MaxChars =
sizeof(String) /
sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize };
1631 inline static bool Usable(
SizeType len) {
return (MaxSize >= len); }
1632 inline void SetLength(
SizeType len) { str[LenPos] = (Ch)(MaxSize - len); }
1633 inline SizeType GetLength()
const {
return (
SizeType)(MaxSize - str[LenPos]); }
1638 #if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN
1669 GenericValue* elements;
1683 void SetArrayRaw(GenericValue* values,
SizeType count, Allocator& allocator) {
1684 flags_ = kArrayFlag;
1686 data_.a.elements = (GenericValue*)allocator.Malloc(count *
sizeof(GenericValue));
1687 std::memcpy(data_.a.elements, values, count *
sizeof(GenericValue));
1690 data_.a.elements = NULL;
1691 data_.a.size = data_.a.capacity = count;
1695 void SetObjectRaw(Member* members,
SizeType count, Allocator& allocator) {
1696 flags_ = kObjectFlag;
1698 data_.o.members = (Member*)allocator.Malloc(count *
sizeof(Member));
1699 std::memcpy(data_.o.members, members, count *
sizeof(Member));
1702 data_.o.members = NULL;
1703 data_.o.size = data_.o.capacity = count;
1707 void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT {
1708 flags_ = kConstStringFlag;
1710 data_.s.length = s.length;
1714 void SetStringRaw(StringRefType s, Allocator& allocator) {
1716 if(ShortString::Usable(s.length)) {
1717 flags_ = kShortStringFlag;
1718 data_.ss.SetLength(s.length);
1721 flags_ = kCopyStringFlag;
1722 data_.s.length = s.length;
1723 str = (Ch *)allocator.Malloc((s.length + 1) *
sizeof(Ch));
1726 std::memcpy(str, s, s.length *
sizeof(Ch));
1727 str[s.length] =
'\0';
1731 void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
1733 flags_ = rhs.flags_;
1734 rhs.flags_ = kNullFlag;
1737 template <
typename SourceAllocator>
1738 bool StringEqual(
const GenericValue<Encoding, SourceAllocator>& rhs)
const {
1742 const SizeType len1 = GetStringLength();
1743 const SizeType len2 = rhs.GetStringLength();
1744 if(len1 != len2) {
return false; }
1746 const Ch*
const str1 = GetString();
1747 const Ch*
const str2 = rhs.GetString();
1748 if(str1 == str2) {
return true; }
1750 return (std::memcmp(str1, str2,
sizeof(Ch) * len1) == 0);
1771 template <
typename Encoding,
typename Allocator = MemoryPoolAllocator<>,
typename StackAllocator = CrtAllocator>
1774 typedef typename Encoding::Ch
Ch;
1799 allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
1805 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1808 : ValueType(std::forward<ValueType>(rhs)),
1809 allocator_(rhs.allocator_),
1810 ownAllocator_(rhs.ownAllocator_),
1811 stack_(std::move(rhs.stack_)),
1812 parseResult_(rhs.parseResult_)
1815 rhs.ownAllocator_ = 0;
1820 ~GenericDocument() {
1824 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1826 GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT
1830 ValueType::operator=(std::forward<ValueType>(rhs));
1835 allocator_ = rhs.allocator_;
1836 ownAllocator_ = rhs.ownAllocator_;
1837 stack_ = std::move(rhs.stack_);
1838 parseResult_ = rhs.parseResult_;
1841 rhs.ownAllocator_ = 0;
1842 rhs.parseResult_ = ParseResult();
1855 ValueType::Swap(rhs);
1856 stack_.Swap(rhs.stack_);
1857 internal::Swap(allocator_, rhs.allocator_);
1858 internal::Swap(ownAllocator_, rhs.ownAllocator_);
1859 internal::Swap(parseResult_, rhs.parseResult_);
1887 template <
unsigned parseFlags,
typename SourceEncoding,
typename InputStream>
1889 ValueType::SetNull();
1891 ClearStackOnExit scope(*
this);
1892 parseResult_ = reader.template Parse<parseFlags>(is, *
this);
1895 this->RawAssign(*stack_.template Pop<ValueType>(1));
1906 template <
unsigned parseFlags,
typename InputStream>
1908 return ParseStream<parseFlags, Encoding, InputStream>(is);
1916 template <
typename InputStream>
1918 return ParseStream<kParseDefaultFlags, Encoding, InputStream>(is);
1930 template <
unsigned parseFlags>
1933 return ParseStream<parseFlags | kParseInsituFlag>(s);
1941 return ParseInsitu<kParseDefaultFlags>(str);
1953 template <
unsigned parseFlags,
typename SourceEncoding>
1957 return ParseStream<parseFlags, SourceEncoding>(s);
1964 template <
unsigned parseFlags>
1966 return Parse<parseFlags, Encoding>(str);
1973 return Parse<kParseDefaultFlags>(str);
1999 struct ClearStackOnExit {
2001 ~ClearStackOnExit() { d_.ClearStack(); }
2003 ClearStackOnExit(
const ClearStackOnExit&);
2004 ClearStackOnExit& operator=(
const ClearStackOnExit&);
2005 GenericDocument& d_;
2009 template <
typename,
typename,
typename>
friend class GenericReader;
2010 template <
typename,
typename>
friend class GenericValue;
2013 bool Null() {
new (stack_.template Push<ValueType>()) ValueType();
return true; }
2014 bool Bool(
bool b) {
new (stack_.template Push<ValueType>()) ValueType(b);
return true; }
2015 bool Int(
int i) {
new (stack_.template Push<ValueType>()) ValueType(i);
return true; }
2016 bool Uint(
unsigned i) {
new (stack_.template Push<ValueType>()) ValueType(i);
return true; }
2017 bool Int64(int64_t i) {
new (stack_.template Push<ValueType>()) ValueType(i);
return true; }
2018 bool Uint64(uint64_t i) {
new (stack_.template Push<ValueType>()) ValueType(i);
return true; }
2019 bool Double(
double d) {
new (stack_.template Push<ValueType>()) ValueType(d);
return true; }
2021 bool String(
const Ch* str,
SizeType length,
bool copy) {
2023 new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator());
2025 new (stack_.template Push<ValueType>()) ValueType(str, length);
2029 bool StartObject() {
new (stack_.template Push<ValueType>()) ValueType(
kObjectType);
return true; }
2031 bool Key(
const Ch* str,
SizeType length,
bool copy) {
return String(str, length, copy); }
2033 bool EndObject(
SizeType memberCount) {
2034 typename ValueType::Member* members = stack_.template Pop<typename ValueType::Member>(memberCount);
2035 stack_.template Top<ValueType>()->SetObjectRaw(members, (
SizeType)memberCount, GetAllocator());
2039 bool StartArray() {
new (stack_.template Push<ValueType>()) ValueType(
kArrayType);
return true; }
2041 bool EndArray(
SizeType elementCount) {
2042 ValueType* elements = stack_.template Pop<ValueType>(elementCount);
2043 stack_.template Top<ValueType>()->SetArrayRaw(elements, elementCount, GetAllocator());
2049 GenericDocument(
const GenericDocument&);
2051 GenericDocument& operator=(
const GenericDocument&);
2054 if (Allocator::kNeedFree)
2055 while (stack_.GetSize() > 0)
2056 (stack_.template Pop<ValueType>(1))->~ValueType();
2059 stack_.ShrinkToFit();
2066 static const size_t kDefaultStackCapacity = 1024;
2067 Allocator* allocator_;
2068 Allocator* ownAllocator_;
2069 internal::Stack<StackAllocator> stack_;
2070 ParseResult parseResult_;
2077 template <
typename Encoding,
typename Allocator>
2078 template <
typename SourceAllocator>
2082 switch (rhs.GetType()) {
2087 RawAssign(*d.stack_.template Pop<GenericValue>(1));
2091 if (rhs.flags_ == kConstStringFlag) {
2092 flags_ = rhs.flags_;
2093 data_ = *
reinterpret_cast<const Data*
>(&rhs.data_);
2095 SetStringRaw(
StringRef(rhs.GetString(), rhs.GetStringLength()), allocator);
2099 flags_ = rhs.flags_;
2100 data_ = *
reinterpret_cast<const Data*
>(&rhs.data_);
2104 RAPIDJSON_NAMESPACE_END
2106 #if defined(_MSC_VER) || defined(__GNUC__)
2110 #endif // RAPIDJSON_DOCUMENT_H_
BaseType::difference_type DifferenceType
Signed integer type (e.g. ptrdiff_t)
Definition: document.h:136
GenericValue< Encoding, Allocator > ValueType
Value type of the document.
Definition: document.h:1775
GenericValue(unsigned u) RAPIDJSON_NOEXCEPT
Constructor for unsigned value.
Definition: document.h:517
true
Definition: rapidjson.h:645
ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)
Remove elements in the range [first, last) of the array.
Definition: document.h:1428
Definition: document.h:1639
Read-only string stream.
Definition: rapidjson.h:571
Concept for receiving events from GenericReader upon parsing. The functions return true if no error o...
GenericValue & Move() RAPIDJSON_NOEXCEPT
Prepare Value for move semantics.
Definition: document.h:692
ValueIterator Begin()
Element iterator.
Definition: document.h:1311
GenericMemberIterator< true, Encoding, Allocator > ConstIterator
Constant iterator type.
Definition: document.h:127
DifferenceType operator-(ConstIterator that) const
Distance.
Definition: document.h:197
GenericValue(const Ch *s, SizeType length, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:558
GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT
Constructor for int64_t value.
Definition: document.h:524
~GenericValue()
Destructor.
Definition: document.h:573
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition: reader.h:374
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with kParseDefaultFlags)
Definition: document.h:1917
GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:555
GenericValue & SetString(const std::basic_string< Ch > &s, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1523
GenericValue & SetObject()
Set this value as an empty object.
Definition: document.h:826
GenericStringRef< Ch > StringRefType
Reference to a constant string.
Definition: document.h:428
GenericMemberIterator Iterator
Iterator type itself.
Definition: document.h:125
ConstMemberIterator MemberBegin() const
Const member iterator.
Definition: document.h:882
GenericValue & operator=(GenericValue &rhs) RAPIDJSON_NOEXCEPT
Assignment with move semantics.
Definition: document.h:606
ConstMemberIterator MemberEnd() const
Const past-the-end member iterator.
Definition: document.h:885
unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:322
GenericMemberIterator< false, Encoding, Allocator > NonConstIterator
Non-constant iterator type.
Definition: document.h:129
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:1776
MemberIterator FindMember(const GenericValue< Encoding, SourceAllocator > &name)
Find member by name.
Definition: document.h:960
bool ObjectEmpty() const
Check whether the object is empty.
Definition: document.h:832
false
Definition: rapidjson.h:644
ParseErrorCode
Error code of parsing.
Definition: error.h:59
bool operator!=(const GenericValue< Encoding, SourceAllocator > &rhs) const
Not-equal-to operator.
Definition: document.h:763
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:375
GenericValue(const Ch *s, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:561
bool RemoveMember(const Ch *name)
Remove a member in object by its name.
Definition: document.h:1158
bool Accept(Handler &handler) const
Generate events of this value to a Handler.
Definition: document.h:1536
GenericStringRef< CharType > StringRef(const std::basic_string< CharType > &str)
Mark a string object as constant string.
Definition: document.h:386
GenericValue & PopBack()
Remove the last element in the array.
Definition: document.h:1402
GenericValue & Reserve(SizeType newCapacity, Allocator &allocator)
Request the array to have enough capacity to store elements.
Definition: document.h:1328
ValueIterator End()
Past-the-end element iterator
Definition: document.h:1314
bool EraseMember(const Ch *name)
Erase a member in object by its name.
Definition: document.h:1248
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string (with kParseDefaultFlags)
Definition: document.h:1972
bool GetBool() const
Set boolean value.
Definition: document.h:814
MemberIterator RemoveMember(MemberIterator m)
Remove a member in object by iterator.
Definition: document.h:1186
MemberIterator MemberBegin()
Member iterator.
Definition: document.h:888
friend void swap(GenericValue &a, GenericValue &b) RAPIDJSON_NOEXCEPT
free-standing swap function helper
Definition: document.h:688
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:426
GenericValue & AddMember(StringRefType name, StringRefType value, Allocator &allocator)
Add a constant string value as member (name-value pair) to the object.
Definition: document.h:1110
GenericValue & AddMember(GenericValue &name, std::basic_string< Ch > &value, Allocator &allocator)
Add a string object as member (name-value pair) to the object.
Definition: document.h:1039
GenericValue & operator[](const std::basic_string< Ch > &name)
Get a value from an object associated with name (string object).
Definition: document.h:876
GenericValue(const Ch *s, SizeType length) RAPIDJSON_NOEXCEPT
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:552
GenericValue & operator[](const GenericValue< Encoding, SourceAllocator > &name)
Get a value from an object associated with the name.
Definition: document.h:861
GenericValue & SetString(const Ch *s, SizeType length, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1505
MemberIterator EraseMember(ConstMemberIterator pos)
Remove a member from an object by iterator.
Definition: document.h:1214
GenericStringRef(const CharType *str, SizeType len)
Create constant string reference from pointer and length.
Definition: document.h:318
bool operator==(const std::basic_string< Ch > &rhs) const
Equal-to operator with string object.
Definition: document.h:751
GenericPointer< Value > Pointer
GenericPointer for Value (UTF-8, default allocator).
Definition: pointer.h:1012
Name-value pair in a JSON object value.
Definition: document.h:82
GenericValue & SetString(const Ch *s, SizeType length)
Set this value as a string without copying source string.
Definition: document.h:1488
GenericStringRef< CharType > StringRef(const CharType *str)
Mark a character pointer as constant string.
Definition: document.h:348
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string (with kParseDefaultFlags)
Definition: document.h:1940
SizeType Size() const
Get the number of elements in array.
Definition: document.h:1278
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:325
BaseType::reference Reference
Reference to (const) GenericMember.
Definition: document.h:134
const GenericValue * ConstValueIterator
Constant value iterator for iterating in array.
Definition: document.h:432
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: document.h:1987
Concept for encoding of Unicode characters.
MemberIterator FindMember(const std::basic_string< Ch > &name)
Find member by string object name.
Definition: document.h:979
GenericValue & AddMember(GenericValue &name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:993
Result of parsing (wraps ParseErrorCode)
Definition: error.h:101
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition: document.h:1888
MemberIterator FindMember(const Ch *name)
Find member by name.
Definition: document.h:939
ConstValueIterator Begin() const
Constant element iterator.
Definition: document.h:1317
void Clear()
Remove all elements in the array.
Definition: document.h:1290
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string.
Definition: document.h:1965
Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.
Definition: pointer.h:71
Type
Type of JSON value.
Definition: rapidjson.h:642
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:1774
GenericDocument(Type type, Allocator *allocator=0, size_t stackCapacity=kDefaultStackCapacity, StackAllocator *stackAllocator=0)
Constructor.
Definition: document.h:1785
GenericValue & operator[](SizeType index)
Get an element from array by index.
Definition: document.h:1302
BaseType::pointer Pointer
Pointer to (const) GenericMember.
Definition: document.h:132
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
Definition: document.h:1984
object
Definition: rapidjson.h:646
GenericValue & PushBack(GenericValue &value, Allocator &allocator)
Append a GenericValue at the end of the array.
Definition: document.h:1347
GenericValue & Swap(GenericValue &other) RAPIDJSON_NOEXCEPT
Exchange the contents of this value with those of other.
Definition: document.h:668
MemberIterator MemberEnd()
Past-the-end member iterator
Definition: document.h:891
GenericStringRef(const CharType(&str)[N]) RAPIDJSON_NOEXCEPT
Create string reference from const character array.
Definition: document.h:286
bool operator!=(const Ch *rhs) const
Not-equal-to operator with const C-string pointer.
Definition: document.h:766
GenericValue(const std::basic_string< Ch > &s, Allocator &allocator)
Constructor for copy-string from a string object (i.e. do make a copy of string)
Definition: document.h:567
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:480
GenericValue(Type type) RAPIDJSON_NOEXCEPT
Constructor with JSON value type.
Definition: document.h:469
A document for parsing JSON text as DOM.
Definition: document.h:73
array
Definition: rapidjson.h:647
GenericValue * ValueIterator
Value iterator for iterating in array.
Definition: document.h:431
bool operator==(const GenericValue< Encoding, SourceAllocator > &rhs) const
Equal-to operator.
Definition: document.h:703
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:484
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:427
Definition: document.h:1643
GenericMemberIterator< false, Encoding, Allocator >::Iterator MemberIterator
Member iterator for iterating in object.
Definition: document.h:429
size_t GetStackCapacity() const
Get the capacity of stack in bytes.
Definition: document.h:1995
GenericValue(bool b) RAPIDJSON_NOEXCEPT
Constructor for boolean value.
Definition: document.h:502
GenericValue(int i) RAPIDJSON_NOEXCEPT
Constructor for int value.
Definition: document.h:510
GenericValue< Encoding, Allocator > value
value of member.
Definition: document.h:84
GenericValue() RAPIDJSON_NOEXCEPT
Default constructor creates a null value.
Definition: document.h:439
GenericStringRef(const CharType *str)
Explicitly create string reference from const character pointer.
Definition: document.h:308
friend void swap(GenericDocument &a, GenericDocument &b) RAPIDJSON_NOEXCEPT
free-standing swap function helper
Definition: document.h:1875
null
Definition: rapidjson.h:643
GenericDocument(Allocator *allocator=0, size_t stackCapacity=kDefaultStackCapacity, StackAllocator *stackAllocator=0)
Constructor.
Definition: document.h:1798
GenericValue< Encoding, Allocator > name
name of member (must be a string)
Definition: document.h:83
string
Definition: rapidjson.h:648
Allocator & GetAllocator()
Get the allocator of this document.
Definition: document.h:1992
GenericMember< Encoding, Allocator > Member
Name-value pair in an object.
Definition: document.h:424
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream.
Definition: document.h:1907
SizeType MemberCount() const
Get the number of members in the object.
Definition: document.h:829
CharType Ch
character type of the string
Definition: document.h:260
Encoding EncodingType
Encoding type from template parameter.
Definition: document.h:425
bool HasMember(const GenericValue< Encoding, SourceAllocator > &name) const
Check whether a member exists in the object with GenericValue name.
Definition: document.h:925
GenericValue & SetString(StringRefType s)
Set this value as a string without copying source string.
Definition: document.h:1495
void RemoveAllMembers()
Remove all members in the object.
Definition: document.h:1143
GenericMemberIterator(const NonConstIterator &it)
Iterator conversions to more const.
Definition: document.h:160
GenericValue & operator=(StringRefType str) RAPIDJSON_NOEXCEPT
Assignment of constant string reference (no copy)
Definition: document.h:625
GenericDocument & Swap(GenericDocument &rhs) RAPIDJSON_NOEXCEPT
Exchange the contents of this document with those of another.
Definition: document.h:1854
bool operator==(const Ch *rhs) const
Equal-to operator with const C-string pointer.
Definition: document.h:745
In-situ(destructive) parsing.
Definition: reader.h:138
bool HasMember(const Ch *name) const
Check whether a member exists in the object.
Definition: document.h:901
bool HasMember(const std::basic_string< Ch > &name) const
Check whether a member exists in the object with string object.
Definition: document.h:912
bool Empty() const
Check whether the array is empty.
Definition: document.h:1284
Reference to a constant string (not taking a copy)
Definition: document.h:259
GenericValue(double d) RAPIDJSON_NOEXCEPT
Constructor for double value.
Definition: document.h:549
GenericValue & SetBool(bool b)
Definition: document.h:817
Concept for allocating, resizing and freeing memory block.
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:70
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:1758
GenericValue & CopyFrom(const GenericValue< Encoding, SourceAllocator > &rhs, Allocator &allocator)
Deep-copy assignment from Value.
Definition: document.h:656
GenericValue & SetArray()
Set this value as an empty array.
Definition: document.h:1275
(Constant) member iterator for a JSON object value
Definition: document.h:112
GenericValue & SetString(const Ch *s, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1513
MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last)
Remove members in the range [first, last) from an object.
Definition: document.h:1227
const Ch *const s
plain CharType pointer
Definition: document.h:324
GenericMemberIterator< true, Encoding, Allocator >::Iterator ConstMemberIterator
Constant member iterator for iterating in object.
Definition: document.h:430
ConstValueIterator End() const
Constant past-the-end element iterator.
Definition: document.h:1320
GenericMemberIterator()
Default constructor (singular value)
Definition: document.h:142
SizeType Capacity() const
Get the capacity of array.
Definition: document.h:1281
GenericValue & AddMember(GenericValue &name, StringRefType value, Allocator &allocator)
Add a constant string value as member (name-value pair) to the object.
Definition: document.h:1024
GenericValue< Encoding, Allocator > ValueType
Value type of itself.
Definition: document.h:433
GenericValue & PushBack(StringRefType value, Allocator &allocator)
Append a constant string reference at the end of the array.
Definition: document.h:1370
bool HasParseError() const
Whether a parse error has occured in the last parsing.
Definition: document.h:1981
GenericValue & AddMember(StringRefType name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:1096
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2074
GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT
Constructor for uint64_t value.
Definition: document.h:538
number
Definition: rapidjson.h:649
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string.
Definition: document.h:1931
ValueIterator Erase(ConstValueIterator pos)
Remove an element of array by iterator.
Definition: document.h:1416
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
Definition: document.h:1954
SizeType GetStringLength() const
Get the length of string.
Definition: document.h:1478
A read-write string stream.
Definition: rapidjson.h:605