Function Create

Summary

#include <include/rapidjson/pointer.h>

(1) ValueType & Create(ValueType &root, typename ValueType::AllocatorType &allocator, bool *alreadyExist=0) const

(2) template <typename stackAllocator>
    ValueType & Create(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, bool *alreadyExist=0) const

Function overload

Synopsis

#include <include/rapidjson/pointer.h>

ValueType & Create(ValueType &root, typename ValueType::AllocatorType &allocator, bool *alreadyExist=0) const

Description

Create a value in a subtree.

If the value is not exist, it creates all parent values and a JSON Null value. So it always succeed and return the newly created or existing value.

Remind that it may change types of parents according to tokens, so it potentially removes previously stored values. For example, if a document was an array, and "/foo" is used to create a value, then the document will be changed to an object, and all existing array elements are lost.

Parameters

root - Root value of a DOM subtree to be resolved. It can be any value other than document root.

allocator - Allocator for creating the values if the specified value or its parents are not exist.

alreadyExist - If non-null, it stores whether the resolved value is already exist.

Returns
The resolved newly created (a JSON Null value), or already exists value.

Mentioned in

Source

Lines 461-508 in include/rapidjson/pointer.h.

ValueType& Create(ValueType& root, typename ValueType::AllocatorType& allocator, bool* alreadyExist = 0) const {
    RAPIDJSON_ASSERT(IsValid());
    ValueType* v = &root;
    bool exist = true;
    for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
        if (v->IsArray() && t->name[0] == '-' && t->length == 1) {
            v->PushBack(ValueType().Move(), allocator);
            v = &((*v)[v->Size() - 1]);
            exist = false;
        }
        else {
            if (t->index == kPointerInvalidIndex) { // must be object name
                if (!v->IsObject())
                    v->SetObject(); // Change to Object
            }
            else { // object name or array index
                if (!v->IsArray() && !v->IsObject())
                    v->SetArray(); // Change to Array
            }
            if (v->IsArray()) {
                if (t->index >= v->Size()) {
                    v->Reserve(t->index + 1, allocator);
                    while (t->index >= v->Size())
                        v->PushBack(ValueType().Move(), allocator);
                    exist = false;
                }
                v = &((*v)[t->index]);
            }
            else {
                typename ValueType::MemberIterator m = v->FindMember(GenericValue<EncodingType>(GenericStringRef<Ch>(t->name, t->length)));
                if (m == v->MemberEnd()) {
                    v->AddMember(ValueType(t->name, t->length, allocator).Move(), ValueType().Move(), allocator);
                    m = v->MemberEnd();
                    v = &(--m)->value; // Assumes AddMember() appends at the end
                    exist = false;
                }
                else
                    v = &m->value;
            }
        }
    }
    if (alreadyExist)
        *alreadyExist = exist;
    return *v;
}

Synopsis

#include <include/rapidjson/pointer.h>

template <typename stackAllocator>
ValueType & Create(GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, bool *alreadyExist=0) const

Description

Creates a value in a document.

Parameters

document - A document to be resolved.

alreadyExist - If non-null, it stores whether the resolved value is already exist.

Returns
The resolved newly created, or already exists value.

Mentioned in

Source

Lines 516-519 in include/rapidjson/pointer.h.

template <typename stackAllocator>
ValueType& Create(GenericDocument<EncodingType, typename ValueType::AllocatorType, stackAllocator>& document, bool* alreadyExist = 0) const {
    return Create(document, document.GetAllocator(), alreadyExist);
}





Add Discussion as Guest

Log in