protobuf MessageToJson删除值为0的字段 [英] protobuf MessageToJson removes fields with value 0

查看:84
本文介绍了protobuf MessageToJson删除值为0的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个接收protobuf的Python脚本,将其转换为json对象,然后将其推送到另一个服务.我使用 json.loads(MessageToJson(protobuf))将protobuf转换为python字典对象.后来我用 json.dumps(dictionary)将其转换回json.

I'm writing a Python script that receives protobufs, converts them to json objects, and pushes them to another service. I use json.loads(MessageToJson(protobuf)) to convert the protobuf to a python dictionary object. Later I convert it back to json with json.dumps(dictionary).

我有一个带有可选枚举字段的原型,例如:

I have a proto with an optional enumerated field such as:

enum C_TYPE
{
    AB = 0;
    BC = 1;
    CD = 2;
}

当我收到带有指定为 BC 的字段的原型时,一切都会按我的预期进行.当我收到带有指定为 AB 的字段的原型时,该字段将被忽略-在python字典或后续的json转储中不会出现.我发现了一种解决方法是使用 json.loads(MessageToJson(protobuf,includes_default_value_fields = True))但这将为所有缺少的字段创建默认值,而不仅仅是具有 0的字段枚举.这意味着缺少枚举 0 的字段-但不是!

When I receive a proto with a field designated as BC everything works as I expect it. When I receive a proto with a field designated AB that field gets ignored -- it does not turn up in the python dictionary or subsequent json dump. A workaround I have found is to use json.loads(MessageToJson(protobuf, including_default_value_fields=True)) but that will create default values for all missing fields, not just the ones that have a 0 enumeration. It implies that the field with enumeration 0 is missing - but it's not!

将枚举字段的值设置为 0 时正确的方法是什么?

What is the correct way to retrieve the value of the enumeration field when it is set to 0?

推荐答案

没有正确的方法,我错误地定义了protobuf.对于枚举字段,第一个值为 为默认值.这意味着如果protobuf通过时没有设置值,则会将其设置为默认值,并且在转换为json时会被忽略(除非您希望保留所有默认值.)

There is no correct way, I'm defining my protobufs incorrectly. For enumerated fields, the first value is the default value. This means if a protobuf comes through without a set value, it is set to the default value and, when converted to json, ignored (unless you want to keep all default values.)

因此,建议对默认值使用一次性使用的名称,以便能够正确地区分何时设置了默认值.即我应该将protobuf定义为:

Thus, it is recommended use throw-away names for the default value to be able to properly distinguish when it has been set. i.e. I should define my protobuf as:

enum C_TYPE
{
    NONE = 0;
    AB = 1;
    BC = 2;
    CD = 3;
}

来自有关可选字段和默认值的Protobuf文档:

对于枚举,默认值为枚举列表中列出的第一个值类型定义.这意味着在将值添加到时必须小心枚举值列表的开头.

For enums, the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list.

另外来自golang/protobuf上的问题:

Additionally from an issue on golang/protobuf:

这正在按预期方式工作.proto3零值在JSON格式.零值应该是丢弃"值:如果序列化邮件的发件人设置了字段为无效或无法识别的值.

This is working as intended. proto3 zero-values are omitted in the JSON format too. The zero-value should be a "throwaway" value: it's also what you will see if the sender of a serialized message sets the field to an invalid or unrecognized value.

这篇关于protobuf MessageToJson删除值为0的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆