将Jason反序列化为ProtoBuffer消息时,空值设置为默认值。

huangapple go评论60阅读模式
英文:

When deserializing Jason to ProtoBuffer message null values set to default values

问题

I'm using ProtoBuffer 3 and I have a message that is composed of multiple fields and some of them are other messages that contains some (or all) nullable values.

For example:

message MainMessage {
   google.protobuf.StringValue title = 1;
   NotNullable sub1 = 2;
   Nullable sub2 = 3;
   NotNullable sub3 = 4;
}

message NotNullable {
    int64 id = 1;
    string value = 2;
}

message Nullable {
    google.protobuf.Int64Value id = 1;
    google.protobuf.Int64Value value = 2;
}

I'm getting the message as a son and I want to deserialize it using the following code:

String json = "{\"title\":{\"value\":\"A nice title\"},\"sub1\":{\"id\":{\"value\":1}}}";
MainMessage.Builder message = MainMessage.newBuilder();
JsonFormat.parser().merge(json, message);

After running the code above the object that I'm getting contains all of the fields (even that sub2 and sub3 are not in the JSON) where the missing data is init to the default value.

For example, the following code will return true:

assertTrue(message.getSub2().getId().isInitialized());
assertTrue(message.getSub3().isInitialized());

I checked the documentation but couldn't find a way to tell the parser not to set those fields - is there such an option?

英文:

I'm using ProtoBuffer 3 and I have a message that is composed of multiple fields and some of them are other messages that contains some (or all) nullable values.

For example:

message MainMessage {
   google.protobuf.StringValue title = 1;
   NotNullable sub1 = 2;
   Nullable sub2 = 3;
   NotNullable sub3 = 4;
}

message NotNullable {
    int64 id = 1;
    string value = 2;
}

message Nullable {
    google.protobuf.Int64Value id = 1;
    google.protobuf.Int64Value value = 2;
}

I'm getting the message as a son and I want to deserialize it using the following code:

String json = "{\"title\":{\"value\":\"A nice title\"},\"sub1\":{\"id\":{\"value\":1}}}";
MainMessage.Builder message = MainMessage.newBuilder();
JsonFormat.parser().merge(json, message);

After running the code above the object that I'm getting contains all of the fields (even that sub2 and sub3 are not in the JSON) where the missing data is init to the default value.

for example, the following code will return true:

assertTrue(message.getSub2().getId().isInitialized())
assertTrue(message.getSub3().isInitialized())

I checked the documentation but couldn't find a way to tell the parse not to set those fields - is there such an option?

答案1

得分: 0

这篇文章似乎解决了这个问题:https://itnext.io/protobuf-and-null-support-1908a15311b6

我实现的方式如下(对于名称的混淆,我想保留原始名称,所以抱歉):

message MainMessage {
   google.protobuf.StringValue title = 1;
   NotNullable sub1 = 2;
   Nullable sub2 = 3;
   NotNullable sub3 = 4;
}

message NotNullable {
    int64 id = 1;
    string value = 2;
}

message Nullable {
    google.protobuf.Int64Value id = 1;
    google.protobuf.Int64Value value = 2;
}

message AllowNullable {
    oneof kind {
       google.protobuf.NullValue null = 1;
       Nullable value = 2;
    }
}

现在只需在调用值之前使用 has 方法来检查它是否为null,例如:

if (message.getSub3().hasValue()) {
   // 做一些操作...
}
英文:

It seems like this article solves the issue: https://itnext.io/protobuf-and-null-support-1908a15311b6

The way I've implemented it is as follow (sorry for the confusion with the names - I wanted to keep the original names):

message MainMessage {
   google.protobuf.StringValue title = 1;
   NotNullable sub1 = 2;
   Nullable sub2 = 3;
   NotNullable sub3 = 4;
}

message NotNullable {
    int64 id = 1;
    string value = 2;
}

message Nullable {
    google.protobuf.Int64Value id = 1;
    google.protobuf.Int64Value value = 2;
}

message AllowNullable {
    oneof kind {
       google.protobuf.NullValue null = 1;
       Nullable value = 2;
}

Now all that is required is to use the has method before calling the value to check whether or not it's null

For example:

if(message.getSub3().hasValue()) {
   // do something...
}

huangapple
  • 本文由 发表于 2020年8月14日 15:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63408033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定