I want to send a datetime.datetime object in serialized form from Python using JSON and de-serialize in JavaScript using JSON. What is the best way to do this?
相关问题
- Is there a limit to how many levels you can nest i
- how to define constructor for Python's new Nam
- Jackson Deserialization not calling deserialize on
- streaming md5sum of contents of a large remote tar
- How to maintain order of key-value in DataFrame sa
My advice is to use a library. There are several available at pypi.org.
I use this one, it it works good: https://pypi.python.org/pypi/asjson
Here's a fairly complete solution for recursively encoding and decoding datetime.datetime and datetime.date objects using the standard library
json
module. This needs Python >= 2.6 since the%f
format code in the datetime.datetime.strptime() format string is only supported in since then. For Python 2.5 support, drop the%f
and strip the microseconds from the ISO date string before trying to convert it, but you'll loose microseconds precision, of course. For interoperability with ISO date strings from other sources, which may include a time zone name or UTC offset, you may also need to strip some parts of the date string before the conversion. For a complete parser for ISO date strings (and many other date formats) see the third-party dateutil module.Decoding only works when the ISO date strings are values in a JavaScript literal object notation or in nested structures within an object. ISO date strings, which are items of a top-level array will not be decoded.
I.e. this works:
And this too:
But this doesn't work as expected:
Here's the code:
I've worked it out.
Let's say you have a Python datetime object, d, created with datetime.now(). Its value is:
You can serialize it to JSON as an ISO 8601 datetime string:
The example datetime object would be serialized as:
This value, once received in the Javascript layer, can construct a Date object:
As of Javascript 1.8.5, Date objects have a toJSON method, which returns a string in a standard format. To serialize the above Javascript object back to JSON, therefore, the command would be:
Which would give you:
This string, once received in Python, could be deserialized back to a datetime object:
This results in the following datetime object, which is the same one you started with and therefore correct:
Apparently The “right” JSON (well JavaScript) date format is 2012-04-23T18:25:43.511Z - UTC and "Z". Without this JavaScript will use the web browser's local timezone when creating a Date() object from the string.
For a "naive" time (what Python calls a time with no timezone and this assumes is local) the below will force local timezone so that it can then be correctly converted to UTC:
why is this so hard.
Using
json
, you can subclass JSONEncoder and override the default() method to provide your own custom serializers:Then, you can call it like this:
You can add the 'default' parameter to json.dumps to handle this:
Which is ISO 8601 format.
A more comprehensive default handler function:
Update: Added output of type as well as value.
Update: Also handle date