Am not so sure what you want to do, but I can tell you C++ and Json... hummm
you can use a lib called rapid json and generate the json you need appending key-values as it were a dictionary
here an example:
#include <iostream>
#include <string>
#include <rapidjson\document.h>
#include <rapidjson\writer.h>
#include <rapidjson\stringbuffer.h>
void output(const rapidjson::Document& document)
{
std::string strOp = document["op"].GetString();
std::string ret = "{\"op\":\"" + strOp;
std::string strTopic = document["topic"].GetString();
ret += "\", \"topic\":\"" + strTopic;
std::string strType = document["type"].GetString();
ret += "\", \"type\":\"" + strType + "\"}";
std::cout << "json" << std::endl;
std::cout << ret << std::endl;
//{ "op": "subscribe", "topic" : "/cmd_vel", "type" : "geometry_msgs/Twist" }
}
int main(int argC, char** argV)
{
std::cout << "ROS" << std::endl;
rapidjson::Document fromScratch;
fromScratch.SetObject();
rapidjson::Document::AllocatorType& allocator = fromScratch.GetAllocator();
fromScratch.AddMember("op", "subscribe", allocator);
fromScratch.AddMember("topic", "/cmd_vel", allocator);
fromScratch.AddMember("type", "geometry_msgs/Twist", allocator);
output(fromScratch);
getchar();
return 0;
}
The output will look like:
You can put json into a
string
. Or do you mean make the entire message into json, and use it outside of ros with for example a web server that uses json?yes entire request format should be in json format and same needs to be published so that other node can subscribe the same.