def default(self, obj):
"""Converts an ndarray into a dictionary for efficient serialization.
The dict has three keys:
- dtype : The datatype of the array as a string.
- shape : The shape of the array as a tuple.
- __ndarray__ : The data of the array as a list.
Parameters
----------
obj : :obj:`numpy.ndarray`
The ndarray to encode.
Returns
-------
:obj:`dict`
The dictionary serialization of obj.
Raises
------
TypeError
If obj isn't an ndarray.
"""
if isinstance(obj, np.ndarray):
return dict(__ndarray__=obj.tolist(),
dtype=str(obj.dtype),
shape=obj.shape)
# Let the base class default method raise the TypeError
return _json.JSONEncoder(self, obj)
json_serialization.py 文件源码
python
阅读 24
收藏 0
点赞 0
评论 0
评论列表
文章目录