通过 Boost ASIO 发送对象时断言“IsObject()”失败

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::Document.Parse()

中的字符串
{ "rotation": "1.000000", "size": "1.000000", "collide": "100", "lifePoint": "5", "id": "1", "type": "1", "pos.x": "1.100000", "pos.y": "1.200000" }

并且我在这个 class

中使用我的 document.Parse
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

您应该检查 parse errors 的解析 return 值:

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());