Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf to json in python

I have an object that I de-serialize using protobuf in Python. When I print the object it looks like a python object, however when I try to convert it to json I have all sorts of problems.

For example, if I use json.dumps() I get that the object (the generated code from protoc) does not contain a _ dict _ error.

If I use jsonpickle I get UnicodeDecodeError: 'utf8' codec can't decode byte 0x9d in position 97: invalid start byte.

Test code below is using jsonpickle with the error shown above.

if len(sys.argv) < 2:     print ("Error: missing ser file")     sys.exit() else :     fileLocation = sys.argv[1]  org = BuildOrgObject(fileLocation)   org = org.Deserialize()   #print (org) jsonObj = jsonpickle.encode(org) print (jsonObj) 
like image 786
exHash Avatar asked Nov 01 '13 20:11

exHash


People also ask

Does Protobuf use JSON?

JSON is limited to certain python objects, and it cannot serialize every python object. Protobuf supports a wider range of data types when compared to JSON. For example, enumerations and methods are supported by Protobuf and not supported by JSON. JSON supports only a subset of python data types.

Is Protobuf better than JSON?

JSON is usually easier to debug (the serialized format is human-readable) and easier to work with (no need to define message types, compile them, install additional libraries, etc.). Protobuf, on the other hand, usually compresses data better and has built-in protocol documentation via the schema.


1 Answers

I'd recommend using protobuf↔json converters from google's protobuf library:

from google.protobuf.json_format import MessageToJson  json_obj = MessageToJson(org) 

You can also serialise the protobuf to a dictionary:

from google.protobuf.json_format import MessageToDict dict_obj = MessageToDict(org) 

Refer to the protobuf package API documentation: https://developers.google.com/protocol-buffers/docs/reference/python/ (see module google.protobuf.json_format).

like image 116
denis-sumin Avatar answered Sep 17 '22 23:09

denis-sumin