-
Notifications
You must be signed in to change notification settings - Fork 547
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
Encoding/Decoding ISO-8601 #43
Comments
For anybody who stumbles onto this with the same problem, here's how I'm accomplishing this for now: #[derive(Debug)]
pub struct FileEntry {
pub filename: String,
pub size: u64,
pub last_modified: DateTime<UTC>,
}
impl Decodable for FileEntry {
fn decode<D: Decoder>(d: &mut D) -> Result<FileEntry, D::Error> {
d.read_struct("root", 0, |d| {
Ok(FileEntry{
filename: try!(d.read_struct_field("filename", 0, |d| Decodable::decode(d))),
size: try!(d.read_struct_field("size", 0, |d| Decodable::decode(d))),
last_modified: {
let json_str: String = try!(d.read_struct_field("last_modified", 0, |d| Decodable::decode(d)));
match json_str.parse() {
Ok(datetime) => datetime,
Err(err) => return Err(d.error(err.description())),
}
},
})
})
}
} |
Probably fixing this would also fix #42 and vice versa. I'm almost dormant nowadays but looking forward to fix this. Thank you for reports. |
Is there anything particularly bad about the following implementations of impl Decodable for DateTime<UTC> {
fn decode<D: Decoder>(d: &mut D) -> Result<Outcome, D::Error> {
let field = try!(d.read_str());
match field.parse() {
Ok(result) => Ok(result),
Err(_) => Err(d.error(&*format!("Could not parse '{}' as a DateTime<UTC>.", field)))
}
}
}
impl Encodable for DateTime<UTC> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_str(self.to_rfc3339())
}
} |
I'm also facing this problem. Serialization to ISO format would be great. |
I am facing this problem too. I want to use
It will be great if it can be encoded to ISO-8601. Is there any way to specify the format for DateTime when serializaiton? |
There are now functions for this, but they might not be completely correct: I'd expect |
@jonas-schievink I think the documentation for (Why is the "full" ISO 8601 syntax unsupported? That's because ISO 8601 is in some sense a standard template, and requires a reasonable subset to be actually implemented. ISO 8601 covers very large usage cases and implementing all of them is close to waste---for example, you can write something like |
What are your thoughts on serialization to and from ISO-8601 strings?
I'm looking to decode an API response with something along these lines:
Currently such decode returns in
Err(ExpectedError("Object", "\"2015-05-01T21:48:27.000Z\""))
Just trying to figure out if I'll be re-inventing the wheel, or if this is something that fits within the scope of Chrono.
The text was updated successfully, but these errors were encountered: