-
Notifications
You must be signed in to change notification settings - Fork 537
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
Add Serialize/Deserialize for Duration #117
Comments
+1 I could really use implementation of Serialize/Deserialize for a project I'm working on. |
I wholeheartedly agree. In fact, i'd like to see serialization support for all the core types: At the moment I need to define newtypes in my own code that basically do nothing except handle Serde support. It would be absolutely wonderful to just scrap all that serialization code, as it's rather boilerplate-heavy, and thus helps turn otherwise-nice code into a "Can't see the forest for the trees" kind of deal. If this issue can be taken care of by adding |
Hi @JoeyAcc, |
I would also really appreciate support for (de)serialising |
@Geobert I only now see this response, sorry about that. A newtype is a wrapper type around one other type, for details have a look at this. How that plays out in this context is that you write such a newtype By manipulating exactly how the internal That is also where my request to implement serialization directly for all chrono types comes from. |
This would need to be implemented in rust-lang-deprecated/time to be done correctly. I believe that @lifthrasiir is working on writing a new Duration for chrono itself, which will make it possible for us to do this trivially. For now you could write your own deserializer, see my comment here for an example of how. If you feel like doing a particularly good job I would take a PR that implements that in chrono, keeping in mind that we're getting rid of the Duration before 1.0. |
If I understand you correctly, by "getting rid of the Duration" you mean the impl from |
Yes, mostly. A new |
Chrono (at least for the 0.4.x series) will continue to support three "duration-like" types:
The new |
Thank you both for the explanations. |
@JoeyAcc It will be essentially the same format as |
I see, that makes sense. I was not aware of those restrictions on the design space, so I kept wondering why have the sec/nanosec split at all. At first I thought it was due to the value range of u64/i64. But as you already remarked, The range is sufficient for a couple of hundred more years*. *This is assuming the hardware stays at nanosecond precision, which may not hold. I myself wouldn't mind moving as far as we can in the direction of the Planck time unit, for example. |
In that case we can always make |
I'm not entirely following the history of this issue, but I noticed that the dependency on the |
Same here. |
Yeah now that we are working on completely owning My inclination is to emit something like |
I like the “total number of seconds” serialization. Nice and simple, unlike ISO 8601 periods. |
I would stay on the standard as a default, and use the |
Support for a human readable format would be helpfull for config files. Something akin to humantime. |
the time crates has now a serde features too, chrono could just update time dependencies. |
+1 |
@quodlibetor I'm a bit of a rust newbie, but I'd like to give a shot at implementing the traits. I saw your linked comment, but I'm not sure where to start. Can you give me some quick pointers? |
+1 some infos that I found: |
+1 |
1 similar comment
+1 |
The current Is there any reason for it not supporting Edit: looking into this now. |
I did some investigation, here are my findings:
So to fix this, Then, the In other words, we'd like to specify the following feature in [features]
# --- snip ---
serde = ["dep:serde", "time/serde"] There are two alternatives for this to choose before stabilization. The first is to enable So here are the steps I suggest: TODO:
|
Unfortunately this needs revisiting - #553 has been closed |
Similarly to the additional serde formats we support for things like |
Which types do even support Serialization atm? |
Any update on this? |
I think we're ready to do this -- anyone want to send a PR? |
I also would love if |
Maybe something like this: I'm not sure where to copy and paste this ... pub mod timedelta_milliseconds {
use chrono::TimeDelta;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(duration: &TimeDelta, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_i64(duration.num_milliseconds())
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<TimeDelta, D::Error>
where
D: Deserializer<'de>,
{
Ok(TimeDelta::milliseconds(i64::deserialize(deserializer)?))
}
}
pub mod timedelta_milliseconds_opt {
use chrono::TimeDelta;
use serde::{de, Deserialize, Deserializer, Serializer};
pub fn serialize<S>(opt: &Option<TimeDelta>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *opt {
None => serializer.serialize_none(),
Some(dur) => serializer.serialize_some(&dur.num_milliseconds()),
}
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<TimeDelta>, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_option(OptionMillisecondTimeDeltaVisitor)
}
struct OptionMillisecondTimeDeltaVisitor;
impl<'de> de::Visitor<'de> for OptionMillisecondTimeDeltaVisitor {
type Value = Option<TimeDelta>;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("Some(milliseconds) or None")
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(None)
}
fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
where
D: de::Deserializer<'de>,
{
let millis = i64::deserialize(d)?;
Ok(Some(TimeDelta::milliseconds(millis)))
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(None)
}
}
} maybe somehere in https://docs.rs/chrono/latest/src/chrono/lib.rs.html#612 |
For features
serde
andrustc-serialize
The text was updated successfully, but these errors were encountered: