2

Protostuff 代码生成器生成的类是否与 Protobuf 创建的类兼容?

我试图(反)序列化一些简单的消息并得到几个例外:

原始文件 (WrapperClass.proto)

package tutorial;
option java_package = "com.example.tutorial";
message ProjectId {
    required int32 id = 1;
}
message UserId {
    required ProjectId project = 1;
    required int32 projectUserId = 2;
}
message ChannelId {
    required ProjectId project = 1;
    required string name = 2;
}

Protostuff 到 Protobuf 测试(示例)

ProjectId projectId = new ProjectId(1);
byte[] projectarray = ProtostuffIOUtil.toByteArray(projectId, ProjectId.getSchema(), buffer);
com.example.tutorial.WrapperClass.ProjectId returnBufProject = com.example.tutorial.WrapperClass.ProjectId.parseFrom(projectarray);

问题:

一切都适用于 ProjectId,但对于 UserId 和 ChannelId(一切都更复杂一些),我得到:

com.google.protobuf.InvalidProtocolBufferException: Message missing required fields: project
    at com.google.protobuf.UninitializedMessageException.asInvalidProtocolBufferException(UninitializedMessageException.java:81)
    at com.example.tutorial.WrapperClass$ChannelId$Builder.buildParsed(Test.java:1278)
    at com.example.tutorial.WrapperClass$ChannelId$Builder.access$17(Test.java:1273)
    at com.example.tutorial.WrapperClass$ChannelId.parseFrom(Test.java:1142)
    ...

反之亦然:

Protobuf 到 Protostuff 测试(示例)

com.example.tutorial.WrapperClass.ProjectId projectId2 = com.example.tutorial.WrapperClass.ProjectId.newBuilder().setId(1).build();
byte[] project2array = projectId2.toByteArray();
ProjectId returnStufProject = new ProjectId();
ProtostuffIOUtil.mergeFrom(project2array, returnStufProject, ProjectId.getSchema());

问题

同样,对于 ProjectId 以外的所有内容,都有一个例外:

java.lang.RuntimeException: Reading from a byte array threw an IOException (should never happen).
    at com.dyuproject.protostuff.IOUtil.mergeFrom(IOUtil.java:53)
    at com.dyuproject.protostuff.ProtostuffIOUtil.mergeFrom(ProtostuffIOUtil.java:96)
    at JacksonTest.main(JacksonTest.java:92)
Caused by: com.dyuproject.protostuff.ProtobufException: Protocol message contained an invalid tag (zero).
    at com.dyuproject.protostuff.ProtobufException.invalidTag(ProtobufException.java:98)
    at com.dyuproject.protostuff.ByteArrayInput.readFieldNumber(ByteArrayInput.java:220)
    at com.example.tutorial.ProjectId$1.mergeFrom(ProjectId.java:115)
    at com.example.tutorial.ProjectId$1.mergeFrom(ProjectId.java:1)
    at com.dyuproject.protostuff.ByteArrayInput.mergeObjectEncodedAsGroup(ByteArrayInput.java:390)
    at com.dyuproject.protostuff.ByteArrayInput.mergeObject(ByteArrayInput.java:362)
    at com.example.tutorial.UserId$1.mergeFrom(UserId.java:138)
    at com.example.tutorial.UserId$1.mergeFrom(UserId.java:1)
    at com.dyuproject.protostuff.IOUtil.mergeFrom(IOUtil.java:43)
    ... 2 more

我是在尝试一些不可能的事情,还是我只是做错了什么?

4

1 回答 1

4

ProtostuffIOUtil问题很简单:我不需要使用(反)序列化我的消息,而是使用ProtobufIOUtil

于 2012-07-04T08:54:43.957 回答