Python Array Issue with jsonpickle

729 views Asked by At

I have some values here. Printing the array shows the values just fine. However conducting Jsonpickle shows something about numpy and py/tuple items. Have no idea why, and attempted to cleare cache and rebuild. That did not solve the issue. Anyone know why this could occur? All my other arrays are printing fine with jsonpickle.

It seems like I am referring some memory instead of values.

print(test)
[104.97287576678693, 99.802264802045, 115.59585412670378, 103.42889046311696, 116.20011484134734]


print(jsonpickle.encode(test))
[{"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/reduce": [{"py/type": "numpy.dtype"}, {"py/tuple": ["f8", false, true]}, {"py/tuple": [3, "<", null, null, null, -1, -1, 0]}]}, {"py/b64": "7lq4mEM+WkA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "+eB3TljzWEA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "SNhYeSLmXEA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "1Sn88HLbWUA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "/cF6rs4MXUA="}]}]}]
2

There are 2 answers

0
dimabendera On BEST ANSWER

Wrong:

jsonpickle.encode(list(np.array([1, 2, 3, 4])))

'[{"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/reduce": [{"py/type": "numpy.dtype"}, {"py/tuple": ["i8", 0, 1]}, {"py/tuple": [3, "<", null, null, null, -1, -1, 0]}]}, {"py/b64": "AQAAAAAAAAA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "AgAAAAAAAAA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "AwAAAAAAAAA="}]}]}, {"py/reduce": [{"py/function": "numpy.core.multiarray.scalar"}, {"py/tuple": [{"py/id": 2}, {"py/b64": "BAAAAAAAAAA="}]}]}]'

Right:

jsonpickle.encode(np.array([1, 2, 3, 4]).tolist())
'[1, 2, 3, 4]'
0
sj_prov On

When encoding numpy arrays, make sure you must enable the numpy extension by registering its handlers:

>>> import jsonpickle.ext.numpy as jsonpickle_numpy
>>> jsonpickle_numpy.register_handlers()

After that, encoding should work