我正在尝试在我的项目中使用 protostuff 库进行序列化/反序列化。我按照github中提到的 README.md 进行操作。
我遇到了一个问题,即 ZonedDateTime 字段未正确序列化/反序列化。
考虑这个简单的例子
public class SomeClass {
private ZonedDateTime dateTime;
private String message;
public SomeClass(ZonedDateTime dateTime, String message) {
this.dateTime = dateTime;
this.message = message;
}
public static void main(String[] args) {
SomeClass serialized = new SomeClass(ZonedDateTime.now(), "some message");
byte[] bytes = getProtoByteArray(serialized, SomeClass.class);
SomeClass deSerialized = getObjectFromProtoByteArray(bytes, SomeClass.class);
System.out.println(serialized.getDateTime()); // 2021-06-14T23:07:07.100+05:30[Asia/Calcutta]
System.out.println(deSerialized.getDateTime()); // 2021-06-14T23:07:07.100null[Asia/Calcutta]
}
//serialization
public static <T> byte[] getProtoByteArray(T message, Class<T> clazz) {
LinkedBuffer buffer = LinkedBuffer.allocate(512);
Schema<T> schema = RuntimeSchema.getSchema(clazz);
// ser
final byte[] protostuff;
try {
protostuff = ProtobufIOUtil.toByteArray(message, schema, buffer);
} finally {
buffer.clear();
}
return protostuff;
}
//deserialization
public static <T> T getObjectFromProtoByteArray(byte[] data, Class<T> clazz) {
Schema<T> schema = RuntimeSchema.getSchema(clazz);
T parsed = schema.newMessage();
ProtobufIOUtil.mergeFrom(data, parsed, schema);
return parsed;
}
//getters setters
}
正如您在输出中看到的,反序列化对象的偏移量字段为 null
2021-06-14T23:07:07.100null[Asia/Calcutta]
我们是否必须配置一些东西来解决这个问题?或者当前库不支持 ZonedDateTime 类型?