dataclasses-json
dataclasses-json copied to clipboard
Add support for encoding numpy integer and floating point types
Feel free to ignore if this is too much of an edge case for your module, but I found a need to serialise dataclasses to JSON that had NumPy types (int64 etc) in them. This PR adds support for encoding NumPy types, but only if the numpy module is available.
It would be nice to handle numpy arrays as well. Something like:
elif np_available and _isinstance_safe(o, np.ndarray):
result = o.tolist()
It would be nice to handle numpy arrays as well. Something like:
elif np_available and _isinstance_safe(o, np.ndarray): result = o.tolist()
Thanks, I have belatedly incorporated your suggestion, simplified the code and added a test for encoding to JSON,
I wonder why not use encoder/decoder instead? I've seen people do this with fields being pandas Dataframes
Thanks for your PR. Imo, I believe dataclasses-json should only support native types out of the box while 3rd-party types like numpy can be de/encoded with custom encoders.
I'd say if the author added instead an example for docs with custom encoder/decoder for numpy, that would have helped many people. I'll wait a bit, if no reaction follows, I'll close this one and update docs with examples for both numpy and pandas.
Closing this in favor of #470