断言"IsObject()"在通过 Boost ASIO 发送对象时失败
Assertion `IsObject()' failed when sending object by Boost ASIO
我正在使用RapidJSON通过Boost ASIO发送对象,并收到以下错误消息:
./include/rapidjson/document.h:1233: rapidjson::GenericValue<Encoding, Allocator>::MemberIterator rapidjson::GenericValue<Encoding, Allocator>::FindMember(const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::GenericValue<Encoding, Allocator>::MemberIterator = rapidjson::GenericMemberIterator<false, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> >]: Assertion `IsObject()' failed.
这是我放在 rapidJson::D ocument 中的字符串。解析((
{ "rotation": "1.000000", "size": "1.000000", "collide": "100", "lifePoint": "5", "id": "1", "type": "1", "pos.x": "1.100000", "pos.y": "1.200000" }
我使用我的文档。在此类中解析
class Data
{
public:
Data(std::string data) {
rapidjson::Document document;
document.Parse(data.c_str());
rotation_ = document["rotation"].GetFloat();
size_ = document["size"].GetFloat();
collide_ = document["collide"].GetInt();
lifePoint_ = document["lifePoint"].GetInt();
id_ = document["id"].GetInt();
type_ = document["type"].GetInt();
pos_.first = document["pos.x"].GetFloat();
pos_.second = document["pos.y"].GetFloat();
};
~Data(){};
void print()
{
std::cout << rotation_ << " = rotation_" << std::endl;
std::cout << size_ << " = size_" << std::endl;
std::cout << collide_ << " = collide_" << std::endl;
std::cout << lifePoint_ << " = lifePoint_" << std::endl;
std::cout << id_ << " = id_" << std::endl;
std::cout << type_ << " = type_" << std::endl;
std::cout << pos_.first << " = pos_.x" << std::endl;
std::cout << pos_.second << " = pos_.y" << std::endl;
}
std::string getJson() {
std::string json("{ "rotation": "" + std::to_string(rotation_) + "", "size": "" + std::to_string(size_) + "", "collide": "" + std::to_string(collide_) + "", "lifePoint": "" + std::to_string(lifePoint_) + "", "id": "" + std::to_string(id_) + "", "type": "" + std::to_string(type_) + "", "pos.x": "" + std::to_string(pos_.first) + "", "pos.y": "" + std::to_string(pos_.second) + "" }");
return json;
};
private:
//geometry
std::pair<float, float> pos_;
float rotation_ = 1;
float size_ = 1;
int collide_ = 0;
//game
int lifePoint_;
int id_;
int type_;
};
我在这里调用数据构造函数
void BoostClient::receivePacket()
{
boost::array<char, 2048> recv_buf;
udp::endpoint sender_endpoint;
static_cast<udp::socket *>(socket_)->receive_from(
boost::asio::buffer(recv_buf), sender_endpoint);
std::cout << "received: " << std::string(recv_buf.data()) << std::endl;
std::string json(recv_buf.data());
Data data(json);
data.print();
};
有人知道如何解决吗?我已经验证了 JSON 语法,它已经足够好了。
输出:
E�ived: { "rotation": "1.000000", "size": "1.000000", "collide": "100", "lifePoint": "5", "id": "1", "type": "1", "pos.x": "1.100000", "pos.y": "1.200000" }
client: ./include/rapidjson/document.h:1233: rapidjson::GenericValue<Encoding, Allocator>::MemberIterator rapidjson::GenericValue<Encoding, Allocator>::FindMember(const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::GenericValue<Encoding, Allocator>::MemberIterator = rapidjson::GenericMemberIterator<false, rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> >]: Assertion `IsObject()' failed.
[1] 10410 abort (core dumped) ./client 192.168.43.58 1300
您应该检查解析返回值是否存在解析错误:
ParseResult ok = doc.Parse("[42]");
if (!ok) {
fprintf(stderr, "JSON parse error: %s (%u)",
GetParseError_En(ok.Code()), ok.Offset());
...
确保在发送数据时写入以零结尾的字符串 - 应该类似于 send(json_str.c_str(), json_str.length() + 1)
.
你有没有尝试过这样获得你的价值?
rotation_ = std::stof(document["rotation"].GetString());
size_ = std::stof(document["size"].GetString());
collide_ = std::stoi(document["collide"].GetString());
lifePoint_ = std::stoi(document["lifePoint"].GetString());
id_ = std::stoi(document["id"].GetString());
type_ = std::stoi(document["type"].GetString());
pos_.first = std::stof(document["pos.x"].GetString());
pos_.second = std::stof(document["pos.y"].GetString());
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- C++ Boost::asio x64 async_send失败,出现错误 10014 (WSAEFAULT)
- 由于管道破裂,提升 asio 发送消息失败
- 断言"IsObject()"在通过 Boost ASIO 发送对象时失败
- asio::thread_pool 在调用构造函数之前失败
- 串行端口奇偶校验检查在升压 asio 中失败
- boost::async_connect 上的 ASIO 完成处理程序在第一次失败后再也没有调用过
- 证书验证在客户端服务器通信中使用boost :: asio和openssl失败
- Asio UDP 套接字接收失败
- OSX上的Boost ASIO多播失败
- 为什么ASIO同步写入newtork第二次发送数据失败
- 使用boost::asio::async_read失败,但boost::asio::read工作(我使用io_strea
- Boost::Asio异步写入失败
- Boost ASIO async_connect在应该失败的时候成功了,为什么?
- Std::bind在boost::asio::ip::tcp::resolver上失败
- 当' boost::asio::ip::tcp::resolver::resolve() '失败时,应该提供哪个'
- Mac上使用Qt的Boost Tcp-Asio-Server链接失败
- Boost::asio读取大于65536字节的文件失败.处理步骤
- boost asio编译失败(C++类中的初始化列表)
- 日志含义boost::asio连接本地主机失败