[New-bugs-announce] [issue21543] json library fails to serialize objects such as datetime
Dann Ke
report at bugs.python.org
Tue May 20 18:51:57 CEST 2014
New submission from Dann Ke:
I'm using the json library to serialize objects out for web use.
Whenever I serialize a datetime or other included python library object (uuid as another example), it fails with
TypeError: datetime.datetime(2014, 5, 20, 9, 37, 56, 133627) is not JSON serializable
While this behaviour is well and good, what I am suggesting is that I would like a way for this library to attempt to serialize an object when it encounters an exception.
For instance, if it attempts to serialize a datetime and fails, I would like the option of submitting a method as a parameter to the json.dumps() method that the object is put into in an attempt to serialize it.
As a code example:
import datetime, json
json.dumps(dict(mydate=datetime.datetime.now()),
serialize=str)
That way, when it encounters a TypeError, it will set mydate to a string and will be serialized out.
Right now, I have to use str() on a few different fields because they are not standard types (str, int, bool, float, None, etc)
This isn't a priority and I'm open to criticisms. Would you accept a patch for this if you are too busy to work on this feature?
Thank you for your time.
Dann
----------
components: Extension Modules, Library (Lib)
messages: 218849
nosy: Dann.Ke
priority: normal
severity: normal
status: open
title: json library fails to serialize objects such as datetime
type: behavior
versions: Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21543>
_______________________________________
More information about the New-bugs-announce
mailing list