Using RapidJSON, I can read a local json file by:
std::ifstream ifs { R"(/home//am_v2.json)" };
IStreamWrapper isw { ifs };
Document doc {};
doc.ParseStream( isw );
However, I want to read a variable received from:
auto msg = mqttClient->consume_message();
The content is shown by msg->to.string()
However, I cannot get this content to be read using:
std::ifstream ifs { msg };
Any clue?
Here a more complete code:
void MqttApplication::send()
{
try {
mqttClient->start_consuming();
mqttClient->subscribe(TOPIC, QOS)->wait();
}
catch (const mqtt::exception& exc) {
cerr << exc.what() << endl;
return;
}
while (true) {
auto msg = mqttClient->consume_message();
if (!msg) {
if (!mqttClient->is_connected()) {
cout << "MQTT: mqtt_application lost connection. Attempting reconnect" << endl;
if (mqttTryReconnect(*mqttClient)) {
mqttClient->subscribe(TOPIC, QOS);
cout << "MQTT: mqtt_application reconnected" << endl;
continue;
}
else {
cout << "MQTT: mqtt_application reconnect failed." << endl;
}
}
else {
cout << "MQTT: mqtt_application an error occurred retrieving messages." << endl;
}
break;
}
if (msg->get_topic() == "command" &&
msg->to_string() == "exit") {
cout << "Exit command received" << endl;
break;
}
cout << msg->get_topic() << ": " << msg->to_string() << endl;
Document doc;
doc.Parse(msg->to.string());
}
}
Thanks
First, make sure you
#define RAPIDJSON_HAS_STDSTRING 1
before you include the Rapidjson headers.
Parsing the contents of a message from mqtt is then as simple as:
Document doc;
doc.Parse(msg->to.string());
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.