can't serialize datetime.datetime #12

llonchj opened this Issue Aug 22, 2012 · 8 comments

6 participants


TypeError: "can't serialize datetime.datetime"

import datetime
import msgpack
msg = msgpack.packb(
MessagePack member

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.

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

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

MessagePack member

It loses type information.


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).


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.

$ 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

Related issue:
@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

If you want to keep your msgpack values really compact, temporenc (, 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