google.protobuf.json_format.MessageToJson changes names of fields. How to avoid it?

♀尐吖头ヾ 提交于 2019-12-11 02:01:36

问题


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 changed it fields to:

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

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


回答1:


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



来源:https://stackoverflow.com/questions/43835243/google-protobuf-json-format-messagetojson-changes-names-of-fields-how-to-avoid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!