Views: move the base64 encoding to the renderers.
Hard-coding the serialization encoding in the serializers is wrong. This fix now enables us to change to easily change to msgpack as the transport layer.master
parent
2880673e27
commit
3dfceb63b1
@ -0,0 +1,18 @@
|
||||
from rest_framework.utils.encoders import JSONEncoder as DRFJSONEncoder
|
||||
from rest_framework.renderers import JSONRenderer as DRFJSONRenderer
|
||||
|
||||
from .serializers import b64encode
|
||||
|
||||
|
||||
class JSONEncoder(DRFJSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, bytes) or isinstance(obj, memoryview):
|
||||
return b64encode(obj)
|
||||
return super().default(obj)
|
||||
|
||||
|
||||
class JSONRenderer(DRFJSONRenderer):
|
||||
"""
|
||||
Renderer which serializes to JSON with support for our base64
|
||||
"""
|
||||
encoder_class = JSONEncoder
|
Loading…
Reference in New Issue