3Hren/msgpack-rust

`serialize_i64` not writing an int64 msgpack type when value fits into something smaller

edgimar opened this issue · 2 comments

I am trying to use [serde(serialize_with = "myfunc")] to serialize a std::time::Duration type, where myfunc is defined as:

fn myfunc<S>(duration: &std::time::Duration, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_i64(duration.as_millis() as i64)
}

When I attempt to serialize the struct with a Duration value of 256 using to_vec_named(), the serialized msgpack contains [cd, 1, 0] for the value.

This is intentional since 23f9b31

Couldn't this "most effective" behavior be made an option? There are some cases where one wants to explicitly encode with a specific type.