C++ Boost JSON PTree 解析器无法解析字符串

4

我将尝试使用ptree本地存储信息并通过boost message_queue发送JSON消息。

以下是接收方的代码:

#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;

}

以下是发送方的代码:

#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);
}

发送者正在工作,并有以下输出:
$ ./sendptree                                                         
{
    "prefix": "standard",
    "faceID": "42"
}

接收方正确接收了数据,但在使用read_json函数时未能成功解析数据。
$ ./recvptree                                                             
{
    "prefix": "standard",
    "faceID": "42"
}

terminate called after throwing an 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
1个回答

5

我认为问题在于你的接收缓冲区没有以null作为结尾。下面是可行的代码:

服务器:

#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);
}

客户端:

#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::istringstream is(buffer);
    read_json(is, pt);                                                                                                                                                                                     

    std::cout << "prefix: " << pt.get<std::string>("prefix") << " -- faceID: " << pt.get<std::string>("faceID") << std::endl;
    system("PAUSE");
}

是的,看起来可以工作了,谢谢。之前我一直在做很多无用的事情,因为我没有完全理解它的工作原理,所以造成了很多混乱。 - kurojishi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接