C++ - Passing rapidjson::Document as an argument to a function C++ - Passing rapidjson::Document as an argument to a function json json

C++ - Passing rapidjson::Document as an argument to a function


Use a reference or a value as argument. Using the [] with a pointer will try to use your document as if it was an array of document. A reference or a value will call the expected operator.

// a const referencefoo(const rapidjson::Document& jsonDocument) {    std::cout << jsonDocument["name"] << std::endl;}// a copy (or move)foo(rapidjson::Document jsonDocument) {    std::cout << jsonDocument["name"] << std::endl;}

I'd recommend you to use the reference, as your function don't need to consume any resources in the document, but only observe and print a value.

The call of this function will look like this:

rapidjson::Document doc = /* ... */;foo(doc);