java 使用 protobuf 进行序列化时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15908268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:15:18  来源:igfitidea点击:

error with serialization with protobuf

javaserializationprotocol-buffers

提问by Fynn

I'm trying to serialize a structure with protobuf. after many hours trying to figure out what I'm doing wrong I decided to test the google's example and it didn't worked as well

我正在尝试使用 protobuf 序列化一个结构。经过几个小时试图找出我做错了什么后,我决定测试谷歌的例子,但效果不佳

I have the following protocol from google (https://developers.google.com/protocol-buffers/docs/javatutorial):

我有以下来自谷歌的协议(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;
}

and I'm trying to serialize it with:

我正在尝试将其序列化:

Person john = Person.newBuilder()   
    .setId(1234)
    .setName("John Doe")
    .setEmail("[email protected]")
    .addPhone(
        Person.PhoneNumber.newBuilder()
            .setNumber("555-4321")
            .setType(Person.PhoneType.HOME))
    .build();

byte[] serialized = john.toByteArray();

字节 [] 序列化 = john.toByteArray();

and I get "java.lang.UnsupportedOperationException: This is supposed to be overridden by subclasses."

我得到“java.lang.UnsupportedOperationException:这应该被子类覆盖。”

Thanks;

谢谢;

回答by Bruce Martin

As Marc said, A mismatch in Protocol Buffer versions will give you this exact message. In particular if

正如马克所说,协议缓冲区版本不匹配会给你这个确切的信息。特别是如果

  • The .proto definition is converted to java using the 2.4.3 (or earlier) protoc.exe
  • You use the 2.5.0 protobuffers library
  • 使用 2.4.3(或更早版本)protoc.exe 将 .proto 定义转换为 java
  • 您使用 2.5.0 protobuffers 库

you will get this message in many methods (e.g. getParserForType, getUnknownFields) of class GeneratedMessage. There are no doubt other potential mismatch's that will cause this error

您将在GeneratedMessage类的许多方法(例如 getParserForType、getUnknownFields)中获得此消息。毫无疑问,其他潜在的不匹配会导致此错误



With protocol buffers 2.5.0it is essentialyou regenerateall java classes with the 2.5.0 version of protoc (or on windows protoc.exe).

使用协议缓冲区 2.5.0,您必须使用 2.5.0 版本的 protoc(或在 Windows protoc.exe 上)重新生成所有 java 类。



If you do the reverse - run code generated by protoc version 2.5with the libraries for protocol buffers version 2.4. You will get the following message

如果您使用协议缓冲区 2.4 版的库执行protoc 2.5 版生成的反向运行代码。您将收到以下消息

java.lang.VerifyError: class xxx.xxx.xx.. 
overrides final method getUnknownFields.()Lcom/google/protobuf/UnknownFieldSet;