[web2py] Re: datetime, DAL and JSON serialization
Sorry for the long reply time (I am working at two jobs and didn't have time to get to it yet). I can supply the following simple example: the data model is: db.define_table("timestamps", Field("by", db.auth_user), Field("at", "datetime", default=request.now)) and the controller function is: @service.jsonrpc def test_json(): data = [] timestamps = db(db.timestamps.id > 0).select() for ts in timestamps: data.append(dict(by = ts.by.first_name, at = ts.at)) return data When I try to call this from my application (qooxdoo app) I receive the following JSONRPCError: "TypeError: datetime.datetime(2010, 11, 10, 10, 33, 22) is not JSON serializable". I think this is different from the behavior in previous versions where I simply got a string back in return. If I go to the test_json view directly through the browser I get the following response: {'2 {'at': 2 {'at': datetime.datetime(2010, 11, 10, 10, 56, 58), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 10, 58, 40), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 11, 44, 11), 'by': 'Andreas'} {'2 {'at': 2 {'at': datetime.datetime(2010, 11, 10, 11, 48, 17), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 11, 55, 12), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 11, 58, 7), 'by': 'Andreas'} {'2 {'at': 2 {'at': datetime.datetime(2010, 11, 10, 12, 7, 50), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 12, 17, 9), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 12, 20, 20), 'by': 'Andreas'} I think it might be related to the detect_types change nick name has hinted at. Am i right? I have tried to look through the Service class source to understand if it uses as_list or not, but I haven't been able to ascertain when a function receives the as_list attribute. Any help would be greatly appreciated, and thanks for the extremely good work on web2py (it seems I keep saying it, but it is really and truly appreciated!) Omri
[web2py] Re: datetime, DAL and JSON serialization
Sorry for the long reply time (I am working at two jobs and didn't have time to get to it yet). I can supply the following simple example: the data model is: db.define_table("timestamps", Field("by", db.auth_user), Field("at", "datetime", default=request.now)) and the controller function is: @service.jsonrpc def test_json(): data = [] timestamps = db(db.timestamps.id > 0).select() for ts in timestamps: data.append(dict(by = ts.by.first_name, at = ts.at)) return data When I try to call this from my application (qooxdoo app) I receive the following JSONRPCError: "TypeError: datetime.datetime(2010, 11, 10, 10, 33, 22) is not JSON serializable". I think this is different from the behavior in previous versions where I simply got a string back in return. If I go to the test_json view directly through the browser I get the following response: {'2 {'at': 2 {'at': datetime.datetime(2010, 11, 10, 10, 56, 58), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 10, 58, 40), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 11, 44, 11), 'by': 'Andreas'} {'2 {'at': 2 {'at': datetime.datetime(2010, 11, 10, 11, 48, 17), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 11, 55, 12), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 11, 58, 7), 'by': 'Andreas'} {'2 {'at': 2 {'at': datetime.datetime(2010, 11, 10, 12, 7, 50), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 12, 17, 9), 'by': 'Andreas'} 2 {'at': datetime.datetime(2010, 11, 10, 12, 20, 20), 'by': 'Andreas'} I think it might be related to the detect_types change nick name has hinted at. Am i right? I have tried to look through the Service class source to understand if it uses as_list or not, but I haven't been able to ascertain when a function receives the as_list attribute. Any help would be greatly appreciated, and thanks for the extremely good work on web2py (it seems I keep saying it, but it is really and truly appreciated!) Omri
[web2py] Re: datetime, DAL and JSON serialization
This thread might be relevant: https://groups.google.com/d/topic/web2py/1N0TwMOgp3o/discussion - have you started using detect_types? (if not, you should!) This thread might also be relevant: https://groups.google.com/d/topic/web2py/RuBOLSyDc40/discussion - as_list and as_dict (if you use them) convert datetime to string unless you tell them not to. Perhaps you've removed an as_list() or as_dict() call from your flow? Finally, a recent web2py update switched from a simplejson that has been changed to internally support datetime(), into a pristine simplejson, that serializes datetimes using a default() method; If you are calling simplejson.dumps() yourslef, you need to pass a "default=serializers.custom_json" which you didn't need before (Same might apply if you have your own specific .json view or generic.json view)
[web2py] Re: datetime, DAL and JSON serialization
Can you provide a simple example to reproduce the problem? Massimo On Mar 29, 8:48 am, Omri Har-Shemesh wrote: > Hey, > > I have a problem with datetime json serialization. I am using the DAL with > sqlite, and accessing web2py using JSONRPC. > Today I started getting an error when trying to pass datetime fields back to > the client. I haven't changed anything in the code for the routines, but it > seems that the DAL returns a datetime object which is not JSON-serializable. > > Has anything changed recently with the DAL? > I don't think I have changed any configuration properties on my database - > but could this be a reason for it suddenly stop working? > > Thanks for the time and great work on web2py! > > Omri