为什么一些numpy数据类型JSON可序列化而其他不是?

时间:2022-09-25 14:08:06

Numpy has a lot of different basic types, all of which are listed here.

Numpy有很多不同的基本类型,所有这些都列在这里。

I've tracked down an issue in my program to float32s not being JSON-serializable, so I've started testing all datatypes from the list above:

我已经在我的程序中找到了一个问题,因为float32s不是JSON可序列化的,所以我开始测试上面列表中的所有数据类型:

>>> import numpy as np
>>> from json import dumps
>>> dumps(np.bool(True))
'true'
>>> dumps(np.bool_(True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: True is not JSON serializable
>>> dumps(np.int(0))
'0'
>>> dumps(np.int_(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.intc(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.intp(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int8(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int16(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int32(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int64(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint8(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint16(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint32(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint64(0))
Traceback (most recent call last):
  [...]
TypeError: 0 is not JSON serializable
>>> dumps(np.float(0))
'0.0'
>>> dumps(np.float_(0))
'0.0'
>>> dumps(np.float16(0))
Traceback (most recent call last):
  [...]
TypeError: 0.0 is not JSON serializable
>>> dumps(np.float32(0))
Traceback (most recent call last):
  [...]
TypeError: 0.0 is not JSON serializable
>>> dumps(np.float64(0))
'0.0'
>>> dumps(np.complex(0))
Traceback (most recent call last):
  [...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex_(0))
Traceback (most recent call last):
  [...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex64(0))
Traceback (most recent call last):
  [...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex128(0))
Traceback (most recent call last):
  [...]
TypeError: 0j is not JSON serializable

So, no complex type is serializable, that makes sense.

因此,没有复杂类型是可序列化的,这是有道理的。

But bool works and bool_ doesn't. int works, but anything else with int in its name doesn't. float, float_ and float64 are all fine, but float16 and float32 are not.

但bool工作,bool_没有。 int工作,但其名称中包含int的任何其他内容都不起作用。 float,float_和float64都可以,但float16和float32都没有。

Why is this the case? Obviously, they can all easily be converted to a string, the stacktrace even shows repr() being used to display their value. Might this be unintentional? Or is there a good reason for this behaviour?

为什么会这样?显然,它们都可以很容易地转换为字符串,堆栈跟踪甚至显示用于显示其值的repr()。这可能是无心的吗?或者这种行为有充分的理由吗?

1 个解决方案

#1


5  

The data types that are JSON serializable are all Python built-ins:

JSON可序列化的数据类型都是Python内置函数:

>>> np.int is int
True
>>> np.float is float
True
>>> np.bool is bool
True

So all NumPy data types that you show are not JSON serializable. At least is consistent.

因此,您显示的所有NumPy数据类型都不是JSON可序列化的。至少是一致的。

np.float_ is the same as np.float64 (tested on MacOS):

np.float_与np.float64相同(在MacOS上测试):

>>> np.float_ is np.float64
True

The help says:

帮助说:

np.float64

np.float64

64-bit floating-point number. Character code 'd'. Python float compatible.

64位浮点数。字符代码'd'。 Python float兼容。

Whereas:

鉴于:

np.float32

np.float32

32-bit floating-point number. Character code 'f'. C float compatible.

32位浮点数。字符代码'f'。 C浮动兼容。

So, Python float compatible types work with json.dumps(), but C compatible ones don't.

因此,Python float兼容类型适用于json.dumps(),但C兼容类型不适用。

#1


5  

The data types that are JSON serializable are all Python built-ins:

JSON可序列化的数据类型都是Python内置函数:

>>> np.int is int
True
>>> np.float is float
True
>>> np.bool is bool
True

So all NumPy data types that you show are not JSON serializable. At least is consistent.

因此,您显示的所有NumPy数据类型都不是JSON可序列化的。至少是一致的。

np.float_ is the same as np.float64 (tested on MacOS):

np.float_与np.float64相同(在MacOS上测试):

>>> np.float_ is np.float64
True

The help says:

帮助说:

np.float64

np.float64

64-bit floating-point number. Character code 'd'. Python float compatible.

64位浮点数。字符代码'd'。 Python float兼容。

Whereas:

鉴于:

np.float32

np.float32

32-bit floating-point number. Character code 'f'. C float compatible.

32位浮点数。字符代码'f'。 C浮动兼容。

So, Python float compatible types work with json.dumps(), but C compatible ones don't.

因此,Python float兼容类型适用于json.dumps(),但C兼容类型不适用。