Trying to encode in cbor some list
s and dict
s which may have deeply nested numpy float32
values.
Although those values looks similar and interchangeable:
import numpy as np
x = np.float32(1.001)
y = 2.002
print(x, y) # 1.001 2.001
print(type(x), type(y)) # <class 'numpy.float32'> <class 'float'>
cbor
would fail to encode np.float32:
import cbor
cbor.dumps([x, y])
# ValueError: cannot serialize unknown object: 1.001
I don't want or need to retain the np.float32
type information, so I would be ok if cbor
treats a np.float32
(and similar) object just like a basic float
object.
But I don't want to deeply scan and convert my object (which in this simplified case is [x, y]
, but it is more complex in reality) as that would be a performance penalty.
Is there a way to register an encoder for np.float32
so that I can convert the value to Python's float
and encode it with default cbor
ancoder?