Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#12845 closed (worksforme)

JSON serialization of empty query set raises "cannot serialize" error.

Reported by: Oroku Saki Owned by: Natalia Bidart
Component: Core (Serialization) Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I try to use

json.dumps({'items': myitemqueryset})

and the query set is empty, a serialization error comes up with the not-so-helpful message that [] cannot be serialized. In fact [] can be serialized. I take it that an empty queryset's repr returns [], but behaves differently than a normal empty list.

Change History (6)

comment:1 by Alex Gaynor, 15 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:2 by Oroku Saki, 15 years ago

Oops. My mistake: I put json.dumps but meant:

{'items': serializers.serialize('json', myitemqueryset)}

comment:3 by Natalia Bidart, 15 years ago

Owner: changed from nobody to Natalia Bidart
Status: newassigned

This is working for me using SVN rev 12554, see working example:

In [3]: Person.objects.count()
Out[3]: 0

In [4]: from django.core import serializers

In [5]: serializers.serialize('json', Person.objects.all())
Out[5]: '[]'

comment:4 by Chris Beaven, 15 years ago

Resolution: worksforme
Status: assignedclosed

I concur:

In [1]: from django.core import serializers

In [2]: from django.contrib.auth.models import *

In [3]: Group.objects.all()
Out[3]: []

In [4]: serializers.serialize('json', Group.objects.all())
Out[4]: '[]'

In [5]: serializers.serialize('json', [])
Out[5]: '[]'

If there's some other factor we're failing to consider, orokusaki, please comment and reopen (after testing against the latest trunk)

comment:5 by Oroku Saki, 15 years ago

@SmileyChris, Alex I'm sorry, I didn't include my email so I didn't get an update. I will trust that I just had a typo or something of the sort, rather than trying to reopen this. I decided after much griping that week that I didn't need to try to use serializers for an API, and instead I've switched to Django Piston. Therefore I don't have the code anymore.

comment:6 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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