简体   繁体   English

JSON的原始反序列化将在新字段或未知枚举值上失败

[英]Proto deserialization from json will fail on new fields or unknown enum values

proto is designed so that introduction of new fields should not break your code that runs on older version. proto的设计宗旨是使引入新字段不会破坏您在旧版本上运行的代码。

But if you're using json conversion in java using protobuf-java-util then your old code will break on new fields or new enum values unknown to existing code... 但是,如果您使用protobuf-java-utiljava使用json转换,那么您的旧代码将在现有代码未知的新字段或新枚举值上中断...

I've opened an issue on github but it does not get any attention. 我在github上打开了一个问题 ,但是没有引起任何注意。 I hope to get some answers here. 我希望在这里得到一些答案。

Given message like this 给定这样的消息

message Msg {
    required string sender = 1;
    required string message = 2;
}

if we convert this to json and print using 如果我们将其转换为json并使用

JsonFormat.printer().print(msg)

we will have this result 我们将得到这个结果

{
  "sender": "me",
  "message": "message"
}

if we decode this json to Msg using its builder 如果我们使用其生成器将此json解码为Msg

JsonFormat.parser().merge(json, builder)

we will get expected result... But if we try to decode similar json with additional extra field 我们将获得预期的结果...但是,如果我们尝试使用其他extra字段来解码相似的json

{
  "sender": "me",
  "message": "message",
  "extra" : "some extra stuff"
}

we will fail with this exception 我们将因这个例外而失败

com.google.protobuf.InvalidProtocolBufferException: Cannot find field: extra in message proto.Msg

How is that nobody cares about this problem? 没有人关心这个问题怎么办? So far my only solution to this is to write a parser from scratch that will ignore unknown fields and unknown enum values when parsing... 到目前为止,我对此的唯一解决方案是从头开始编写解析器,该解析器将在解析时忽略未知字段和未知枚举值。

Is there something i overlooked or people simply dont use backwards compatibility features? 有什么我忽略的东西吗?或者人们根本不使用向后兼容功能?

该问题已由Google修复,构建器提供了忽略未知字段的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM