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

numpy booleans are not JSON serializable #51

Closed
DavidAntliff opened this issue Apr 12, 2017 · 3 comments
Closed

numpy booleans are not JSON serializable #51

DavidAntliff opened this issue Apr 12, 2017 · 3 comments

Comments

@DavidAntliff
Copy link

If a numpy function that returns a numpy boolean such as np.all() is used, such as in A3Part3 where isRealEven is calculated and returned, the JSON serialization in submit.py will fail with the error:

Traceback (most recent call last):
  File "submitA3.py", line 309, in <module>
    submit()
  File "submitA3.py", line 56, in submit
    rep = submitSolution(email, password, output(partIdx), partIdx)
  File "submitA3.py", line 274, in output
    outputString += convertNpObjToStr(ans) + '\n'
  File "submitA3.py", line 209, in convertNpObjToStr
    return json.dumps(obj)
  File "/home/.../.pyenv/versions/2.7.13/lib/python2.7/json/__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "/home/.../.pyenv/versions/2.7.13/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/home/.../.pyenv/versions/2.7.13/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/home/.../.pyenv/versions/2.7.13/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: True is not JSON serializable

One workaround seems to be to convert isRealEven to a Python bool with isRealEven = bool(np.all(...)).

@xserra xserra closed this as completed Feb 14, 2018
@csaftoiu
Copy link

this will fix it as well:

class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.bool_):
            return super(DateTimeJSONEncoder, self).encode(bool(obj))

        return super(DateTimeJSONEncoder, self).default(obj)

And then:

json.dump(obj, f, cls=CustomJSONEncoder)

@tszwangkwong
Copy link

Where is DateTimeJSONEncoder?

@HarmC
Copy link

HarmC commented Feb 11, 2021

He renamed his class later. Everywhere where it says DateTimeJSONEncoder, it should say CustomJSONEncoder

So then it is this

class CustomJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.bool_):
            return super(CustomJSONEncoder, self).encode(bool(obj))
        return super(CustomJSONEncoder, self).default(obj)

And then:

json.dump(obj, f, cls=CustomJSONEncoder)

works for me

full credits to csaftoiu

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

5 participants