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)
Change History (8)
by , 17 years ago
Attachment: | django-json-lazy.diff added |
---|
comment:1 by , 17 years ago
Component: | Uncategorized → Serialization |
---|
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 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.
comment:5 by , 14 years ago
Cc: | added |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:6 by , 14 years ago
Resolution: | → duplicate |
---|---|
Status: | new → 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.
Altering json.DjangoJSONEncoder to handle encoding of Promise objects.