classification
Title: json.dumps() claims numpy.ndarray and numpy.bool_ are not serializable
Type: behavior Stage:
Components: Extension Modules, Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ethan.furman, miscjunk, r.david.murray
Priority: normal Keywords:

Created on 2013-06-25 21:43 by miscjunk, last changed 2013-06-26 06:09 by ethan.furman. This issue is now closed.

Messages (5)
msg191887 - (view) Author: miscjunk (miscjunk) Date: 2013-06-25 21:43
When json.dumps() is called on a numpy.bool_ object, it crashes.

To recreate:
import numpy as np
import json
a = np.array([1,2,3,4,5,6])
a = a < 5
json.dumps(a) #crash
json.dumps(a[0]) #crash
json.dumps(a.tolist()) #this works!

Example of error output
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "..\python-3.3.2.amd64\lib\json\__init__.py", line 236, in dumps
    return _default_encoder.encode(obj)
  File "..\python-3.3.2.amd64\lib\json\encoder.py", line 191, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "..\python-3.3.2.amd64\lib\json\encoder.py", line 249, in iterencode
    return _iterencode(o, 0)
  File "..\python-3.3.2.amd64\lib\json\encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: False is not JSON serializable
msg191889 - (view) Author: miscjunk (miscjunk) Date: 2013-06-25 22:11
numpy.ndarray is also not serializable by json.dumps()

import numpy as np
import json

a = np.array([1,2,3,4])
json.dumps(a) #crash
json.dumps(a[0]) #this works!
msg191890 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-06-25 23:02
I would (naively) not expect either of these to be serializable.  They are not base data types, which is all the json module handles unless you add extra handlers yourself.

'crash' means interpreter crash (segfault), by the way.
msg191892 - (view) Author: miscjunk (miscjunk) Date: 2013-06-25 23:30
Thanks for the explanation. I suppose this should be posted to the numpy tracker then? Would it be possible for numpy to 'just work' with the json module? Or will the final resolution be to use a handler (the default= parameter in json.loads) ?

Thanks
msg191895 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2013-06-26 06:09
For the curious, here are all the tracebacks:

--> json.dumps(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/json/__init__.py", line 236, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.3/json/encoder.py", line 191, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.3/json/encoder.py", line 249, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.3/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: array([ True,  True,  True,  True, False, False], dtype=bool) is not JSON serializable


--> json.dumps(a[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/json/__init__.py", line 236, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.3/json/encoder.py", line 191, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.3/json/encoder.py", line 249, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.3/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: True is not JSON serializable

While the repr says 'True', the type is <class 'numpy.bool_'>.


and the success:

--> json.dumps(a.tolist()) #this works!
'[true, true, true, true, false, false]'


Summary
=======

No bug here, defined behavior.  Raising the issue with NumPy won't help as this is not a bug... although perhaps they have a json handler already written somewhere?  At any rate, yes, said handler would have to be specified as the 'default' parameter.
History
Date User Action Args
2013-06-26 06:09:58ethan.furmansetstatus: open -> closed
resolution: not a bug
messages: + msg191895
2013-06-26 05:54:44ethan.furmansetnosy: + ethan.furman
2013-06-25 23:30:38miscjunksetmessages: + msg191892
2013-06-25 23:02:32r.david.murraysettype: crash -> behavior

messages: + msg191890
nosy: + r.david.murray
2013-06-25 22:11:04miscjunksetmessages: + msg191889
title: json.dumps() claims numpy.bool_ is not serializable -> json.dumps() claims numpy.ndarray and numpy.bool_ are not serializable
2013-06-25 21:43:54miscjunkcreate