Fixed #5868 -- Provided an example of how to extend simplejson to serialize
lazy translation objects for those who want to use it directly. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6645 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
77a846bab0
commit
75efa2811d
|
@ -135,6 +135,23 @@ For example::
|
|||
json_serializer = serializers.get_serializer("json")()
|
||||
json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
|
||||
|
||||
Django ships with a copy of simplejson_ in the source. Be aware, that if
|
||||
you're using that for serializing directly that not all Django output can be
|
||||
passed unmodified to simplejson. In particular, `lazy translation objects`_
|
||||
need a `special encoder`_ written for them. Something like this will work::
|
||||
|
||||
from django.utils.functional import Promise
|
||||
from django.utils.encoding import force_unicode
|
||||
|
||||
class LazyEncoder(simplejson.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, Promise):
|
||||
return force_unicode(obj)
|
||||
return obj
|
||||
|
||||
.. _lazy translation objects: ../i18n/#lazy-translation
|
||||
.. _special encoder: http://svn.red-bean.com/bob/simplejson/tags/simplejson-1.7/docs/index.html
|
||||
|
||||
Writing custom serializers
|
||||
``````````````````````````
|
||||
|
||||
|
|
Loading…
Reference in New Issue