Fwd: [pydal] datetime not json serializable (#112)

58 views
Skip to first unread message

Massimo DiPierro

unread,
Mar 27, 2015, 12:54:46 PM3/27/15
to web2py-d...@googlegroups.com

This is a problem

---------- Forwarded message ----------
From: "salomonderossi" <notifi...@github.com>
Date: Mar 27, 2015 5:56 AM
Subject: [pydal] datetime not json serializable (#112)
To: "web2py/pydal" <py...@noreply.github.com>
Cc:

Datetime objects in the pyDLA are not json serializable. In a pevious version of web2py's DAL json serializion did work fine.

I use Version 15.3 of pyDAL with python2.7 on Ubuntu 14.04.2 LTS.

Here ist a small program which demonstrates the issue:

from pydal import DAL, Field
from datetime import datetime

db = DAL('sqlite://storage.db', folder="test_databases")
db.define_table('test_table', Field('date_field', 'datetime'))

db.test_table.insert(date_field=datetime.now())

rows = db().select(db.test_table.ALL)
print(rows.as_json())

Which gives this output on my machine:

Traceback (most recent call last):
  File "db_text.py", line 13, in <module>
    print(rows.as_json())
  File "/usr/local/lib/python2.7/dist-packages/pydal/objects.py", line 2741, in as_json
    return json.dumps(items)
  File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2015, 3, 27, 13, 37, 7) is not JSON serializable


Reply to this email directly or view it on GitHub.

Paolo Valleri

unread,
Mar 27, 2015, 1:05:30 PM3/27/15
to web2py-d...@googlegroups.com
a solution can be to set datetime_to_str=True when calling record.as_dict (https://github.com/web2py/pydal/blob/master/pydal/objects.py#L2730)
what do you think?

 Paolo

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Massimo DiPierro

unread,
Mar 27, 2015, 1:08:45 PM3/27/15
to web2py-d...@googlegroups.com

Yes.

Paolo Valleri

unread,
Mar 27, 2015, 1:13:06 PM3/27/15
to web2py-d...@googlegroups.com
ok, I'll post a PR with a test case 

 Paolo

Giovanni Barillari

unread,
Mar 30, 2015, 6:55:05 AM3/30/15
to web2py-d...@googlegroups.com
Hi all,
I would like to implement json and xml serializers inside pyDAL, porting a bit of the code of web2py and refactoring code to allow external serializers injection (like we do right now but with a cleaner and simpler way).

I will post a PR in the next days to allow you to share your considerations.


/Giovanni
Reply all
Reply to author
Forward
0 new messages