This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
serde deserializer fails for structs using serde(default)
#11
Comments
The workaround is a little unpleasant but not the worst thing ever: fn hocon_to_json(hocon: hocon::Hocon) -> Fallible<serde_json::Value> {
use serde_json::{Map, Number, Value};
match hocon {
Hocon::Boolean(b) => Ok(Value::Bool(b)),
Hocon::Integer(i) => Ok(Value::Number(Number::from(i))),
Hocon::Real(f) => Ok(Value::Number(Number::from_f64(f).ok_or_else(|| {
format_err!("{} cannot be converted to serde_json::Number", f)
})?)),
Hocon::String(s) => Ok(Value::String(s)),
Hocon::Array(vec) => {
let mut res = vec![];
for v in vec.into_iter() {
res.push(hocon_to_json(v)?);
}
Ok(Value::Array(res))
}
Hocon::Hash(map) => {
let mut res = Map::new();
for (k, v) in map.into_iter() {
res.insert(k, hocon_to_json(v)?);
}
Ok(Value::Object(res))
}
Hocon::Null => Ok(Value::Null),
Hocon::BadValue(err) => bail!("bad hocon value: {}", err),
}
}
let hocon = file
.hocon()
.map_err(|e| format_err!("Error parsing HOCON from {}: {}", p.display(), e))?;
// convert to json
let json = hocon_to_json(hocon)?;
let json_text = serde_json::to_string(&json)?;
// use the json deserialize implementation to populate the struct
let cfg: MyStruct = serde_json::from_str(&json_text)
.map_err(|e| format_err!("Error parsing HOCON from {}: {}", p.display(), e))?; |
mockersf
added a commit
that referenced
this issue
Nov 22, 2019
thank you, that's fixed for the next release |
Thanks! FWIW, I tried this against my Config type and encountered one of the |
I actually know now how to remove all the remaining I'll get them fixed in the next few days, thanks for reminding me |
Only |
Thanks this is better! |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
yields:
What should happen is the serde machinery will compute a default value and that should be used instead of generating an error.
The text was updated successfully, but these errors were encountered: