#3324: FloatFields are converted to decimal and simplejson cannot serialize
------------------------------+---------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: jacob
Status: reopened | Component: Serialization
Version: SVN | Resolution:
Keywords: | Stage: Accepted
Has_patch: 1 | Needs_docs: 0
Needs_tests: 0 | Needs_better_patch: 0
------------------------------+---------------------------------------------
Changes (by anonymous):
* status: closed => reopened
* has_patch: 0 => 1
* resolution: worksforme =>
Comment:
Hey Jacob,
I've been able to reproduce this in django trunk (currently r4463). Below
is a simple test case to demonstrate the !TypeError.
I think this arises when the database connector (postgresql_psycopg2 in my
case) converts '''''models.!FloatField''''' data to Python's
'''''decimal.Decimal''''' type.
== Demonstrate the error ==
{{{
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.utils.simplejson import JSONEncoder
>>> from decimal import Decimal
>>> decimal_num = Decimal("1.23")
>>> s = JSONEncoder().encode({"foo": [decimal_num,]})
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
312, in encode
chunks = list(self.iterencode(o))
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
265, in _iterencode
for chunk in self._iterencode_dict(o, markers):
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
235, in _iterencode_dict
for chunk in self._iterencode(value, markers):
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
262, in _iterencode
for chunk in self._iterencode_list(o, markers):
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
170, in _iterencode_list
for chunk in self._iterencode(value, markers):
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
273, in _iterencode
for chunk in self._iterencode_default(o, markers):
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
279, in _iterencode_default
newobj = self.default(o)
File
"/home/ewalstad/var/django_trunk/django/utils/simplejson/encoder.py", line
300, in default
raise TypeError("%r is not JSON serializable" % (o,))
TypeError: Decimal("1.23") is not JSON serializable
}}}
== Apply the patch ==
{{{
$ pwd
/home/ewalstad/var/django_trunk
$ patch django/utils/simplejson/encoder.py
./3324_Decimal_is_not_JSON_serializable.diff
patching file django/utils/simplejson/encoder.py
}}}
== Rerun the test ==
{{{
>>> from django.utils import simplejson
>>> decimal_num = Decimal("1.23")
>>> s = JSONEncoder().encode({"foo": [decimal_num,]})
>>> print s
{"foo": [1.23]}
>>> d = simplejson.loads(s)
>>> print d
{u'foo': [1.23]}
>>> print type(d['foo'][0])
<type 'float'>
}}}
Note that upon deserialization the number that was a Decimal type becomes
a float type. I found [http://lists.initd.org/pipermail/psycopg/2005-
September/003897.html this] comment by Federico Di Gregorio (from the
psycopg team) saying that sending a float back into the db will work but
there will be the expected/usual rounding errors.
Still, my vote is to commit this patch as it makes JSON work with
PostgreSQL Numeric / Python Decimal data types where it doesn't work now.
I looked for where to add/change tests for this in Django source, but
didn't find someplace that seemed simple and effective. If you need tests
for this I'll write them but please give me a hint as to where the test
should go (a subdir and/or file name would be great).
Thanks,
Eric.
--
Ticket URL: <http://code.djangoproject.com/ticket/3324#comment:5>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---