Skip to content

Commit

Permalink
fix potential clash in namepsace of msgspec json and python's own json
Browse files Browse the repository at this point in the history
  • Loading branch information
VigneshVSV committed Oct 23, 2024
1 parent adffbfd commit 4c64ef3
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions hololinked/server/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import json
import pickle
from msgspec import json, msgpack
from msgspec import json as msgspecjson, msgpack
import json as pythonjson
import inspect
import array
Expand All @@ -36,6 +35,11 @@
from enum import Enum
from collections import deque

try:
import numpy
except ImportError:
pass

from ..param.parameters import TypeConstrainedList, TypeConstrainedDict, TypedKeyMappingsConstrainedDict
from .constants import JSONSerializable, Serializers
from .utils import format_exception_as_json
Expand Down Expand Up @@ -82,15 +86,15 @@ class JSONSerializer(BaseSerializer):

def __init__(self) -> None:
super().__init__()
self.type = json
self.type = msgspecjson

def loads(self, data : typing.Union[bytearray, memoryview, bytes]) -> JSONSerializable:
"method called by ZMQ message brokers to deserialize data"
return json.decode(self.convert_to_bytes(data))
return msgspecjson.decode(self.convert_to_bytes(data))

def dumps(self, data) -> bytes:
"method called by ZMQ message brokers to serialize data"
return json.encode(data, enc_hook=self.default)
return msgspecjson.encode(data, enc_hook=self.default)

@classmethod
def default(cls, obj) -> JSONSerializable:
Expand Down Expand Up @@ -119,6 +123,8 @@ def default(cls, obj) -> JSONSerializable:
if obj.typecode == 'u':
return obj.tounicode()
return obj.tolist()
if 'numpy' in globals() and isinstance(obj, numpy.ndarray):
return obj.tolist()
replacer = cls._type_replacements.get(type(obj), None)
if replacer:
return replacer(obj)
Expand Down

0 comments on commit 4c64ef3

Please sign in to comment.