utils: add special serializer for numpy.int64 (#492)

It looks like COUNT(*) returns a numpy.int64 value that the
default JSONEncoder does not handle.

While at if we get a type we are not handling make it easier to
debug the issue by throwing a TypeError exception with useful
data.

Fix #486
This commit is contained in:
Riccardo Magliocchetti 2016-05-20 20:10:29 +02:00 committed by Maxime Beauchemin
parent 7630d73002
commit e1a3854f2a

View File

@ -7,6 +7,7 @@ from __future__ import unicode_literals
import functools
import json
import logging
import numpy
from datetime import datetime
import parsedatetime
@ -221,6 +222,12 @@ def json_iso_dttm_ser(obj):
"""
if isinstance(obj, datetime):
obj = obj.isoformat()
elif isinstance(obj, numpy.int64):
obj = int(obj)
else:
raise TypeError(
"Unserializable object {} of type {}".format(obj, type(obj))
)
return obj