Search code examples
pythonjsonserializationprotocol-buffers-3

Protobuf3: Serialize a Python object to JSON


According to the manual, Protobuf 3.0.0 supports JSON serialization:

A well-defined encoding in JSON as an alternative to binary proto encoding.

What have I tried

  • json.dumps(instance) which raised TypeError(repr(o) + " is not JSON serializable")
  • Looked for a instance.to_json() (or alike) function
  • Searched the Python docs

How do I serialize a Python proto object to JSON?


Solution

  • There is a function MessageToJson in the json_format module. This function can be used to serialize the message.