Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for encoding numpy integer and floating point types #352

Closed
wants to merge 6 commits into from

Conversation

pvanheus
Copy link

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.

@sebastian-ruiz
Copy link

It would be nice to handle numpy arrays as well. Something like:

elif np_available and _isinstance_safe(o, np.ndarray):
    result = o.tolist()

@pvanheus
Copy link
Author

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,

@george-zubrienko
Copy link
Collaborator

I wonder why not use encoder/decoder instead? I've seen people do this with fields being pandas Dataframes

@matt035343
Copy link
Collaborator

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.

@george-zubrienko
Copy link
Collaborator

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.

@matt035343 matt035343 linked an issue Jul 13, 2023 that may be closed by this pull request
@george-zubrienko george-zubrienko self-assigned this Jul 20, 2023
@matt035343
Copy link
Collaborator

Closing this in favor of #470

@matt035343 matt035343 closed this Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to encode numpy datatypes
4 participants