C++ boost json ptree parser fail to parse string -
i'm trying send json messages trough boost message_queue using ptree storing informations localy.
this code of receiver:
#include <boost/interprocess/ipc/message_queue.hpp> #include <iostream> #include <string> //#include <boost/archive/text_iarchive.hpp> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/serialization/string.hpp> using boost::interprocess::message_queue; using boost::property_tree::ptree; using boost::property_tree::read_json; #define max_size 1000 int main() { message_queue mq (boost::interprocess::open_or_create,"coda",10,max_size); message_queue::size_type recv_size; unsigned int priority; ptree pt; std::stringstream iss; std::string serialized_string; serialized_string.resize(max_size); mq.receive(&serialized_string[0],max_size,recv_size,priority); iss << serialized_string; std::istringstream is(iss.str()); read_json(is, pt); std::cout pt.get<std::string>("prefix") << pt.get<std::string>("faceid") << std::endl; }
and code of th sender:
#include <boost/interprocess/ipc/message_queue.hpp> #include <boost/serialization/string.hpp> #include <string> #include <sstream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> using boost::interprocess::message_queue; using boost::property_tree::ptree; using boost::property_tree::write_json; #define max_size 1000 void send(ptree pt) { std::stringstream oss; write_json(oss,pt); std::string serialized_strings(oss.str()); message_queue mq(boost::interprocess::open_only,"coda"); mq.send(serialized_strings.data(), serialized_strings.size(),0); std::cout << oss.str() << std::endl; } int main() { ptree pt; pt.put("prefix","standard"); pt.put("faceid",42); send(pt); }
the sender works , have output:
$ ./sendptree { "prefix": "standard", "faceid": "42" }
the receiver receive data correctly fail parse (with read_json call).
$ ./recvptree { "prefix": "standard", "faceid": "42" } terminate called after throwing instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::json_parser::json_parser_error> >' what(): <unspecified file>(5): expected end of input [1] 24052 abort ./recvptree
i think receive buffer not null terminated. code works:
server:
#include <boost/interprocess/ipc/message_queue.hpp> #include <boost/serialization/string.hpp> #include <string> #include <sstream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> using boost::interprocess::message_queue; using boost::property_tree::ptree; using boost::property_tree::write_json; #define max_size 1000 void send(ptree pt) { std::stringstream oss; write_json(oss,pt); message_queue mq(boost::interprocess::open_or_create,"coda",10,max_size); mq.send(oss.str().c_str(), oss.str().size(),0); std::cout << oss.str() << std::endl; system("pause"); } int main() { ptree pt; pt.put("prefix","standard"); pt.put("faceid",42); send(pt); }
client:
#include <boost/interprocess/ipc/message_queue.hpp> #include <string> #include <strstream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <boost/serialization/string.hpp> using boost::interprocess::message_queue; using boost::property_tree::ptree; using boost::property_tree::read_json; #define max_size 1000 int main() { message_queue mq (boost::interprocess::open_or_create,"coda",10,max_size); size_t recv_size; unsigned int priority; ptree pt; char buffer[max_size]; mq.receive(buffer,max_size,recv_size,priority); buffer[recv_size] = 0; std::istrstream is(buffer); read_json(is, pt); std::cout << "prefix: " << pt.get<std::string>("prefix") << " -- faceid: " << pt.get<std::string>("faceid") << std::endl; system("pause"); }
Comments
Post a Comment