Package: protobuf-serialization
Protobuf Serialization Tools for PythonUtils to help with serialization and deserialization of Python objects into protobuf3 (e.g. for your gRPC application). Inspired by serialization libraries like marshmallow, Django rest Framework and serpy, but for protobuf3 instead of JSON.While the Python protobuf library already has a class-based infrastructure, it can be very awkward to use, and there are a lot of operations you are restricted from doing. This library uses a familiar class-based "serializer" interface to make it easy to define how you'd like to map a Python object (such as a Django or SQLAlchemy model instance) or a source dictionary to an output, which in this case is a Python protobuf instance (rather than a dictionary object).Another problem this solves is that protobuf3 doesn't support nullable values for primitives like strings, integers, bool. In real-life applications involving databases, it is very common to have data to sometimes be NULL/None. APIs often also have this need as well - NULL may have an actual meaning that isn't conveyed by the default primitive value (empty string, 0, false). When transitioning from something like a REST API to gRPC, I felt that this was something that is missing.A solution for this is to use Google's wrapper values (like IntValue), which basically wrap a primitive inside a message (which CAN be nullable). However they can be cumbersome to work with. This library automates that away in both serializtion and deserialization directions so you don't have to think about it - values are encoded to a wrapped value, and unwrapped again in the deserialization step.FeaturesWritten for high serialization performanceUses Google's wrapper types to support "nullable" valuesField types for common types (string, int, enum, date, datetime, JSON string), as well as custom fieldsSupports nested serializersConvenience functions for serialization and deserialization100% unit test coverageUsagepip install protobuf-serializationThe core base class for the serializer isProtobufSerializer. There is alsoProtobufDictSerializerto handle dict-like objects instead.This library also comes with a cool utility calledserialize_to_protobuf(), which will basically take a dictionary and output a protobuf. Underneath the hood it will introspect the protobuf class and dynamically generate aProtobufSerializerThere is also a functionprotobuf_to_dict()which does the reverse operation - it takes a protobuf and turns it into a dict. It's similar tohttps://github.com/kaporzhu/protobuf-to-dictbut with some simplications, plus some better support for things like datetimes.ExamplesSerializerfromprotobuf_serialization.tests.compiled.example_pb2importFooclassFooSerializer(ProtobufSerializer):protobuf_class=Fooid=fields.Field()name=fields.Field()content=fields.WrappedStrField()is_active=fields.WrappedField()count=fields.WrappedIntField()created_at=fields.DateTimeField()foo_serializer=FooSerializer()proto=foo_serializer.dump(instance)serialize_to_protobuffromprotobuf_serialization.tests.compiled.example_pb2importFoosource=dict(id=1,name='val',content='stuff',is_active=True,count=12,created_at=created_at)data=serialize_to_protobuf(source,Foo)