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

make json dump understand numpy types #3

Open
ffont opened this issue May 31, 2018 · 0 comments
Open

make json dump understand numpy types #3

ffont opened this issue May 31, 2018 · 0 comments

Comments

@ffont
Copy link
Member

ffont commented May 31, 2018

We can make the pymtg.io.json_dump function compatible with numpy types by defining our own JSONEncoder. See example to support numpy arrays, ints and floats:

class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.integer):
            return int(obj)
        elif isinstance(obj, numpy.floating):
            return float(obj)
        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)

json.dump({}, open('filename.json', 'w'), cls=NumpyEncoder)
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

No branches or pull requests

1 participant