Kurt
Kurt

Reputation: 2403

how to use custom encoding/decoding with Google Protobuf

I'm writing a python app that already has the protobuf messages defined. However, we need to use a custom wire format (I believe that's the correct term).

How do you (in python) override the base encoding functions?

I've looked in encoder.py and that's a maze of nested functors. So what do I need to monkey patch (or whatever) to specify my own encodings?

Thanks.

Upvotes: 0

Views: 1249

Answers (1)

Kenton Varda
Kenton Varda

Reputation: 45246

I wouldn't recommend trying to monkey-patch the encoding functions. You will almost certainly break something.

What you can do is write an independent function which encodes a protobuf in an arbitrary way via reflection. For an example of this, see the text_format.py module in Protobufs itself. This module encodes and decodes messages in an alternative human-readable format entirely based on the public descriptor and reflection interfaces. You can follow the same pattern to write your own encoder/decoder for any format.

Upvotes: 1

Related Questions