Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 984
Kenenbek Arzymatov Avatar asked May 07 '17 18:05

Kenenbek Arzymatov


People also ask

Is protobuf more efficient than JSON?

Protobuf, the binary format crafted by Google, surpasses JSON performance even on JavaScript environments like Node. js/V8 and web browsers.

Should I use protobuf or JSON?

Protobuf is mostly useful for internal services whereas JSON is mostly useful for web applications. Prior knowledge of schema is essential in decoding Protobuf messages, whereas data can be easily decoded or parsed in JSON with knowing schemas in advance.

Is protobuf human-readable?

Protobuf is a binary serialization protocol developed by Google. Since it serializes to binary, it is not human readable (although you can still pick out strings when viewing the file as ASCII text, see the example below).

How is date defined in proto?

proto file contains examples of Timestamp using, including related to Linux and Windows programs. Example 1: Compute Timestamp from POSIX time() . Timestamp timestamp; timestamp. set_seconds(time(NULL)); timestamp.


1 Answers

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

like image 94
skgbanga Avatar answered Oct 31 '22 18:10

skgbanga