From 263281d71d0425d7bb9b4ebbdf1811ef637ee60a Mon Sep 17 00:00:00 2001 From: Malcolm Box Date: Thu, 21 Nov 2013 20:09:48 +0000 Subject: Fix issue #1231: JSONEncoder doesn't handle dict-like objects Check for __getitem__ and then attempt to convert to a dict. The check for __getitem__ is there as there's no universal way to check if an object is a mapping type, but this is a likely proxy --- rest_framework/utils/encoders.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'rest_framework/utils') diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index 35ad206b..22b1ab3d 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -44,6 +44,12 @@ class JSONEncoder(json.JSONEncoder): return str(o) elif hasattr(o, 'tolist'): return o.tolist() + elif hasattr(o, '__getitem__'): + try: + return dict(o) + except KeyError: + # Couldn't convert to a dict, fall through + pass elif hasattr(o, '__iter__'): return [i for i in o] return super(JSONEncoder, self).default(o) -- cgit v1.2.3