CPPRestSDK (casablanca) Extract JSON from incoming WebSocket messages (invalid token)

I am connecting to a WebSocket which always responds in JSON. I see that there is an extract_string method for websocket_incoming_message , but after trying a lot of things with json:value it seems that you can only build JSON arrays on the fly by inserting key-value pairs one by one. Am I missing something here or is there a way to infer from websocket_incoming_message and directly convert it to a json:value array?

  wsClient.set_message_handler([=](websocket_incoming_message msg) { // handle message from server... printf("[WebSocket INBOUND]: %s", msg.extract_string().get().c_str()); printJSON(json::value::parse(conversions::to_string_t(msg.extract_string().get()))); }); 

printJSON goes through json::value and prints each key-value-pair.

Unhandled exception at 0x00007FF866923FB8 in RestAPI.exe: Microsoft C ++ exception: web :: json :: json_exception in memory location 0x0000003E553FDDC0. occurred

Console output:

[WebSocket INBOUND]: {"T": NULL, "S": NULL, "op": 10, "d": {"heartbeat_interval": 41250, "_ trace": ["gateway-PRD-main cr3x"] }} p>

Despite the fact that we can compile and run the application, I believe that the exception is caused by the fact that they passed a string containing a JSON table, and not a single element? Does this mean that I need to manually parse the string and pull out each key-value pair while creating a json array?

There must be a way to do this, this seems like basic functionality needed.

Similar unresolved issue

Any help here would be greatly appreciated! Thank you for your time.

+7
c ++ json websocket casablanca cpprest-sdk
source share
2 answers

Try to catch web :: json :: json_exception and print a message, this may give you a hint about what's wrong

+2
source share

I got a complete solution. Try using boost pacakges from nuget. The documentation will help you parse json data from a string. I think jsoncpp is not an updated package available in nuget.so, please try increasing the packages available in nuget.

MYJSON STRING

{"action": "refresh_dashboard", "data": {"users_list": [{"user_id": "901e6076ff351cfc2195fb86f8438a26", "extensions": ["1002"], "name": "Karthik M"}, {" user_id ":" 7d617ef5b2390d081d901b0d5cd108eb "," extensions ": [" 1015 "]," name ":" Synway User2 "}, {" user_id ":" c8f667f7d663e81f6e7fa34b9296f067 "," extensions ":," name ": 1012 Rahib Video "}, {" user_id ":" cc3f94ecc14ee9c55670dcde9adc1887 "," extensions ": [" 1006 "]," name ":" Rounak S Kiran "}, {" user_id ":" 6c29ebdb34e1761fdf9423c573087979 "," extensions " 1003 "]," name ":" Amar Nath "}, {" user_id ":" 8e15c2d95d4325cb07f0750846966be8 "," extensions ": [" 1011 "]," name ":" TLS User "}, {" user_id ":" 2fc4142bdacf83c1957de39 "," extensions ": [" 1014 "]," name ":" Synway User1 "}, {" user_id ":" 74d5b5a9aca1faa4c2f217ce87b621d8 "," extensions ": [" 1008 "]," name ":" Robin Raju "} , {"user_id": "a7ad7e73bf93ea83c8efdc1723cba198", "extensions": ["1007"], "name": "Arshad Arif"}, {" user_id ":" b55146df593ec8d09e5fe12a8a4c1108 "," extensions ": [" 1001 "]," name ":" Rahib Rasheed "}, {" user_id ":" 391391de005a8f5403c7b5591f462ea1 "," extensions ":," name ": 101 "Sangeeth J"}, {"user_id": "3258f7ae4ae1db60435cbcf583f64a89", "extensions": ["1009"], "name": "Aby TL"}, {"user_id": "90bc84e5e8a3427fe35e99bd4386de95", "extensions" 1010 "]," name ":" Prince T "}, {" user_id ":" b501ef5b270a196afc0eed557ca74237 "," extensions ": [" 1005 "]," name ":" Jineed AJ "}, {" user_id ":" 1422af351e06adeab2de92f5a633a444 "," extensions ": [" 1004 "]," name ":" Ashok PA "}]," busy_users ": []," reg_users ": [{" user_id ":" 901e6076ff351cfc2195fb86f8438a26 "," status ":" registered "}, {" user_id ":" 6c29ebdb34e1761fdf9423c573087979 "," status ":" registered "}]," contacts ": [{" owner_id ":" 901e6076ff351cfc2195fb86f8438a26 "," status ":" ready "}, {" owner_id ": "6c29ebdb34e1761fdf9423c573087979", "one hundred tus ":" ready "}]}}

CODE

 client.receive().then([](websocket_incoming_message msg) { std::cout << "receiving data from socket"; // msg.message_type(); return msg.extract_string(); //1..i have one string //cout<<"\n///////////test"<< msg.extract_string().get().c_str(); // // 2.convert to json array //json::value::parse( ::to_string_t(msg.extract_string().get())) // }).then([](std::string body) { //std::cout << "displaying the data"; std::cout << body << std::endl; std::string ss = body; ptree pt; std::istringstream is(ss); read_json(is, pt); std::cout <<"\n 1st"<< "action: " << pt.get<std::string>("action") << "\n"; std::cout <<"\n 2nd"<< "data: " << pt.get<std::string>("data") << "\n"; std::cout << "--------------------------------------------------------------"; for (auto& e : pt.get_child("users_list")) { std::cout << "\n" << "users list " << e.second.get<std::string>("user_id") << "\n"; } }); 

useful resources

  • Parse a JSON array as std :: string with Boost ptree

  • Parsing parses a dynamically generated json string (not a file)

+2
source share

All Articles