Opened 17 years ago

Closed 14 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: dev
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: no UI/UX: no

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

Download all attachments as: .zip

Change History (8)

by Jeremy Dunck, 17 years ago

Attachment: django-json-lazy.diff added

Altering json.DjangoJSONEncoder to handle encoding of Promise objects.

comment:1 by Russell Keith-Magee, 17 years ago

Component: UncategorizedSerialization

comment:2 by Jacob, 17 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Greg Wogan-Browne, 16 years ago

Duplicate of #5868?

comment:4 by Jeremy Dunck, 16 years ago

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.

by Jeremy Dunck, 15 years ago

Attachment: 5590.diff added

Updated patch to apply to the 1.2 alpha.

comment:5 by anonymous, 14 years ago

Cc: Brandon Konkle added
Triage Stage: AcceptedReady for checkin

comment:6 by Luke Plant, 14 years ago

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