简体   繁体   中英

json serialize c++

I have this C++ code, and am having trouble json serializing it.

 string uInput;
 string const& retInput;
 while(!std::cin.eof()) {
   getline(cin, uInput);
   JSONExample source; //JSON enabled class from jsonserialize.h
   source.text = uInput;
   //create JSON from producer
   std::string json = JSON::producer<JSONExample>::convert(source); //string -> returns {"JSONExample":{"text":"hi"}}
   //then create new instance from a consumer...
   JSONExample sink = JSON::consumer<JSONExample>::convert(json);
   //retInput = serialize(sink);
   // Json::FastWriter fastWriter;
   // retInput = fastWriter.write(uInput);
   retInput = static_cast<string const&>(uInput);
   pubnub::futres fr_2 = pb_2.publish(chan, retInput);
   cout << "user input as json which should be published is " << retInput<< std::endl;
 while(!cin.eof()) {
   getline(cin, uInput);
   newInput = "\"\\\""; 
   newInput += uInput;
   newInput += "\\\"\""; 

Instead of typing in the message like "\\"hi\\"", this code takes "hi" and does it.

If the change you described made the "Invalid JSON" disappear, then a "more correct" solution would be, AFAICT, to change the publish() line to:

pubnub::futres fr_2 = pb_2.publish(chan, json);

Because json already has JSON serialized data. Of course, if that JSON is what you want to publish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM