如何使 protobuf 不区分大小写?

How do I make protobuf case-insensitive?

我有一个这样的 protobuf 合约,

message Car{ string carId = 1; }

我从这个合同生成 java 类 并用它来解析 JSON 请求。

现在,如果我的 JSON 有 "CarID" 或 "carid",则生成的 protobuf java 类 无法识别该字段。如何使其不区分大小写?

protobuff 描述符 (.proto) 不区分大小写。如果你尝试编译:

message Car{
    string carId = 1;
    string carid =2;
}

你会出现编译错误:

CARID_FIELD_NUMBER is already defined in ...

您还必须知道,对于 proto3,JSON 解析器正在处理 lowerCamelCase。如参考指南所述: https://developers.google.com/protocol-buffers/docs/proto3#json

Use proto field name instead of lowerCamelCase name: By default proto3 JSON printer should convert the field name to lowerCamelCase and use that as the JSON name. An implementation may provide an option to use proto field name as the JSON name instead. Proto3 JSON parsers are required to accept both the converted lowerCamelCase name and the proto field name.

从您的解析器角度来看,"carID" 和 "CarID" 是相同的,因为它会自动将 "CarID" 转换为 "carID"。但是 "carId" 和 "carid" 总是不同的。