[New-bugs-announce] [issue24313] json fails to serialise numpy.int64

Thomas Arildsen report at bugs.python.org
Thu May 28 10:32:31 CEST 2015

New submission from Thomas Arildsen:

When I run the attached example in Python 2.7.9, it succeeds. In Python 3.4, it fails as shown below. I use json 2.0.9 and numpy 1.9.2 with both versions of Python. Python and all packages provided by Anaconda 2.2.0.
The error seems to be caused by the serialised object containing a numpy.int64 type. It might fail with other 64-bit numpy types as well (untested).

TypeError                                 Traceback (most recent call last)
/home/tha/tmp/debug_json/debug_json.py in <module>()
      4 test = {'value': np.int64(1)}
----> 6 obj=json.dumps(test)

/home/tha/.conda/envs/python3/lib/python3.4/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    228         cls is None and indent is None and separators is None and
    229         default is None and not sort_keys and not kw):
--> 230         return _default_encoder.encode(obj)
    231     if cls is None:
    232         cls = JSONEncoder

/home/tha/.conda/envs/python3/lib/python3.4/json/encoder.py in encode(self, o)
    190         # exceptions aren't as detailed.  The list call should be roughly
    191         # equivalent to the PySequence_Fast that ''.join() would do.
--> 192         chunks = self.iterencode(o, _one_shot=True)
    193         if not isinstance(chunks, (list, tuple)):
    194             chunks = list(chunks)

/home/tha/.conda/envs/python3/lib/python3.4/json/encoder.py in iterencode(self, o, _one_shot)
    248                 self.key_separator, self.item_separator, self.sort_keys,
    249                 self.skipkeys, _one_shot)
--> 250         return _iterencode(o, 0)
    252 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/home/tha/.conda/envs/python3/lib/python3.4/json/encoder.py in default(self, o)
    172         """
--> 173         raise TypeError(repr(o) + " is not JSON serializable")
    175     def encode(self, o):

TypeError: 1 is not JSON serializable

files: debug_json.py
messages: 244288
nosy: thomas-arildsen
priority: normal
severity: normal
status: open
title: json fails to serialise numpy.int64
type: crash
versions: Python 3.4
Added file: http://bugs.python.org/file39530/debug_json.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list