Protocol buffers 定义一组预填充的protobuf消息?

Protocol buffers 定义一组预填充的protobuf消息?,protocol-buffers,Protocol Buffers,我希望能够按名称提供预先实例化的消息,而不是从头开始填充它。例如,给定以下模式: message Animal { required int32 num_legs = 1; required int32 num_eyes = 2; } message Zoo { repeated Animal animals; } 我希望能够通过从一组已知动物中进行选择,在配置文件中快速定义动物园: // config.json zoo: { animals: [snake, bird] }

我希望能够按名称提供预先实例化的消息,而不是从头开始填充它。例如,给定以下模式:

message Animal {
  required int32 num_legs = 1;
  required int32 num_eyes = 2;
}

message Zoo {
  repeated Animal animals;
}
我希望能够通过从一组已知动物中进行选择,在配置文件中快速定义动物园:

// config.json
zoo: {
  animals: [snake, bird]
}
蛇和鸟的定义如下:

// animals.json
bird: {
  num_legs: 2
  num_eyes: 2
}

snake: {
  num_legs: 0
  num_eyes: 2
}

做这件事最优雅的方法是什么?

protobuf API有转换protobuf的方法⬌JSON。对于C++,你可以使用其他语言,也有他们的API版本(你没有指定语言)。将其封装在一个简单的helper函数中,并使用您的语言的多行字符串常量语法将JSON格式的消息直接嵌入到源代码中

要获取命名的预定义消息,请使用具有字符串插值的语言。(不限于C++,不幸的是,但是)