Code

Opened 7 years ago

Closed 4 years ago

#5590 closed (duplicate)

JSON encoding fails on Promise (result of gettext_lazy)

Reported by: jdunck Owned by: nobody
Component: Core (Serialization) Version: master
Severity: Keywords: unicode json serialization i18n
Cc: bkonkle Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When attempting to dump a Promise, JSON encoding fails.

Example:

>>> s5 = ugettext_lazy('Yet another string')
>>> simplejson.dumps(s5, cls=json.DjangoJSONEncoder)
...
<type 'exceptions.TypeError'>: <django.utils.functional.__proxy__ object at 0x171ca30> is not JSON serializable

I think the right solution is to have DjangoJSONEncoder handle casting to string.

Patch attached.

Attachments (2)

django-json-lazy.diff (1.4 KB) - added by jdunck 7 years ago.
Altering json.DjangoJSONEncoder to handle encoding of Promise objects.
5590.diff (1.7 KB) - added by jdunck 4 years ago.
Updated patch to apply to the 1.2 alpha.

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by jdunck

Altering json.DjangoJSONEncoder to handle encoding of Promise objects.

comment:1 Changed 7 years ago by russellm

  • Component changed from Uncategorized to Serialization

comment:2 Changed 6 years ago by jacob

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by wogan

Duplicate of #5868?

comment:4 Changed 5 years ago by jdunck

It's the same root cause, but I'm fixing the problem in a different place. #5868 showed using simplejson directly; I'm using the Django-provided JSON serializer. We did have a real-world use case where (I think) a queryset was passed to the JSON serializer and it failed with this error.

Changed 4 years ago by jdunck

Updated patch to apply to the 1.2 alpha.

comment:5 Changed 4 years ago by anonymous

  • Cc bkonkle added
  • Triage Stage changed from Accepted to Ready for checkin

comment:6 Changed 4 years ago by lukeplant

  • Resolution set to duplicate
  • Status changed from new to closed

Malcolm closed the almost identical #5868 with a documentation fix, and commented that we don't need this kind of change applied to Django's serializers on the grounds that we won't ever pass a lazy translation object to Django's serializers. I also can't see how a queryset should ever contain lazy translation objects - a queryset represents data from the database, and so shouldn't ever have that kind of object. It's not at all obvious to me that you can serialize such data, in terms of semantics - a lazy translation object is not equal to that object translated into a specific language, and you can't convert back. So raising an exception seems like good default behaviour for this situation, rather than silently losing information.

So we had Jacob who set Accepted on this ticket, but Malcolm who closed an almost identical ticket with a documentation fix. I'm going to go with the latter, because it seems like Malcolm thought about it more and I agree with his reason, and close this as a duplicate. If normal Django querysets do indeed return lazy translation objects, please re-open with details of how this happens, preferably a test case. If it is some custom unusual behaviour that has resulted in this, I think we should start a thread on django-devs so we can decide whether that is useful behaviour to support in our serializers.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.