google.protobuf.json_format.MessageToJson更改字段名称.如何避免呢? [英] google.protobuf.json_format.MessageToJson changes names of fields. How to avoid it?

查看:1950
本文介绍了google.protobuf.json_format.MessageToJson更改字段名称.如何避免呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些协议缓冲区消息对象.所以我想以这种方式对其进行序列化:

I have some protocol buffer message object. So I want to serialize it in such way:

import json
from google.protobuf.json_format import MessageToJson

with open("file.json", 'w') as fjs:
    fjs.write(MessageToJson(message_object))

但是它更改了对象字段的名称. 例如,我有这样的对象:

But it change the names of object fields. For example I had such object:

[{
    "id": "333333",
    "creation_timestamp": 2011,
}]

MessageToJson 将其字段更改为:

[{
  "id": "333333",
  "creationTimestamp": "2011",
}] 

creation_timestamp更改为creationTimestamp,而2011更改为"2011".如何避免呢?

i.e creation_timestamp is changed to creationTimestamp and 2011 is done to "2011". How to avoid it?

推荐答案

我已阅读了此源代码,事实证明您可以将选项preserving_proto_field_name=True传递给MessageToJson.

I read the source code for this, and turns out that you can pass an option preserving_proto_field_name=True to MessageToJson.

这篇关于google.protobuf.json_format.MessageToJson更改字段名称.如何避免呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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