如果這個工具幫到了你,可以請作者喝杯咖啡 ☕
将JSON数据转换为Protobuf消息定义
tools.json-to-protobuf.inputs.snakeCaseHint
tools.json-to-protobuf.results.emptyMessage
在开发中使用Protobuf进行数据交换时,手动编写.proto文件容易出错且耗时。本工具能将JSON数据自动转换为对应的Protobuf消息定义,根据JSON字段值类型推断为string、int32、double、bool等Protobuf类型,并支持嵌套对象和数组,快速生成可用的Proto消息结构。
示例输入JSON:
{
"name": "Alice",
"age": 25,
"active": true,
"scores": [98, 87],
"address": {
"street": "Main St",
"city": "NY"
}
}
示例输出Protobuf:
syntax = "proto3";
message AutoGenerate {
string name = 1;
int32 age = 2;
bool active = 3;
repeated int32 scores = 4;
Address address = 5;
}
message Address {
string street = 1;
string city = 2;
}
输入的JSON必须为合法的对象或数组格式,空值(null)会被忽略或提示调整。自动生成的字段编号按JSON key顺序递增,如需固定编号请手动修改。本工具仅在浏览器本地处理数据,不会将内容发送至任何服务器,请放心使用。
生成的.proto文件可以作为基础,但仍建议检查字段类型是否符合实际需求(例如将int32调整为sint32、fixed64等)。对于复杂业务模型,可在生成消息框架后添加枚举、oneof等高级结构。常见转换规则:JSON数组元素若为相同类型则生成repeated字段;若数组元素类型不一致,工具可能尝试生成oneof或报错,此时建议先统一数据格式。