diff options
| author | Malcolm Box | 2013-11-21 20:09:48 +0000 |
|---|---|---|
| committer | Malcolm Box | 2013-11-21 20:09:48 +0000 |
| commit | 263281d71d0425d7bb9b4ebbdf1811ef637ee60a (patch) | |
| tree | 9c14f0bc14a2229b9aadd3c20a9131a476d07cfd /rest_framework/utils/encoders.py | |
| parent | 134ffd96a14559f3a1ff4e5210c8718fb4bff9b1 (diff) | |
| download | django-rest-framework-263281d71d0425d7bb9b4ebbdf1811ef637ee60a.tar.bz2 | |
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
Diffstat (limited to 'rest_framework/utils/encoders.py')
| -rw-r--r-- | rest_framework/utils/encoders.py | 6 |
1 files changed, 6 insertions, 0 deletions
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) |
