#22821: DjangoJSONEncoder no longer supports simplejson
-----------------------------------------+--------------------
     Reporter:  Keryn Knight <django@…>  |      Owner:  nobody
         Type:  New feature              |     Status:  new
    Component:  Core (Serialization)     |    Version:  master
     Severity:  Normal                   |   Keywords:
 Triage Stage:  Unreviewed               |  Has patch:  0
Easy pickings:  0                        |      UI/UX:  0
-----------------------------------------+--------------------
 For better or worse, `DjangoJSONEncoder`, despite living in the
 `core.serializers` package (and to my knowledge not being public API), is
 useful to support the few extra datatypes in standard json usage, and
 doesn't have any negative side affects. So it gets used.

 Previously, in Django 1.4, `core.serializers.json` relied upon
 `simplejson`, but starting with 1.5 only used `json`.

 Since then, `simplejson` has moved forward and has a slightly different
 API that prevents using `DjangoJSONEncoder`, because it subclasses
 `json.JSONEncoder` rather than `simplejson.JSONEncoder`, thus this worked
 in 1.4:
 {{{
 import simplejson as json
 from django.core.serializers.json import DjangoJSONEncoder
 json.dumps("{}", cls=DjangoJSONEncoder)
 }}}
 but in 1.5+, it yields a TypeError, with `simplejson==3.5.2`:
 {{{
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/path/to/python2.7/site-packages/simplejson/__init__.py", line
 382, in dumps
     **kw).encode(obj)
 TypeError: __init__() got an unexpected keyword argument
 'namedtuple_as_object'
 }}}
 I think it ought to be possible to decouple the implementation such that
 people wanting to continue using simplejson could still benefit from the
 implementation of default, by doing something like (untested, pseudo-
 code):
 {{{
 class RichEncoder(object):
     def default(self, o):
         ...
         return (RichEncoder, self).default(o)

 class DjangoJSONEncoder(RichEncoder, json.JSONEncoder): pass
 DateTimeAwareJSONEncoder = DjangoJSONEncoder
 }}}
 meanwhile, userland implementations would replace the `json.JSONEncoder`
 part with `simplejson.JSONEncoder`, I guess.

 This came up and bit someone in the IRC channel, and I'm reporting it
 because I ''think'' it's supportable, though it'd benefit me nought.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22821>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/080.08d8333d5c9754d4bcf7057398ce77d9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to