【问题标题】:Thrift: Serialize + Deserialize changes objectThrift:序列化 + 反序列化更改对象
【发布时间】:2012-11-14 08:54:56
【问题描述】:

我有一个类似这样的节俭结构:

struct GeneralContainer {
    1: required string identifier;
    2: required binary data;
}

这个想法是能够在单个“管道”上传递不同类型的 thrift 对象,并且仍然能够在另一端正确反序列化。

但是序列化一个GeneralContainer 对象,然后反序列化它会改变data 字段的内容。我正在使用TBinaryProtocol

TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());

GeneralContainer container = new GeneralContainer();
container.setIdentifier("my-thrift-type");
container.setData(ByteBuffer.wrap(serializer.serialize(myThriftTypeObject)));

byte[] serializedContainer = serializer.serialize(container);
GeneralContainer testContainer = new GeneralContainer();
deserializer.deserialize(testContainer, serializedContainer);
Assert.assertEquals(container, testContainer);  // fails

我的猜测是,当我们使用TBinaryProtocol 序列化包含二进制字段的对象时,某种标记会变得混乱。那是对的吗?如果是,我对协议有哪些选择?我的目标是最小化生成的序列化字节数组的大小。

谢谢, 阿曼

【问题讨论】:

  • 更新:使用TCompactProtocol运行

标签: java thrift thrift-protocol


【解决方案1】:

跟踪到 thrift 0.4 序列化中的一个错误。在 thrift 0.8 中运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-11
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多