move bytes decoding to serializer

This commit is contained in:
Timothy Pogue 2022-09-05 23:25:25 -06:00
parent d526dfb171
commit 38f14349e9
2 changed files with 3 additions and 10 deletions

View File

@ -27,12 +27,6 @@ class WebSocketProxy:
"""
Send data on the wrapped websocket
"""
if not isinstance(data, str):
# We use HybridJSONWebSocketSerializer, which when serialized returns
# bytes because of ORJSON, so we explicitly decode into a string
data = str(data, "utf-8")
if hasattr(self._websocket, "send_text"):
await self._websocket.send_text(data)
else:

View File

@ -57,11 +57,10 @@ class RapidJSONWebSocketSerializer(WebSocketSerializer):
class HybridJSONWebSocketSerializer(WebSocketSerializer):
def _serialize(self, data):
# ORJSON returns bytes
return orjson.dumps(data, default=_json_default)
def _serialize(self, data) -> str:
return str(orjson.dumps(data, default=_json_default), "utf-8")
def _deserialize(self, data):
def _deserialize(self, data: str):
# RapidJSON expects strings
return rapidjson.loads(data, object_hook=_json_object_hook)