Skip to content

Commit

Permalink
utils: add special serializer for numpy.int64
Browse files Browse the repository at this point in the history
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 apache#486
  • Loading branch information
xrmx committed May 19, 2016
1 parent 4738b01 commit c0ff4dc
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions caravel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import functools
import json
import logging
import numpy
from datetime import datetime

import parsedatetime
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit c0ff4dc

Please sign in to comment.