-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expose the Serializer and Deserializer types #44
Comments
I've just discovered Serde a few days ago and have since been trying to use it in a toy project/game, so please excuse the newbie question, but is a fix for this required for stateful deserialization with serde-yaml? I'd need to deserialize names of items into items (e.g. Arc<Sprite>) by looking them up in a library of items (that I'd like to pass as state). It's not often that I don't have to look up anything at all during deserialization, so I'm hopeful that I'm missing something. |
From what I understand, this is also a requirements to user serde_transcode with yaml too. The documentation is casting json to json. I'd like to cast yaml to json, and I suppose it is not possible right now. |
Workarounds: (v0.7.4) For now one can deserialize from a Value, but if you do this the naive way you end up with terrible error messages for anything other than syntax errors. (e.g. However, there is a workaround for that, too, for those who can afford it: use ::serde_yaml::Value;
use ::std::io::Read;
use ::serde::de::DeserializeOwned;
fn read_yaml<T: DeserializeOwned>(mut r: impl Read) -> Result<T, Error> {
// First, read into a form that we can read from multiple times.
let mut s = String::new();
r.read_to_string(&mut s)?;
// Get a Value, which implements Deserializer.
let value = ::serde_yaml::from_str(&s)?;
match _read_using_serde_ignored(value) {
Ok(out) => Ok(out),
Err(_) => {
// That error message was garbage. Let's parse without serde_ignored
// directly from the string, to get an error message with position info.
::serde_yaml::from_str(&s)?;
unreachable!();
}
}
}
fn _read_using_serde_ignored<T: DeserializeOwned>(value: Value) -> Result<T, Error> {
::serde_ignored::deserialize(
value,
|path| warn!("Unused config item (possible typo?): {}", path),
).map_err(Into::into)
} |
@mardiros: This is not true: let v: serde_json::Value = serde_yaml::from_str(yml_string)? |
Thanks I didn't know that works! https://github.com/mardiros/rustaman/blob/features/multi-requests/src/helpers/http.rs#L68 |
Sounds good @pickfire, go for it. |
@dtolnay I think I might need some guidance on how to get this done. I thought it is just as easy as slapping pub against I noticed that there are |
I am not sure how this could be implemented using yaml-rust after trying out an implementation on #129. Should we write ourselves? |
I believe this would be easier to implement after solving dtolnay/request-for-implementation#9 and switching backends. |
Im just leaving this as a note here: A exposed (De)Serializer type is also required to drive something like serde_transcode (as seen here) |
This is required for composition with other libraries like
serde_ignored
.This is harder than just slapping
pub
on the thing because the Deserializer currently borrows some data owned by the serde_yaml::from_str method:May need to refactor into a public owned Deserializer and an internal borrowed Deserializer.
The Serializer is also hard because it doesn't write to an underlying io::Write like JSON's serializer does.
The text was updated successfully, but these errors were encountered: