如果這個工具幫到了你,可以請作者喝杯咖啡 ☕
將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或報錯,此時建議先統一資料格式。