I think we should use gluon.serializers.json which handles this case.

On Monday, 10 December 2012 09:12:04 UTC-6, Niphlod wrote:
>
> this is "normal": the json serializer by default doesn't handle datetime 
> objects serialization.
>
> Python 2.7.3 (default, Apr 10 2012, 23:31:26) 
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import json
> >>> import datetime
> >>> json.dumps(datetime.datetime.now())
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "F:\Programmi\Portable_Python_2.7.3.1\App\lib\json\__init__.py", 
> line 231, in dumps
>     return _default_encoder.encode(obj)
>   File "F:\Programmi\Portable_Python_2.7.3.1\App\lib\json\encoder.py", 
> line 201, in encode
>     chunks = self.iterencode(o, _one_shot=True)
>   File "F:\Programmi\Portable_Python_2.7.3.1\App\lib\json\encoder.py", 
> line 264, in iterencode
>     return _iterencode(o, 0)
>   File "F:\Programmi\Portable_Python_2.7.3.1\App\lib\json\encoder.py", 
> line 178, in default
>     raise TypeError(repr(o) + " is not JSON serializable")
> TypeError: datetime.datetime(2012, 12, 10, 16, 4, 46, 573000) is not JSON 
> serializable
> >>>
>
> We could use the web2py "standard" (standard as in web2py and a few other 
> frameworks "world") object serialization, that takes care of it using 
> mydate.isoformat()[:19].replace('T', ' ')
> but I'm more lenient to have the results strictly as json "standards" (as 
> in worldwide spec) allowed types.
>
> Suggestions/votes welcome.
>
> On Monday, December 10, 2012 4:01:42 PM UTC+1, Rene Dohmen wrote:
>>
>> Hi,
>>
>> I like the new scheduler concept a lot. It looks promising.
>>
>> Something goes wrong when I try to return a datetime in the method I 
>> wanted to schedule...
>> A Simple JSON parsing error
>> --
>> raise TypeError(repr(o) + " is not JSON serializable") TypeError: 
>> datetime.datetime(2012, 12, 10, 15, 57, 59, 300169) is not JSON serializable
>> --
>>
>> Example test code:
>> --
>> def dateTask():
>>     return dict(taskDate=datetime.now())
>>
>> from gluon.scheduler import Scheduler
>> Scheduler(db,dict(dateTask=dateTask)) 
>> --
>> Scheduler started from web2py trunk with:
>> python web2py.py --nogui -K APPNAME -D0
>>
>> Rene
>>
>

-- 



Reply via email to