1

我需要使用.desc文件来读取序列化的协议缓冲区消息并将它们转换为 JSON(使用 jansson)。

这是因为协议缓冲区消息格式将比 C 代码更频繁地更改。这些.desc文件将是可执行文件的运行时输入。

我找到了https://github.com/Sannis/protobuf2json-c但我对此的解读是它需要生成 C 代码。特别是ProtobufCMessage需要存在被解码的消息,并且我看不到在/usr/include/google/protobuf-c/protobuf-c.h不生成 C 代码的情况下制作 ProtobufCMessage (from ) 的方法。

我在这里遗漏了什么,还是需要编写新代码?

4

1 回答 1

2

I'm not familiar with the .desc extension but I'm guessing from the context that it is a file containing a protobuf FileDescriptorProto, defined in google/protobuf/descriptor.proto.

To do what you want, you will most likely need to use the Protobuf C++ or Java library, each of which defines a class DynamicMessage which has the ability to emulate arbitrary message types based on descriptors. You can then combine this with any Protobuf-JSON library that is based on the standard Protobuf reflection interfaces. (You can also write your own JSON converter pretty easily; use the TextFormat class (found in both the C++ and Java Protobuf libs) as a template.)

My understanding is that protobuf-c does not currently contain an equivalent to DynamicMessage.

于 2015-02-11T05:05:42.443 回答