在 C++ 中将 Slice 对象显式序列化为字符串或 ostream

问题描述

有没有办法使用冰将 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

中有其他示例

可以在 https://doc.zeroc.com/ice/3.7/client-server-features/dynamic-ice/streaming-interfaces/c++-streaming-interfaces/the-outputstream-interface-in-c++

找到 OutputStream 的文档

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...