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?
Protobuf, the binary format crafted by Google, surpasses JSON performance even on JavaScript environments like Node. js/V8 and web browsers.
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.
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).
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.
I read the source code for this, and turns out that you can pass an option preserving_proto_field_name=True
to MessageToJson
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With