如何处理 Java 中未知的 protobuf 字段? [英] How to deal with unknown protobuf fields in Java?

查看:124
本文介绍了如何处理 Java 中未知的 protobuf 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 应用程序,它从另一台计算机读取一些 protobuf 数据,然后可以修改一些值并将其写回.用户很可能可以使用过时的 .proto 文件读取数据,因此在这种情况下会有一些它不理解的字段.我最终想在写回所做的更改时保留未知数据;但是,我可以满足于只检测到未知数据(以提示用户升级他/她的应用程序).我不清楚如何处理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.

如果有帮助,我使用的是版本 2 .proto 文件,因为我需要它与远程计算机上的 nanopb 兼容.

If it helps, I am using a version 2 .proto file because I need it to be compatible with nanopb on the remote computer.

这个问题让我了解了一部分,但我的问题有与 JSON 无关.

This question gets me part of the way, but my question has nothing to do with JSON.

推荐答案

首先请注意说未知字段.在 protobuf 中,根据定义,您可以拥有未知字段,但另一方面 - 我想这就是您的情况 - 您可以拥有当前 proto 文件中没有的字段.

First please pay attention when you say unknown fields. In protobuf you can have unknown fields by definition but on the other hand - and I suppose this is your case - you can have fields that you not having in your current proto file.

在这两种情况下,您都可以轻松访问这些值.假设您有一个名为 foo 的原始消息.

In both situation you can easily access the values. Let say you have a proto message named foo.

您必须访问描述符并按名称从那里获取字段,最后获取如下示例的值:

You have to access the descriptor and get the fields from there by name, and lastly get the values exemplified like below:

Builder builder = foo.toBuilder();
FieldDescriptor field = builder.getDescriptorForType().findFieldByName("whatever field");
Object obj = builder.getField(field);

// if your field is int32 cast to int
int value = (int) obj

如果您想写入未知"值,您可以反过来:

If you wish to write the 'unknown' value you could proceed the other way around:

Builder builder = foo.toBuilder();
FieldDescriptor field = builder.getDescriptorForType().findFieldByName("whatever field");
builder.setField(field, 100); // 100 is an example int value
Foo foo = builder.build();

如果您真的想插入原型定义的未知字段,您必须执行以下操作:

In case if you really want to insert proto defined unknown fields you have to do something like:

 UnknownFieldSet.Field seqField = UnknownFieldSet.Field
            .newBuilder()
            .addFixed32(100) // 100 is an example int value
            .build();

 UnknownFieldSet unkFieldSet = UnknownFieldSet
            .newBuilder()
            .addField(99, seqField) // 99 is a proto index number chosen by me
            .build();

 Foo message = foo.toBuilder().setUnknownFields(unkFieldSet).build();

读取定义的未知字段再次由:

Reading the defined unknown fields are again is done by the:

 foo.toBuilder().getUnknownFields()....

我希望这会有所帮助.

这篇关于如何处理 Java 中未知的 protobuf 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆