tomchristie/django-rest-framework — Issue #87: JSON serializer should handle ugettext translations
Author: xeor
Created: 2011-11-26 12:52:00+00:00
State: open

If you are using example ugettext_lazy from django.utils.translation in some output via the djangorestframework.views.View handler, you will get the "TypeError: is not JSON serializable" error message.

A solution which I have used on another project is something like this:

```python
from django.utils.translation import ugettext_lazy as _

class LazyEncoder(simplejson.JSONEncoder):
def default(self, obj):
if isinstance(obj, Promise):
return force_unicode(obj)
return super(LazyEncoder, self).default(obj)

simplejson.dumps({'data': 'normal text', 'translated': _('translated text')}, cls=LazyEncoder)
```