15 #ifndef RAPIDJSON_POINTER_H_ 16 #define RAPIDJSON_POINTER_H_ 23 RAPIDJSON_DIAG_OFF(
switch-
enum)
28 RAPIDJSON_DIAG_OFF(4512)
80 template <
typename ValueType,
typename Allocator = CrtAllocator>
84 typedef typename ValueType::Ch
Ch;
120 #if RAPIDJSON_HAS_STDSTRING 128 Parse(source.c_str(), source.size());
140 Parse(source, length);
215 std::memcpy(p, token.
name, (token.
length + 1) *
sizeof(Ch));
240 template <
typename T>
246 #if RAPIDJSON_HAS_STDSTRING 270 if (
sizeof(
Ch) == 1) {
271 Token token = {
reinterpret_cast<Ch*
>(buffer), length, index };
276 for (
size_t i = 0; i <=
length; i++)
277 name[i] = static_cast<Ch>(buffer[i]);
290 if (token.IsString())
314 Allocator& GetAllocator() {
return *
allocator_; }
323 size_t GetTokenCount()
const {
return tokenCount_; }
354 bool operator!=(
const GenericPointer& rhs)
const {
return !(*
this == rhs); }
366 template<
typename OutputStream>
368 return Stringify<false, OutputStream>(os);
376 template<
typename OutputStream>
377 bool StringifyUriFragment(OutputStream& os)
const {
378 return Stringify<true, OutputStream>(os);
401 ValueType& Create(ValueType& root,
typename ValueType::AllocatorType&
allocator,
bool* alreadyExist = 0)
const {
403 ValueType* v = &root;
406 if (v->IsArray() && t->name[0] ==
'-' && t->length == 1) {
407 v->PushBack(ValueType().Move(), allocator);
408 v = &((*v)[v->Size() - 1]);
412 if (t->index == kPointerInvalidIndex) {
417 if (!v->IsArray() && !v->IsObject())
422 if (t->index >= v->Size()) {
423 v->Reserve(t->index + 1, allocator);
424 while (t->index >= v->Size())
425 v->PushBack(ValueType().Move(), allocator);
428 v = &((*v)[t->index]);
432 if (m == v->MemberEnd()) {
433 v->AddMember(ValueType(t->name, t->length, allocator).Move(), ValueType().Move(),
allocator);
434 v = &(--v->MemberEnd())->
value;
444 *alreadyExist = exist;
455 template <
typename stackAllocator>
457 return Create(document, document.
GetAllocator(), alreadyExist);
479 ValueType* Get(ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
481 ValueType* v = &root;
483 switch (v->GetType()) {
487 if (m == v->MemberEnd())
493 if (t->index == kPointerInvalidIndex || t->index >= v->Size())
495 v = &((*v)[t->index]);
502 if (unresolvedTokenIndex)
503 *unresolvedTokenIndex =
static_cast<size_t>(t -
tokens_);
514 const ValueType* Get(
const ValueType& root,
size_t* unresolvedTokenIndex = 0)
const {
515 return Get(const_cast<ValueType&>(root), unresolvedTokenIndex);
533 ValueType& GetWithDefault(ValueType& root,
const ValueType&
defaultValue,
typename ValueType::AllocatorType& allocator)
const {
535 Value& v = Create(root, allocator, &alreadyExist);
536 return alreadyExist ? v : v.CopyFrom(defaultValue, allocator);
540 ValueType& GetWithDefault(ValueType& root,
const Ch* defaultValue,
typename ValueType::AllocatorType& allocator)
const {
542 Value& v = Create(root, allocator, &alreadyExist);
543 return alreadyExist ? v : v.SetString(defaultValue, allocator);
546 #if RAPIDJSON_HAS_STDSTRING 547 ValueType& GetWithDefault(ValueType& root,
const std::basic_string<Ch>& defaultValue,
typename ValueType::AllocatorType& allocator)
const {
550 Value& v = Create(root, allocator, &alreadyExist);
551 return alreadyExist ? v : v.SetString(defaultValue, allocator);
559 template <
typename T>
561 GetWithDefault(ValueType& root,
T defaultValue,
typename ValueType::AllocatorType& allocator)
const {
562 return GetWithDefault(root, ValueType(defaultValue).Move(), allocator);
566 template <
typename stackAllocator>
568 return GetWithDefault(document, defaultValue, document.
GetAllocator());
572 template <
typename stackAllocator>
574 return GetWithDefault(document, defaultValue, document.
GetAllocator());
577 #if RAPIDJSON_HAS_STDSTRING 578 template <
typename stackAllocator>
581 return GetWithDefault(document, defaultValue, document.
GetAllocator());
589 template <
typename T,
typename stackAllocator>
592 return GetWithDefault(document, defaultValue, document.GetAllocator());
610 ValueType&
Set(ValueType& root, ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
611 return Create(root, allocator) =
value;
615 ValueType&
Set(ValueType& root,
const ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
616 return Create(root, allocator).CopyFrom(value, allocator);
621 return Create(root, allocator) = ValueType(value, allocator).Move();
624 #if RAPIDJSON_HAS_STDSTRING 625 ValueType&
Set(ValueType& root,
const std::basic_string<Ch>&
value,
typename ValueType::AllocatorType&
allocator)
const {
627 return Create(root, allocator) = ValueType(value, allocator).Move();
635 template <
typename T>
637 Set(ValueType& root,
T value, typename ValueType::AllocatorType& allocator)
const {
638 return Create(root, allocator) = ValueType(value).Move();
642 template <
typename stackAllocator>
644 return Create(document) =
value;
648 template <
typename stackAllocator>
650 return Create(document).CopyFrom(value, document.
GetAllocator());
654 template <
typename stackAllocator>
656 return Create(document) = ValueType(value, document.
GetAllocator()).Move();
659 #if RAPIDJSON_HAS_STDSTRING 660 template <
typename stackAllocator>
663 return Create(document) = ValueType(value, document.
GetAllocator()).Move();
671 template <
typename T,
typename stackAllocator>
674 return Create(document) =
value;
692 ValueType&
Swap(ValueType& root, ValueType&
value,
typename ValueType::AllocatorType&
allocator)
const {
693 return Create(root, allocator).Swap(value);
697 template <
typename stackAllocator>
699 return Create(document).Swap(value);
716 ValueType* v = &root;
719 switch (v->GetType()) {
723 if (m == v->MemberEnd())
731 v = &((*v)[t->index]);
738 switch (v->GetType()) {
744 v->Erase(v->Begin() + last->
index);
765 nameBufferSize += t->length;
773 if (nameBufferSize > 0) {
791 return !((c >=
'0' && c <= '9') || (c >=
'A' && c <='Z') || (c >=
'a' && c <=
'z') || c ==
'-' || c ==
'.' || c ==
'_' || c ==
'~');
795 #ifndef __clang__ // -Wdocumentation 813 for (
const Ch*
s = source;
s != source +
length;
s++)
822 bool uriFragment =
false;
823 if (source[i] ==
'#') {
828 if (i != length && source[i] !=
'/') {
838 bool isNumber =
true;
840 while (i < length && source[i] !=
'/') {
852 size_t len = os.
PutEnd(begin);
875 if (c ==
'0') c =
'~';
876 else if (c ==
'1') c =
'/';
890 if (c < '0' || c >
'9')
901 if (isNumber && token->
length > 1 && token->
name[0] ==
'0')
907 for (
size_t j = 0; j < token->
length; j++) {
940 template<
bool uriFragment,
typename OutputStream>
949 for (
size_t j = 0; j < t->length; j++) {
965 j += source.
Tell() - 1;
982 typedef typename ValueType::Ch
Ch;
992 if (*src_ !=
'%' || src_ + 3 > end_) {
998 for (
int j = 0; j < 2; j++) {
999 c =
static_cast<Ch
>(c << 4);
1001 if (h >=
'0' && h <=
'9') c =
static_cast<Ch
>(c + h -
'0');
1002 else if (h >=
'A' && h <=
'F') c =
static_cast<Ch
>(c + h -
'A' + 10);
1003 else if (h >=
'a' && h <=
'f') c =
static_cast<Ch
>(c + h -
'a' + 10);
1013 size_t Tell()
const {
return static_cast<size_t>(src_ - head_); }
1024 template <
typename OutputStream>
1029 unsigned char u =
static_cast<unsigned char>(c);
1030 static const char hexDigits[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F' };
1032 os_.Put(static_cast<typename OutputStream::Ch>(hexDigits[u >> 4]));
1033 os_.Put(static_cast<typename OutputStream::Ch>(hexDigits[u & 15]));
1056 template <
typename T>
1058 return pointer.Create(root, a);
1061 template <
typename T,
typename CharType,
size_t N>
1068 template <
typename DocumentType>
1070 return pointer.Create(document);
1073 template <
typename DocumentType,
typename CharType,
size_t N>
1080 template <
typename T>
1082 return pointer.Get(root, unresolvedTokenIndex);
1085 template <
typename T>
1087 return pointer.Get(root, unresolvedTokenIndex);
1090 template <
typename T,
typename CharType,
size_t N>
1095 template <
typename T,
typename CharType,
size_t N>
1102 template <
typename T>
1104 return pointer.GetWithDefault(root, defaultValue, a);
1107 template <
typename T>
1109 return pointer.GetWithDefault(root, defaultValue, a);
1112 #if RAPIDJSON_HAS_STDSTRING 1113 template <
typename T>
1115 return pointer.GetWithDefault(root, defaultValue, a);
1119 template <
typename T,
typename T2>
1122 return pointer.GetWithDefault(root, defaultValue, a);
1125 template <
typename T,
typename CharType,
size_t N>
1130 template <
typename T,
typename CharType,
size_t N>
1135 #if RAPIDJSON_HAS_STDSTRING 1136 template <
typename T,
typename CharType,
size_t N>
1142 template <
typename T,
typename CharType,
size_t N,
typename T2>
1150 template <
typename DocumentType>
1152 return pointer.GetWithDefault(document, defaultValue);
1155 template <
typename DocumentType>
1157 return pointer.GetWithDefault(document, defaultValue);
1160 #if RAPIDJSON_HAS_STDSTRING 1161 template <
typename DocumentType>
1163 return pointer.GetWithDefault(document, defaultValue);
1167 template <
typename DocumentType,
typename T2>
1170 return pointer.GetWithDefault(document, defaultValue);
1173 template <
typename DocumentType,
typename CharType,
size_t N>
1178 template <
typename DocumentType,
typename CharType,
size_t N>
1183 #if RAPIDJSON_HAS_STDSTRING 1184 template <
typename DocumentType,
typename CharType,
size_t N>
1185 typename DocumentType::ValueType&
GetValueByPointerWithDefault(DocumentType& document,
const CharType(&
source)[N],
const std::basic_string<typename DocumentType::Ch>& defaultValue) {
1190 template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1198 template <
typename T>
1200 return pointer.
Set(root, value, a);
1203 template <
typename T>
1205 return pointer.
Set(root, value, a);
1208 template <
typename T>
1210 return pointer.
Set(root, value, a);
1213 #if RAPIDJSON_HAS_STDSTRING 1214 template <
typename T>
1216 return pointer.
Set(root, value, a);
1220 template <
typename T,
typename T2>
1223 return pointer.Set(root, value, a);
1226 template <
typename T,
typename CharType,
size_t N>
1231 template <
typename T,
typename CharType,
size_t N>
1236 template <
typename T,
typename CharType,
size_t N>
1241 #if RAPIDJSON_HAS_STDSTRING 1242 template <
typename T,
typename CharType,
size_t N>
1243 typename T::ValueType&
SetValueByPointer(
T& root,
const CharType(&
source)[N],
const std::basic_string<typename T::Ch>&
value,
typename T::AllocatorType&
a) {
1248 template <
typename T,
typename CharType,
size_t N,
typename T2>
1256 template <
typename DocumentType>
1258 return pointer.
Set(document, value);
1261 template <
typename DocumentType>
1263 return pointer.
Set(document, value);
1266 template <
typename DocumentType>
1268 return pointer.
Set(document, value);
1271 #if RAPIDJSON_HAS_STDSTRING 1272 template <
typename DocumentType>
1274 return pointer.
Set(document, value);
1278 template <
typename DocumentType,
typename T2>
1281 return pointer.
Set(document, value);
1284 template <
typename DocumentType,
typename CharType,
size_t N>
1285 typename DocumentType::ValueType&
SetValueByPointer(DocumentType& document,
const CharType(&
source)[N],
typename DocumentType::ValueType& value) {
1289 template <
typename DocumentType,
typename CharType,
size_t N>
1290 typename DocumentType::ValueType&
SetValueByPointer(DocumentType& document,
const CharType(&
source)[N],
const typename DocumentType::ValueType& value) {
1294 template <
typename DocumentType,
typename CharType,
size_t N>
1295 typename DocumentType::ValueType&
SetValueByPointer(DocumentType& document,
const CharType(&
source)[N],
const typename DocumentType::Ch* value) {
1299 #if RAPIDJSON_HAS_STDSTRING 1300 template <
typename DocumentType,
typename CharType,
size_t N>
1301 typename DocumentType::ValueType&
SetValueByPointer(DocumentType& document,
const CharType(&
source)[N],
const std::basic_string<typename DocumentType::Ch>& value) {
1306 template <
typename DocumentType,
typename CharType,
size_t N,
typename T2>
1314 template <
typename T>
1316 return pointer.
Swap(root, value, a);
1319 template <
typename T,
typename CharType,
size_t N>
1320 typename T::ValueType&
SwapValueByPointer(
T& root,
const CharType(&
source)[N],
typename T::ValueType& value,
typename T::AllocatorType& a) {
1324 template <
typename DocumentType>
1326 return pointer.
Swap(document, value);
1329 template <
typename DocumentType,
typename CharType,
size_t N>
1330 typename DocumentType::ValueType&
SwapValueByPointer(DocumentType& document,
const CharType(&
source)[N],
typename DocumentType::ValueType& value) {
1336 template <
typename T>
1338 return pointer.
Erase(root);
1341 template <
typename T,
typename CharType,
size_t N>
1358 #endif // RAPIDJSON_POINTER_H_ GenericPointer(const Token *tokens, size_t tokenCount)
Constructor with user-supplied tokens.
ValueType & Set(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, const ValueType &value) const
Set a value in a document, with copy semantics.
T::ValueType & GetValueByPointerWithDefault(T &root, const GenericPointer< typename T::ValueType > &pointer, const typename T::ValueType &defaultValue, typename T::AllocatorType &a)
char * u64toa(uint64_t value, char *buffer)
ValueType & Set(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, const Ch *value) const
Set a null-terminated string in a document.
const CharType(& source)[N]
ValueType & Set(ValueType &root, const Ch *value, typename ValueType::AllocatorType &allocator) const
Set a null-terminated string in a subtree.
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
#define RAPIDJSON_ASSERT(x)
Assertion.
void Parse(const Ch *source, size_t length)
Parse a JSON String or its URI fragment representation into tokens.
Ch * CopyFromRaw(const GenericPointer &rhs, size_t extraToken=0, size_t extraNameBufferSize=0)
Clone the content from rhs to this.
Ch * nameBuffer_
A buffer containing all names in tokens.
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
ValueType & Set(ValueType &root, ValueType &value, typename ValueType::AllocatorType &allocator) const
Set a value in a subtree, with move semantics.
ValueType::EncodingType EncodingType
Encoding type from Value.
Invalid percent encoding in URI fragment.
ValueType & Set(ValueType &root, const ValueType &value, typename ValueType::AllocatorType &allocator) const
Set a value in a subtree, with copy semantics.
GenericPointer(const Ch *source, size_t length, Allocator *allocator=0)
Constructor that parses a string or URI fragment representation, with length of the source string...
PointerParseErrorCode parseErrorCode_
Parsing error code.
const Ch * head_
Original head of the string.
PercentDecodeStream(const Ch *source, const Ch *end)
Constructor.
Token * tokens_
A list of tokens.
static RAPIDJSON_NAMESPACE_BEGIN const SizeType kPointerInvalidIndex
Represents an invalid index in GenericPointer::Token.
GenericPointer Append(const Ch *name, SizeType length, Allocator *allocator=0) const
Append a name token with length, and return a new Pointer.
A helper stream for decoding a percent-encoded sequence into code unit.
SizeType index
A valid array index, if it is not equal to kPointerInvalidIndex.
T::ValueType & SwapValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::ValueType &value, typename T::AllocatorType &a)
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
A token is the basic units of internal representation.
A document for parsing JSON text as DOM.
A token must begin with a '/'.
A read-write string stream.
SizeType length
Length of the name.
bool valid_
Whether the parsing is valid.
GenericPointer(const Ch *source, Allocator *allocator=0)
Constructor that parses a string or URI fragment representation.
size_t parseErrorOffset_
Offset in code unit when parsing fail.
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
ValueType & Swap(ValueType &root, ValueType &value, typename ValueType::AllocatorType &allocator) const
Swap a value with a value in a subtree.
Allocator stackAllocator stackAllocator & document
Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.
GenericPointer & operator=(const GenericPointer &rhs)
Assignment operator.
const Ch * end_
Past-the-end position.
A helper stream to encode character (UTF-8 code unit) into percent-encoded sequence.
GenericPointer< Value > Pointer
GenericPointer for Value (UTF-8, default allocator).
PercentEncodeStream(OutputStream &os)
RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr< internal::IsSame< typename internal::RemoveConst< T >::Type, Ch > >),(GenericPointer)) Append(T *name
Append a name token without length, and return a new Pointer.
Allocator & GetAllocator()
Get the allocator of this document.
size_t tokenCount_
Number of tokens in tokens_.
#define RAPIDJSON_DELETE(x)
! customization point for global delete
char * u32toa(uint32_t value, char *buffer)
T::ValueType & CreateValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::AllocatorType &a)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
ValueType::Ch Ch
Character type from Value.
const Ch * name
Name of the token. It has null character at the end but it can contain null character.
PointerParseErrorCode
Error code of parsing.
GenericPointer(const GenericPointer &rhs, Allocator *allocator=0)
Copy constructor.
bool NeedPercentEncode(Ch c) const
Check whether a character should be percent-encoded.
GenericPointer Append(const Token &token, Allocator *allocator=0) const
Append a token and return a new Pointer.
bool Erase(ValueType &root) const
Erase a value in a subtree.
bool EraseValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer)
Allocator stackAllocator stackAllocator T defaultValue const
ValueType & Set(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, ValueType &value) const
Set a value in a document, with move semantics.
bool Stringify(OutputStream &os) const
Stringify to string or URI fragment representation.
const GenericPointer< typename T::ValueType > T2 defaultValue
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Reference to a constant string (not taking a copy)
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
const GenericPointer< typename T::ValueType > & pointer
A character must percent encoded in URI fragment.
Allocator * ownAllocator_
Allocator owned by this Pointer.
T::ValueType & SetValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, typename T::ValueType &value, typename T::AllocatorType &a)
GenericPointer(Allocator *allocator=0)
Default constructor.
const Ch * src_
Current read position.
T::ValueType * GetValueByPointer(T &root, const GenericPointer< typename T::ValueType > &pointer, size_t *unresolvedTokenIndex=0)
ValueType & Swap(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, ValueType &value) const
Swap a value with a value in a document.
~GenericPointer()
Destructor.