I need to serialize a numpy array to some JSON-compatible form. Since the framework I'm using doesn't give me access to the JSON encoder/decoder object, I'm stuck serializing a numpy array to something that can then be marshalled into JSON. I've opted for either array.tobytes
or array.tostring
(both seem to be essentially the same thing).
Below is an example which illustrates my problem:
import numpy as np
a = np.random.rand(1024, 1024) # create array of random values
b = array.tobytes() # serialize array
a2 = np.fromstring(b)
When I inspect the value of a2
, I find that it only contains the first line of the original a
. In other words, a2 == a[0, :]
.
How can I decode the full array?