Protobuf 动态消息解析

时间:2021-07-28 09:56:49

标签: java protocol-buffers proto protobuf-java

我正在尝试使用 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 名称解析 Any 字段。但我没有地方可以做。

附言迭代生成的 dynamicMessage 并检查字段是否为 Any 然后解析它似乎不是一个好主意,因为我将嵌套 any。

我的主要目标是通过 protobuf 模式解析消息并转换为 json。模式会改变,所以我不能使用生成类的方法。

这是测试代码。

1 个答案:

答案 0 :(得分:0)

用递归方法解决。我遍历所有字段并检查是否有任何字段并通过 type_url 解析它

相关问题