clarkduvall / serpy

ridiculously fast object serialization
http://serpy.readthedocs.org/en/latest/
MIT License
960 stars 62 forks source link

Add support for DRF-like `Meta` classes inside custom serializer #72

Open jacobstoehr opened 4 years ago

jacobstoehr commented 4 years ago

If pulled, this would extend serpy with the ability to accept a Meta class inside the custom serializer class. The syntax for this class is heavily borrowed from DRF's ModelSerializer, e.g. it needs a model (either Django or SQLAlchemy) and an exclude or fields attribute which tell serpy which fields to exclude or include in the serialization.

This was inspired by the need to serialize a very big Django model but with greater speed than what DRF provides. However, this had the drawback of also needing to specifiy each field to be serialized in the Serializer, instead of just inserting a Meta class and be done.

I have done some basic benchmarks, this does not negatively impact the standard serialization speed, because of the top level if meta: statement, which will just skip the code if no Meta class is provided. If the class has been provided, the serialization speed doesn't suffer, at least on my machine. I have not conducted any kind of statistical benchmarking, just quick tests using the UNIX time utility.

I wanted to keep this code inside the SerializerMeta, so that the implicit fields are calculated at startup time, not at serialization time. This however means all serializers inheriting from serpy.Serializer will have this ability, not like DRF where only serializers inheriting from rest_framework.serializers.ModelSerializer will have this ability.

coveralls commented 4 years ago

Coverage Status

Coverage decreased (-18.0%) to 82.0% when pulling f0e578b1a6b67b017ec47a6cf5b040630ce36f97 on jacobstoehr:master into 0c58fb897e60c572f5d21f93966b3e6d8a91c305 on clarkduvall:master.