Function Accept


#include <include/rapidjson/document.h>

template <typename Handler>
bool Accept(Handler &handler) const


Generate events of this value to a Handler.

This function adopts the GoF visitor pattern. Typical usage is to output this JSON value as JSON text via Writer, which is a Handler. It can also be used to deep clone this value via GenericDocument, which is also a Handler.

Template Parameters

Handler - type of handler.


handler - An object implementing concept Handler.

Mentioned in


Lines 1932-1970 in include/rapidjson/document.h.

template <typename Handler>
bool Accept(Handler& handler) const {
    switch(GetType()) {
    case kNullType:     return handler.Null();
    case kFalseType:    return handler.Bool(false);
    case kTrueType:     return handler.Bool(true);
    case kObjectType:
        if (RAPIDJSON_UNLIKELY(!handler.StartObject()))
            return false;
        for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
            RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator.
            if (RAPIDJSON_UNLIKELY(!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.data_.f.flags & kCopyFlag) != 0)))
                return false;
            if (RAPIDJSON_UNLIKELY(!m->value.Accept(handler)))
                return false;
        return handler.EndObject(data_.o.size);
    case kArrayType:
        if (RAPIDJSON_UNLIKELY(!handler.StartArray()))
            return false;
        for (const GenericValue* v = Begin(); v != End(); ++v)
            if (RAPIDJSON_UNLIKELY(!v->Accept(handler)))
                return false;
        return handler.EndArray(data_.a.size);

    case kStringType:
        return handler.String(GetString(), GetStringLength(), (data_.f.flags & kCopyFlag) != 0);

        RAPIDJSON_ASSERT(GetType() == kNumberType);
        if (IsDouble())         return handler.Double(data_.n.d);
        else if (IsInt())       return handler.Int(data_.n.i.i);
        else if (IsUint())      return handler.Uint(data_.n.u.u);
        else if (IsInt64())     return handler.Int64(data_.n.i64);
        else                    return handler.Uint64(data_.n.u64);

Add Discussion as Guest

Log in