-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shall not include Time conversion by default #58
Comments
doc not updated, why not, i think its ok, if you want special format you set it. |
It's bad because your're defining the single possible variant for |
this not single possible variant, you can do many things: require "./src/msgpack"
msg = Time.now.to_msgpack(Time::Format.new("%F"))
p msg
p String.from_msgpack(msg)
time = Time.from_msgpack(Time::Format.new("%F"), msg)
p time
class A
include MessagePack::Serializable
@[MessagePack::Field(converter: Time::Format.new("%F %T"))]
property value : Time?
end
a = A.new
a.value = Time.now
msg = a.to_msgpack
p msg
p A.from_msgpack(msg)
maybe string conversion by default not optimal, but default case just simplify things. |
What if me or a third party wants to store time in Unix seconds? |
struct UnixConverter
def self.from_msgpack(unpacker)
Time.unix_ms(unpacker.read_int)
end
def self.to_msgpack(value, packer)
packer.write(value.to_unix_ms)
end
end
msg = Time.now.to_unix_ms.to_msgpack
p msg
p Int64.from_msgpack(msg)
p UnixConverter.from_msgpack(MessagePack::IOUnpacker.new(msg))
class B
include MessagePack::Serializable
@[MessagePack::Field(converter: UnixConverter)]
property value : Time?
end
a = B.new
a.value = Time.now
msg = a.to_msgpack
p msg
p B.from_msgpack(msg)
|
Yes, so why do we use a converter for unix time but don't use one for ISO formatted time? Why does ISO format has higher "precedence" in this implementation? That's what I'm talking about. |
may be unix by default would be better, i just choose the same as json converter. |
There should not be any defaults IMO unless stated otherwise by MessagePack specs. It is a breaking change indeed, but it would reduce possible confusion. I guess I have to also create an issue in Crystal itself, but afraid that the core team would deny it due to "it works as it works" and "it's too breaking" and "you're bikeshedding". We'll see. Regarding to this repo, I could create a PR creating |
i not sure that default need to be deleted, at least you can redefine default also: require "msgpack"
struct Time
def to_msgpack(packer : MessagePack::Packer)
packer.write(self.to_unix)
end
def self.new(pull : MessagePack::Unpacker)
Time.unix(pull.read_int)
end
end
msg = Time.now.to_msgpack
p msg
p Time.from_msgpack(msg) |
I have to agree with no default or a standard extension default. The current default wasted a bunch of time debugging an issue with sub seconds. The current default doesn't store them. Crystal Example:
|
I've also seen |
It's even stated in the docs:
msgpack-crystal/src/message_pack/from_msgpack.cr
Lines 202 to 204 in 4c123df
I suggest that proper implementation does not "assume" anything. There should be a converter for anything which is out of MessagePack specification scope.
A
Time
can be transferred either as ISO 8601 string or epoch seconds or even epoch milliseconds. There should not be a default constructor forTime
IMO.The text was updated successfully, but these errors were encountered: