|
22 | 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23 | 23 | SOFTWARE. |
24 | 24 | """ |
25 | | -import json |
26 | 25 | import pickle |
27 | | -from msgspec import json, msgpack |
| 26 | +from msgspec import json as msgspecjson, msgpack |
28 | 27 | import json as pythonjson |
29 | 28 | import inspect |
30 | 29 | import array |
|
36 | 35 | from enum import Enum |
37 | 36 | from collections import deque |
38 | 37 |
|
| 38 | +try: |
| 39 | + import numpy |
| 40 | +except ImportError: |
| 41 | + pass |
| 42 | + |
39 | 43 | from ..param.parameters import TypeConstrainedList, TypeConstrainedDict, TypedKeyMappingsConstrainedDict |
40 | 44 | from .constants import JSONSerializable, Serializers |
41 | 45 | from .utils import format_exception_as_json |
@@ -82,15 +86,15 @@ class JSONSerializer(BaseSerializer): |
82 | 86 |
|
83 | 87 | def __init__(self) -> None: |
84 | 88 | super().__init__() |
85 | | - self.type = json |
| 89 | + self.type = msgspecjson |
86 | 90 |
|
87 | 91 | def loads(self, data : typing.Union[bytearray, memoryview, bytes]) -> JSONSerializable: |
88 | 92 | "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)) |
90 | 94 |
|
91 | 95 | def dumps(self, data) -> bytes: |
92 | 96 | "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) |
94 | 98 |
|
95 | 99 | @classmethod |
96 | 100 | def default(cls, obj) -> JSONSerializable: |
@@ -119,6 +123,8 @@ def default(cls, obj) -> JSONSerializable: |
119 | 123 | if obj.typecode == 'u': |
120 | 124 | return obj.tounicode() |
121 | 125 | return obj.tolist() |
| 126 | + if 'numpy' in globals() and isinstance(obj, numpy.ndarray): |
| 127 | + return obj.tolist() |
122 | 128 | replacer = cls._type_replacements.get(type(obj), None) |
123 | 129 | if replacer: |
124 | 130 | return replacer(obj) |
|
0 commit comments