1

I am having problems saving the JSON response using json cpprestsdk so that I can pass it back to the calling function. The parse() function seems to do a shallow copy rather than a deep copy. Can someone please help me with the method that I should be using to make a deep copy of the results returned?

Thanks in advance.

functionA()
{

   web::json::value myJsonObjSv;

   http_client client(L"http://url.com:8080/getdata");

   return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value> {

       if(response.status_code() == status_codes::OK) {
           return response.extract_json();
       }

   return pplx::task_from_result(json::value()); }).then([&myJsonObjSv](pplx::task<json::value> previousTask) {
        try {
             // capture json response to json_resp
             json::value const resp = previousTask.get();

             // Now save the JSON object to return from this function
             myJsonObjSv = json::value::parse(resp.serialize());

             // This statement displays the contents of myJsonObjSv to be the same as resp
             wcout << "myJsonObjSv[" myJsonObjSv.serialize() << "]" << endl;
         }
        catch( const http_exception& e) {
             // print error
         }
    });

    // This statement to display the contents of myJsonObjSv crashes since there was a shallow copy made rather than a deep copy
    wcout << "myJsonObjSv[" myJsonObjSv.serialize() << "]" << endl;

}
QueryMan
  • 21
  • 3

0 Answers0