问题描述
有没有办法使用冰将 Slice 对象显式序列化为字符串?问题是有一个对象必须可以通过 json / xml / ice 发送,并且因为 ice 在 Ice(Slice) 的规范语言中已经有一个平台无关的对象,所以不需要包含另一个像 protobuf 这样的库。但据我所知,不可能显式序列化对象。我错了吗?
解决方法
您可以使用 OutputStream API 以 Ice 二进制格式序列化对象
Ice::ByteSeq inParams,outParams;
Ice::OutputStream out(communicator);
out.startEncapsulation();
Demo::CPtr c = new Demo::C;
c->s.name = "blue";
c->s.value = Demo::blue;
out.write(c);
out.writePendingValues();
out.endEncapsulation();
out.finished(inParams);
在 ice-demos 存储库 https://github.com/zeroc-ice/ice-demos/tree/3.7/cpp98/Ice/invoke
中有其他示例 找到 OutputStream 的文档