天道酬勤,学无止境

protobuf-java

How to log Protobuf string in nested objects in a human-readable way?

Given a proto file: syntax = "proto3"; package hello; message TopGreeting { NestedGreeting greeting = 1; } message NestedGreeting { Greeting greeting = 1; } message Greeting { string message = 1; } and the code: public class Main { public static void main(String[] args) { System.out.printf("From top: %s%n", newGreeting("오늘은 무슨 요일입니까?")); System.out.printf("Directly: %s%n", "오늘은 무슨 요일입니까?"); System.out.printf("ByteString: %s", newGreeting("오늘은 무슨 요일입니까?").toByteString().toStringUtf8()); } private static Hello.TopGreeting newGreeting(String message) { Hello.Greeting greeting = Hello.Greeting

2021-11-26 02:38:31    分类:问答    serialization   protocol-buffers   protobuf-java

Protobuf-3:如何解决缺少的方法“getIdCase()”? 使用“--experimental_allow_proto3_optional”生成的类(Protobuf-3: How to resolve missing method "getIdCase()" ? Classes generated with '--experimental_allow_proto3_optional ')

问题 我使用了 proto3_optional,并且在“可选”字段上缺少这个 xxCase() 方法。 有解决方法吗? 生成的消息类“muni.model.Model$Person”缺少方法“getId Case ” NoSuchMethodException: muni.model.Model$Person.getId Case() 注意:我需要保留用于验证对象的 hasXX() 方法和用于反序列化的 JsonFormat.printer(myproto) 方法。 原型对象 message Person{ //Output only optional string id = 3; // "muni.model.Model$Person" missing method "getIdCase" optional string firstName = 4; // optional added to test hasFirstName() optional string lastName = 5; optional string email = 6; optional string phone1 = 7; optional string phone2 = 8; PostalAddress address = 9; //For objects no need for optional

2021-10-30 16:04:08    分类:技术分享    protocol-buffers   protobuf-java

方法 emptyIntList() 未定义(The method emptyIntList() is undefined)

问题 我是Google Protobuf 的新手。 尝试使用下面的student.proto文件玩它。 syntax = "proto3"; package rld; option java_package = "com.rld"; option java_outer_classname = "StudentDTO"; message Student { string name = 1; int32 roll = 2; repeated int32 mark = 3; //Marks in various subjects } message StudentDatabase { repeated Student student = 1; } 然后我尝试使用下面的 Protobuf 编译器命令编译它。 从这里,我下载了编译器。 protoc -I=. --java_out=. ./student.proto 它编译成功并生成StudentDTO.java ,但有错误。 类中有两个方法“ emptyIntList() ”和“ newIntList() ”,但它们没有被定义。 现在我的问题是如何解决这些错误或者我错过了什么? 回答1 作为新手,我似乎忘记在更新Protobuf Compiler时更新Google Protobuf Runtime 。 两个版本必须匹配。 这里有2个解决方案

2021-10-24 14:54:06    分类:技术分享    protobuf-java   google-protocol-buffer

Protoc Java 插件的依赖关系?(Dependencies for Protoc Java plugin?)

问题 我正在尝试在 How to write a custom Protobuf CodeGenerator in Java 中制作工作示例。 当我尝试编译文件时 import com.google.protobuf.compiler.PluginProtos; import java.io.IOException; public class MyPlugin { public static void main(String[] args) throws IOException { CodeGenerator gen = new CodeGenerator(); PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(System.in); codeGeneratorRequest.getProtoFileList().forEach(gen::handleFile); // get the response and do something with it //PluginProtos.CodeGeneratorResponse response = PluginProtos.CodeGeneratorResponse

2021-10-23 22:27:49    分类:技术分享    java   protocol-buffers   protoc   protobuf-java

Protocol Buffers:如何在 Java 中解析 .proto 文件(Protocol Buffers: How to parse a .proto file in Java)

问题 我正在尝试动态解析 Java 中的给定 .proto 文件以解码 Protobuf 编码的二进制文件。 我有以下解析方法,其中“proto”字符串包含 .proto 文件的内容: public static Descriptors.FileDescriptor parseProto (String proto) throws InvalidProtocolBufferException, Descriptors.DescriptorValidationException { DescriptorProtos.FileDescriptorProto descriptorProto = DescriptorProtos.FileDescriptorProto.parseFrom(proto.getBytes()); return Descriptors.FileDescriptor.buildFrom(descriptorProto, null); } 但是,在执行时,前一个方法会引发异常,并显示消息“协议消息标签的线路类型无效。”。 我使用来自 Google 的示例 .proto 文件,所以我想它是有效的:https://github.com/google/protobuf/blob/master/examples/addressbook.proto 这是堆栈跟踪: 15:43

2021-10-20 15:04:24    分类:技术分享    java   protocol-buffers   protobuf-java

The method emptyIntList() is undefined

I am new to Google Protobuf. Tried to play with it using below student.proto file. syntax = "proto3"; package rld; option java_package = "com.rld"; option java_outer_classname = "StudentDTO"; message Student { string name = 1; int32 roll = 2; repeated int32 mark = 3; //Marks in various subjects } message StudentDatabase { repeated Student student = 1; } Then I tried to compile it using below Protobuf compiler command. From here, I downloaded the compiler. protoc -I=. --java_out=. ./student.proto It compiled successfully and generated StudentDTO.java, but with errors. There are two methods

2021-10-01 11:08:56    分类:问答    protobuf-java   google-protocol-buffer

Protobuf-3: How to resolve missing method "getIdCase()" ? Classes generated with '--experimental_allow_proto3_optional '

I used the proto3_optional, and getting this xxCase() method missing on field that are 'optional'. Is there a workaround for this? Generated message class "muni.model.Model$Person" missing method "getIdCase" NoSuchMethodException: muni.model.Model$Person.getIdCase() Note: I need to retain hasXX() method working for validating objects, and JsonFormat.printer(myproto) for deserializing. Proto object message Person{ //Output only optional string id = 3; // "muni.model.Model$Person" missing method "getIdCase" optional string firstName = 4; // optional added to test hasFirstName() optional string

2021-09-23 20:36:52    分类:问答    protocol-buffers   protobuf-java

Dependencies for Protoc Java plugin?

I'm trying to make working example at How to write a custom Protobuf CodeGenerator in Java . When I try to compile a file with import com.google.protobuf.compiler.PluginProtos; import java.io.IOException; public class MyPlugin { public static void main(String[] args) throws IOException { CodeGenerator gen = new CodeGenerator(); PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(System.in); codeGeneratorRequest.getProtoFileList().forEach(gen::handleFile); // get the response and do something with it //PluginProtos.CodeGeneratorResponse response

2021-09-08 08:21:39    分类:问答    java   protocol-buffers   protoc   protobuf-java

How to deal with unknown protobuf fields in Java?

I have a Java application that reads some protobuf data from another computer and can then modify some values and write it back. It is very likely that a user could read the data using an outdated .proto file, so there would be some fields it doesn't understand in this case. I would ultimately like to preserve the uknown data when writing back the changes made; however, I could settle for just detecting that there is unknown data (to prompt the user to upgrade his/her application). It is not clear to me how to deal with unknown fields in Java. If it helps, I am using a version 2 .proto file

2021-07-30 15:29:46    分类:问答    java   protocol-buffers   protobuf-java

How can I add my own code to JAVA generated classes from proto file?

I'm using protobuf and I'm generating JAVA classes from the following proto file. syntax = "proto3"; enum Greeting { NONE = 0; MR = 1; MRS = 2; MISS = 3; } message Hello { Greeting greeting = 1; string name = 2; } message Bye { string name = 1; } option java_multiple_files = true; Now I need to add some code to the generated files and I found that is possible using a custom plugin (https://developers.google.com/protocol-buffers/docs/reference/java-generated#plugins). I'm trying to generate that plugin in Java, something like this. public class Test { PluginProtos.CodeGeneratorResponse

2021-07-30 13:06:49    分类:问答    protocol-buffers   protoc   protobuf-java