我正在尝试使用 protobuf 序列化结构。经过几个小时试图找出我做错了什么后,我决定测试谷歌的示例,但效果不佳
我有来自谷歌的以下协议(https://developers.google.com/protocol-buffers/docs/javatutorial):
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
repeated PhoneNumber phone = 4;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
}
message AddressBook {
repeated Person person = 1;
}
我正在尝试使用以下方法对其进行序列化:
Person john = Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe@example.com")
.addPhone(
Person.PhoneNumber.newBuilder()
.setNumber("555-4321")
.setType(Person.PhoneType.HOME))
.build();
字节[] 序列化 = john.toByteArray();
我得到“java.lang.UnsupportedOperationException:这应该被子类覆盖”。
谢谢;