|
| 1 | +.. _proto_serializers: |
| 2 | + |
| 3 | +Proto Serializers |
| 4 | +================= |
| 5 | + |
| 6 | +The serializers work almost exactly the same with REST framework's ``Serializer`` |
| 7 | +class and ``ModelSerializer``, but use ``message`` instead of ``data`` as |
| 8 | +input and output. |
| 9 | + |
| 10 | + |
| 11 | +Declaring serializers |
| 12 | +--------------------- |
| 13 | + |
| 14 | +Declaring a serializer looks very similar to declaring a rest framework |
| 15 | +serializer:: |
| 16 | + |
| 17 | + from rest_framework import serializers |
| 18 | + from django_grpc_framework import proto_serializers |
| 19 | + |
| 20 | + class PersonProtoSerializer(proto_serializers.ProtoSerializer): |
| 21 | + name = serializers.CharField(max_length=100) |
| 22 | + email = serializers.EmailField(max_length=100) |
| 23 | + |
| 24 | + class Meta: |
| 25 | + proto_class = hrm_pb2.Person |
| 26 | + |
| 27 | + |
| 28 | +Overriding serialization and deserialization behavior |
| 29 | +----------------------------------------------------- |
| 30 | + |
| 31 | +A proto serializer is the same as one rest framework serializer, but we are |
| 32 | +adding the following logic: |
| 33 | + |
| 34 | +- Protobuf message -> Dict of python primitive datatypes. |
| 35 | +- Protobuf message <- Dict of python primitive datatypes. |
| 36 | + |
| 37 | +If you need to alter the convert behavior of a serializer class, you can do so |
| 38 | +by overriding the ``.message_to_data()`` or ``.data_to_message`` methods. |
| 39 | + |
| 40 | +Here is the default implementation:: |
| 41 | + |
| 42 | + from google.protobuf.json_format import MessageToDict, ParseDict |
| 43 | + |
| 44 | + class ProtoSerializer(BaseProtoSerializer, Serializer): |
| 45 | + def message_to_data(self, message): |
| 46 | + """Protobuf message -> Dict of python primitive datatypes. |
| 47 | + """ |
| 48 | + return MessageToDict( |
| 49 | + message, including_default_value_fields=True, |
| 50 | + preserving_proto_field_name=True |
| 51 | + ) |
| 52 | + |
| 53 | + def data_to_message(self, data): |
| 54 | + """Protobuf message <- Dict of python primitive datatypes.""" |
| 55 | + return ParseDict( |
| 56 | + data, self.Meta.proto_class(), |
| 57 | + ignore_unknown_fields=True |
| 58 | + ) |
| 59 | + |
| 60 | +The default behavior requires you to provide ``ProtoSerializer.Meta.proto_class``, |
| 61 | +it is the protobuf class that should be used for create output proto message |
| 62 | +object. You must either set this attribute, or override the |
| 63 | +``data_to_message()`` method. |
| 64 | + |
| 65 | + |
| 66 | +Serializing objects |
| 67 | +------------------- |
| 68 | + |
| 69 | +We can now use ``PersonProtoSerializer`` to serialize a person object:: |
| 70 | + |
| 71 | + >>> serializer = PersonProtoSerializer(person) |
| 72 | + >>> serializer.message |
| 73 | + name: "amy" |
| 74 | + |
| 75 | + >>> type(serializer.message) |
| 76 | + <class 'hrm_pb2.Person'> |
| 77 | + |
| 78 | + |
| 79 | +Deserializing objects |
| 80 | +--------------------- |
| 81 | + |
| 82 | +Deserialization is similar:: |
| 83 | + |
| 84 | + >>> serializer = PersonProtoSerializer(message=message) |
| 85 | + >>> serializer.is_valid() |
| 86 | + True |
| 87 | + >>> serializer.validated_data |
| 88 | + OrderedDict([('name', 'amy'), ('email', '[email protected]')]) |
| 89 | + |
| 90 | + |
| 91 | +ModelProtoSerializer |
| 92 | +-------------------- |
| 93 | + |
| 94 | +This is the same as a rest framework ``ModelSerializer``:: |
| 95 | + |
| 96 | + from django_grpc_framework import proto_serializers |
| 97 | + from hrm.models import Person |
| 98 | + import hrm_pb2 |
| 99 | + |
| 100 | + |
| 101 | + class PersonProtoSerializer(proto_serializers.ModelProtoSerializer): |
| 102 | + class Meta: |
| 103 | + model = Person |
| 104 | + proto_class = hrm_pb2.Person |
| 105 | + fields = '__all__' |
0 commit comments