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)
{
...
}