Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

can't serialize datetime.datetime #12

Closed
llonchj opened this Issue · 8 comments

6 participants

Jordi Llonch INADA Naoki Travis Mehlinger HoverHell David Pravec Wouter Bolsterlee
Jordi Llonch

TypeError: "can't serialize datetime.datetime"

import datetime
import msgpack
msg = msgpack.packb(datetime.datetime.now())
INADA Naoki
Owner
Travis Mehlinger

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.

INADA Naoki
Owner

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.

INADA Naoki methane closed this
Travis Mehlinger

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

INADA Naoki
Owner

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

David Pravec

Timestamp related discussion is also here msgpack/msgpack#128

Y.Kentaro y-ken referenced this issue from a commit in y-ken/fluent-plugin-mysql-replicator
Y.Kentaro 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
7bfa0aa
Y.Kentaro y-ken referenced this issue from a commit in y-ken/fluent-plugin-mysql-replicator
Y.Kentaro 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
Marc Fournier mfournier referenced this issue from a commit in c2corg/infrastructure
Marc Fournier mfournier quote grain values
to avoid hitting msgpack/msgpack-python#12
with serialnumber=2010-10-18
e68091e
Wouter Bolsterlee

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
Something went wrong with that request. Please try again.