stringify with rapidjson stringify with rapidjson json json

stringify with rapidjson


I am the author of rapidjson. Thank you for your question. I recorded this to issue in http://code.google.com/p/rapidjson/issues/detail?id=45

It is due to that GenericValue::Accept() is non-const.

As GenericValue::Accept() just generates events for handler, it does not need to modify the value and its decedents. So it should change from:

template <typename Handler>GenericValue& Accept(Handler& handler)

to

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

You may patch this to your rapidjson/document.h or download the latest version (trunk or 0.1x branch).

After this change, you can stringfy a const Value as in tutorial:

const Value& v = ...;FileStream f(stdout);PrettyWriter<FileStream> writer(f);v.Accept(writer);

Or to a string buffer:

const Value& v = ...;StringBuffer buffer;PrettyWriter<StringBuffer> writer(buffer);v.Accept(writer);const char* json = buffer.GetString();