我正在尝试在Thrift
中序列化C
联合,然后在Java
中反序列化。在Java方面,我总是只序列化第一个联合字段。可能是某个人面临类似的问题?
节俭0.9.3
messages.thrift:
struct JLException {
...
}
struct JLFrame {
...
}
union JLMessage {
1: JLException exc,
2: JLFrame frame
}
C中的序列化代码(我替换为“......”不必要的部分):
JLException* jlExeption = g_object_new(TYPE_J_L_EXCEPTION, ... , NULL);
if (jlExeption) {
JLMessage* jlMessage = g_object_new(TYPE_J_L_MESSAGE, "exc", jlExeption, NULL);
serialize_and_send_thrift_message((ThriftStruct*) jlMessage,
THRIFT_STRUCT_CLASS(J_L_MESSAGE_GET_CLASS(jlMessage)));
g_object_unref(jlExeption);
g_object_unref(jlMessage);
}
JLFrame* jlFrame = g_object_new(TYPE_J_L_FRAME, ... , NULL);
if (jlFrame) {
JLMessage* jlMessage = g_object_new(TYPE_J_L_MESSAGE, "frame", jlFrame,
NULL);
serialize_and_send_thrift_message((ThriftStruct*) jlMessage,
THRIFT_STRUCT_CLASS(J_L_MESSAGE_GET_CLASS(jlMessage)));
g_object_unref(jlFrame);
g_object_unref(jlMessage);
}
Java中的反序列化代码:
TDeserializer deserializer = new TDeserializer();
JLMessage message = new JLMessage();
try {
byte[] binData = tuple.getBinary(0);
deserializer.deserialize(message, binData);
} catch (TException e) {
_logger.error(e);
}
if (JLMessage._Fields.EXC.equals(message.getSetField())) {
_logger.info("Received EXCEPTION message");
} else if (JLMessage._Fields.FRAME.equals(message.getSetField())) {
_logger.info("Received FRAME message");
}
因此,在Java端仅记录“已接收的EXCEPTION消息”。对于JLFrame
条消息,它会收到空JLException
条消息。