2 回答

TA贡献1853条经验 获得超9个赞
暂时忽略 JSON;在大多数语言/框架中,您将通过名称访问这些属性,无论是获取值还是检查设置了哪一个。如果名称冲突:你不能这样做。
另外:anyof
允许同一类型用于受歧视联合中的多个成员,在这种情况下,您想要做的事情会变得更加混乱。
最后,回到 JSON:解析器看到"properties":
-接下来它期望什么?一旦解析了该值,在可区分联合中哪个字段被视为“设置”?
所以不,出于多种原因:这是不允许的。

TA贡献1831条经验 获得超10个赞
我已经使用这种方式解决了 JSON 序列化的类似用例。
message MyResponse {
int32 a = 1;
string b = 2;
oneof Properties {
PropertiesType1 properties1 = 3 [json_name = "properties"];
PropertiesType2 properties2 = 4 [json_name = "properties"];
PropertiesType3 properties3 = 5 [json_name = "properties"];
PropertiesType4 properties4 = 6 [json_name = "properties"];
}
}
如果您使用 protoc 编译器,这会起作用,但它不适用于 buf lint/build 等高级工具。希望这可以帮助。
添加回答
举报