3

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?

Adam Matan
  • 128,757
  • 147
  • 397
  • 562

2 Answers2

7

Caveats

I have mistakingly installed protobuf3 - I thought it the, well, protobuf3 Python package, but it's an unofficial Python 3 protobuf 2 package, not the other way around. Remove it before you start.

Solution

After some trial and error, the following solution works. Feel free to post better / official ones if you have any.

Prerequisite: Protobuf 3

  • Remove protobuf2 (I used brew uninstall). Make sure protoc does not appear in the path.
  • Install the protobuf3 binaries. There is no homebrew package yet, so I used OSX binaries protoc-3.0.0-osx-x86_64.zip. The make script is also an option.
    • Copy the content of the bin directory to /usr/local/bin
    • Copy the content of the include to /usr/local/include
  • Make sure protobuf3 is installed - protoc --version should show libprotoc 3.0.0.

Python installation

  • Create a virtual environment
  • Download the master branch of protobuf to /tmp
  • Activate the virtual environment
  • cd protobuf-master/python && setup.py install

Code

The relevant function is MessageToJson in the google.protobuf.json_format module:

from google.protobuf import json_format
o = SomeProtobufClass()
print json_format.MessageToJson(o)
{
...
}
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
4

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

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
rfkortekaas
  • 6,049
  • 2
  • 27
  • 34
  • 1
    Thanks. Just found out about it (see my own answer). My problem was elsewhere, in the package installation (installed the wrong one). – Adam Matan Aug 03 '16 at 10:50