I am getting boost::beast::multibuffer object from http::response<http::dynamic_body>::body() method. Then, I want to parse json content from it like this:
boost::property_tree::read_json(requestBodyStream, propertyTree);
Should I use boost::beast::buffers_to_string and std::stringstream to get requestBodyStream or is it possible to do without spending so much memory on copying the contents of the buffer?
In general, don't program the specific implementation, but program to the concept. Here,
dynamic_bodydocuments:You don't need that concept, as you will be consuming this entirely in-memory anyways, but if you did, you would go about it like:
See it Live On Coliru
It reads a sample response from stdin, let's use
Like so:
Prints
HOWEVER
Now that we've answered the question, let's add context:
Don't use Boost Property Tree (unless you need Property Trees. Hint: you do not). Look at the output: Property Tree is NOT a JSON library
Don't use dynamic body unless you need it. In this case you're reading the entire message in memory, copying it in memory (to convert to a streambuf), reading from it using locale-aware istream (slow) and the result lives as another copy in memory.
Instead, use the simplest model you can and use a JSON library, like, you know, Boost.JSON:
Live On Coliru
It's less code, more efficient and most importantly, correct handling of the JSON!