#5868: Newforms ErrorDict serialization problem
-------------------------------------------+--------------------------------
   Reporter:  tangerine                    |                Owner:  nobody      
          
     Status:  new                          |            Component:  
django.newforms       
    Version:  SVN                          |           Resolution:              
          
   Keywords:  serialization newforms JSON  |                Stage:  Design 
decision needed
  Has_patch:  0                            |           Needs_docs:  0           
          
Needs_tests:  0                            |   Needs_better_patch:  0           
          
-------------------------------------------+--------------------------------
Changes (by gwilson):

  * needs_better_patch:  => 0
  * stage:  Unreviewed => Design decision needed
  * needs_tests:  => 0
  * needs_docs:  => 0

Comment:

 Yes, r6625 turned all newforms error messages into lazy strings, which
 aren't supported by the default `JSONEncoder`:

 {{{
 #!python
 >>> from django.utils import simplejson
 >>> simplejson.dumps(ugettext_lazy('hello world'))
 Traceback (most recent call last):
   File "<console>", line 1, in ?
   File
 "/home/gdub/bzr/django/upstream/django/utils/simplejson/__init__.py", line
 181, in dumps
     separators=separators,
   File
 "/home/gdub/bzr/django/upstream/django/utils/simplejson/encoder.py", line
 312, in encode
     chunks = list(self.iterencode(o))
   File
 "/home/gdub/bzr/django/upstream/django/utils/simplejson/encoder.py", line
 273, in _iterencode
     for chunk in self._iterencode_default(o, markers):
   File
 "/home/gdub/bzr/django/upstream/django/utils/simplejson/encoder.py", line
 279, in _iterencode_default
     newobj = self.default(o)
   File
 "/home/gdub/bzr/django/upstream/django/utils/simplejson/encoder.py", line
 300, in default
     raise TypeError("%r is not JSON serializable" % (o,))
 TypeError: <django.utils.functional.__proxy__ object at 0x892378c> is not
 JSON serializable
 }}}

 The `JSONEncoder` docstring states:

 {{{
     To extend this to recognize other objects, subclass and implement a
     ``.default()`` method with another method that returns a serializable
     object for ``o`` if possible, otherwise it should call the superclass
     implementation (to raise ``TypeError``).
 }}}

 Looks like we might want to do this since lazy strings are used in several
 places throughout Django.  Here's a quick and dirty one that works:

 {{{
 #!python
 from django.utils.functional import Promise
 from django.utils.translation import force_unicode
 from django.utils.simplejson import JSONEncoder

 class LazyEncoder(JSONEncoder):
     def default(self, o):
         if isinstance(o, Promise):
             return force_unicode(o)
         else:
             return super(LazyEncoder, self).default(o)
 }}}

 {{{
 #!python
 >>> simplejson.dumps(ugettext_lazy('hello world'), cls=LazyEncoder)
 '"hello world"'
 >>> simplejson.dumps([1, 2, ugettext_lazy('hi')], cls=LazyEncoder)
 '[1, 2, "hi"]'
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/5868#comment:1>
Django Code <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to