can't serialize datetime.datetime #12

Closed
llonchj opened this Issue Aug 22, 2012 · 8 comments

6 participants

@llonchj

TypeError: "can't serialize datetime.datetime"

import datetime
import msgpack
msg = msgpack.packb(datetime.datetime.now())
@methane
MessagePack member
@tmehlinger

Though there's no standard for date/time representation in JSON, there's a strong push to standardize on ISO 8601.
Javascript's Date().toJSON() behavior is, nearly universally, to return an ISO 8601 timestamp. An overwhelming number of libraries and services that use JSON for interchange assume ISO 8601 for date/time representation. I think it's safe to assume you can serialize datetime objects with .isoformat() by default instead of forcing the developer to provide a default function.

@methane
MessagePack member

DateTime object and it's string representation is different thing.
I don't like making default behavior more lossy.

I welcome proposal of more easy and efficient way to customize (de)serialization.

@methane methane closed this Aug 28, 2012
@tmehlinger

Using .isoformat() as the default behavior wouldn't be lossy. ISO 8601 represents all the information available in a datetime.

@methane
MessagePack member

It loses type information.

@HoverHell

What about extending msgpack format for datetime and decimal (and, possibly, something else)? (were there already discussions on this point?)
Or it would be preferable to add those customizations locally instead of main msgpack format (assuming messages are only used internally)?

proposal of more easy and efficient way to customize (de)serialization

A default-like function that returns packed data (instead of just serializable data)? Though in practical use that would mean extending the format as well. Best I can see (probably not very good) is adding “custom format” type to msgpack with (custom_format_id, length, data_string) packed internals (with corresponding custom functions for (de)serializing).

@alekibango

Timestamp related discussion is also here msgpack/msgpack#128

@y-ken y-ken added a commit to y-ken/fluent-plugin-mysql-replicator that referenced this issue Dec 10, 2013
@y-ken y-ken Bugfix: Time class couldn't serialize with to_msgpack
To resolve this issue, convert Time to String class with to_s.

Regression:
$ irb
require 'time'
require 'msgpack'
Time.parse("2013-11-22 17:30:00 +0900").to_msgpack
NoMethodError: undefined method `to_msgpack' for 2013-11-22 17:30:00
+0900:Time

Related issue:
msgpack/msgpack-python#12
2f5af39
@mfournier mfournier added a commit to c2corg/infrastructure that referenced this issue Jun 7, 2014
@mfournier mfournier quote grain values
to avoid hitting msgpack/msgpack-python#12
with serialnumber=2010-10-18
e68091e
@wbolster

If you want to keep your msgpack values really compact, temporenc (http://temporenc.org), a comprehensive binary encoding format for dates and times, might be what you're looking for. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment