首页 > 解决方案 > 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。模式会改变,所以我不能使用生成类的方法。

这是测试代码。

标签: javaprotocol-buffersprotoprotobuf-java

解决方案


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


推荐阅读