简体   繁体   中英

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.

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...

I've opened an issue on github but it does not get any attention. 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

JsonFormat.printer().print(msg)

we will have this result

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

if we decode this json to Msg using its builder

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

we will get expected result... But if we try to decode similar json with additional extra field

{
  "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修复,构建器提供了忽略未知字段的选项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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