Opened 11 years ago

Closed 8 years ago

#5590 closed (duplicate)

JSON encoding fails on Promise (result of gettext_lazy)

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


When attempting to dump a Promise, JSON encoding fails.


>>> 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 Jeremy Dunck 11 years ago.
Altering json.DjangoJSONEncoder to handle encoding of Promise objects.
5590.diff (1.7 KB) - added by Jeremy Dunck 8 years ago.
Updated patch to apply to the 1.2 alpha.

Download all attachments as: .zip

Change History (8)

Changed 11 years ago by Jeremy Dunck

Attachment: django-json-lazy.diff added

Altering json.DjangoJSONEncoder to handle encoding of Promise objects.

comment:1 Changed 11 years ago by Russell Keith-Magee

Component: UncategorizedSerialization

comment:2 Changed 10 years ago by Jacob

Triage Stage: UnreviewedAccepted

comment:3 Changed 9 years ago by Greg Wogan-Browne

Duplicate of #5868?

comment:4 Changed 9 years ago by Jeremy Dunck

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 8 years ago by Jeremy Dunck

Attachment: 5590.diff added

Updated patch to apply to the 1.2 alpha.

comment:5 Changed 8 years ago by anonymous

Cc: Brandon Konkle added
Triage Stage: AcceptedReady for checkin

comment:6 Changed 8 years ago by Luke Plant

Resolution: duplicate
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top