Qcodes icon indicating copy to clipboard operation
Qcodes copied to clipboard

NumpyJSONDecoder

Open diogovalada opened this issue 4 years ago • 4 comments

Hello,

I see you have an extended class of JSONEncoder that allows saving into JSON types not supported by this class. However, I can't seem to find the reverse class, allowing you to recover these types from json. Is it not implemented, or did I just not find it?

Thanks in advance, Diogo Valada

diogovalada avatar Apr 15 '21 17:04 diogovalada

Hi @Astlaan

Unfortunately that is generally not possible. The numpy encoder works by converting numpy arrays into python lists since json only has one list type it is not really possible to distinguish between a python list and a numpy array when you load this back into memory

jenshnielsen avatar Apr 16 '21 06:04 jenshnielsen

Couldn't this simply be done by encoding numpy/python lists in the same fashion as numbers.Complex/UFloat? I.e with a dictionary specifying the type? It could still have backwards compatibility by decoding the arrays encoded the current way as python lists by default, since they can then easily be turned into numpy arrays with np.array() (which is what a user would have to do manually anyway).

Or we can just decode it always as python lists while sticking with the current encoding scheme.

diogovalada avatar Apr 19 '21 12:04 diogovalada

@jenshnielsen thank you for the replies and support!!

Quick questions before developing any solution:

Would a PR for a two-way conversion that would preserve python types be accepted?

Possibly with a configurable behavior for numpy to preserve backwards compatibility?

caenrigen avatar Apr 20 '21 17:04 caenrigen

Would a PR for a two-way conversion that would preserve python types be accepted?

of course, we can have a look! but a thorough testing would be warranted :)

Possibly with a configurable behavior for numpy to preserve backwards compatibility?

if that's feasible, yes! :)

astafan8 avatar Mar 14 '22 13:03 astafan8