java - Protobuf DynamicMessage 解析
问题描述
我正在尝试使用 java 解析通用 protobuf 消息。它适用于Oneof
或简单的类型,但Any
我有一个问题。
private static DynamicMessage parseData(byte[] data) throws IOException, Descriptors.DescriptorValidationException {
DescriptorProtos.FileDescriptorSet set = DescriptorProtos.FileDescriptorSet.parseFrom(new FileInputStream("file1.desc");
DescriptorProtos.FileDescriptorSet any = DescriptorProtos.FileDescriptorSet.parseFrom(new FileInputStream("any.desc"));
var anyDesc = Descriptors.FileDescriptor.buildFrom(any.getFile(0), new Descriptors.FileDescriptor[] {});
Descriptors.FileDescriptor
md = Descriptors.FileDescriptor.buildFrom(set.getFile(0), new Descriptors.FileDescriptor[] {anyDesc});
Descriptors.Descriptor messageType = md.getMessageTypes().get(0);
return DynamicMessage.parseFrom(messageType, data);
}
我的架构
syntax = "proto3";
import "google/protobuf/any.proto";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
google.protobuf.Any test = 4;
}
message Test2 {
string one = 1;
string two = 2;
string three = 3;
}
message Test3 {
string one = 1;
}
此测试代码有效,我收到消息
query: "sad"
page_number: 1
result_per_page: 123
test {
type_url: "type.googleapis.com/Test2"
value: "\n\003one\022\003two\032\005three"
}
这就是问题所在。Any
字段未解析。我怎么能做到这一点?
我需要按type_url
名称解析任何字段。但我没有可以做的地方。
PS迭代生成的dynamicMessage并检查字段是否为Any然后解析它似乎不是一个好主意,因为我将嵌套任何。
我的主要目标是通过 protobuf 模式解析消息并转换为 json。模式会改变,所以我不能使用生成类的方法。
这是测试代码。
解决方案
用递归方法解决。我浏览所有字段并检查是否有任何字段并通过 type_url 解析它
推荐阅读
- python - Python 中的 Null 或 None 函数
- javascript - 了解传递给解析的多个参数的顺序,以及赋予 .then() 的值
- javascript - 为什么 Typescript 不要求我的函数返回某种类型?
- python - sqlalchemy.orm.exc.UnmappedInstanceError:类'builtins.dict'未映射
- r - 没有数据时如何避免geom_line或geom_path中的连接线?
- wpf - 在多个网格之间共享列宽
- reactjs - 如何在反应确认警报中重定向到另一个页面?
- arrays - 二维数组中的递归蛮力实现
- python - 如何使用请求下载具有原始名称的文件?
- java - 扫描仪运行直到正则表达式 0-255