Skip to content

Commit 4c64ef3

Browse files
committed
fix potential clash in namepsace of msgspec json and python's own json
1 parent adffbfd commit 4c64ef3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

hololinked/server/serializers.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
SOFTWARE.
2424
"""
25-
import json
2625
import pickle
27-
from msgspec import json, msgpack
26+
from msgspec import json as msgspecjson, msgpack
2827
import json as pythonjson
2928
import inspect
3029
import array
@@ -36,6 +35,11 @@
3635
from enum import Enum
3736
from collections import deque
3837

38+
try:
39+
import numpy
40+
except ImportError:
41+
pass
42+
3943
from ..param.parameters import TypeConstrainedList, TypeConstrainedDict, TypedKeyMappingsConstrainedDict
4044
from .constants import JSONSerializable, Serializers
4145
from .utils import format_exception_as_json
@@ -82,15 +86,15 @@ class JSONSerializer(BaseSerializer):
8286

8387
def __init__(self) -> None:
8488
super().__init__()
85-
self.type = json
89+
self.type = msgspecjson
8690

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

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

9599
@classmethod
96100
def default(cls, obj) -> JSONSerializable:
@@ -119,6 +123,8 @@ def default(cls, obj) -> JSONSerializable:
119123
if obj.typecode == 'u':
120124
return obj.tounicode()
121125
return obj.tolist()
126+
if 'numpy' in globals() and isinstance(obj, numpy.ndarray):
127+
return obj.tolist()
122128
replacer = cls._type_replacements.get(type(obj), None)
123129
if replacer:
124130
return replacer(obj)

0 commit comments

Comments
 (0)