From d489c5c88144a25ef0d61fb8deb0b77f3a061480 Mon Sep 17 00:00:00 2001 From: David Pretty Date: Fri, 13 Sep 2013 13:36:18 +1000 Subject: [PATCH] Let JSONEncoder handle Numpy data types. json.JSONEncoder cannot serialize Numpy data types. Numpy arrays and array scalars have a tolist() method which casts the object to a standard python data type. --- rest_framework/utils/encoders.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index b26a2085a..7efd5417b 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -42,6 +42,8 @@ class JSONEncoder(json.JSONEncoder): return str(o.total_seconds()) elif isinstance(o, decimal.Decimal): return str(o) + elif hasattr(o, 'tolist'): + return o.tolist() elif hasattr(o, '__iter__'): return [i for i in o] return super(JSONEncoder, self).default(o)