-
Notifications
You must be signed in to change notification settings - Fork 562
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
Why not impl TryInto<InnerType>
for Value
?
#902
Comments
I agree. Code like let map = match value
.method()
.another_method()
{
serde_json::Value::Object(o) => o,
_ => panic!("expected an object")
}; could be written a lot cleaner like let map: serde_json::Map<_, _> = value
.method()
.another_method()
.try_into()
.expect("expected an object"); By the way, it would be |
Funny how I started implementing it on my own. I would add to that, do impl on the borrow of Value use core::convert::TryInto;
impl TryFrom<&Value> for i64 {
type Error = Error;
fn try_from(value: &Value) -> Result<Self, Self::Error> {
value
.as_i64()
.ok_or(serde::de::Error::custom("Cannot interpret Value as i64"))
}
}
impl<'a> TryFrom<&'a Value> for &'a str {
type Error = Error;
fn try_from(value: &'a Value) -> Result<Self, Self::Error> {
value
.as_str()
.ok_or(serde::de::Error::custom("Cannot interpret Value as &str"))
}
} that would allow you to do: fn get_property<'a, T>(val: &'a Value, name: &str) -> std::result::Result<T, T::Error>
where
T: TryFrom<&'a Value>,
<T as std::convert::TryFrom<&'a Value>>::Error: serde::de::Error,
{
val.as_object()
.ok_or(serde::de::Error::custom("value is not an object"))?
.get(name)
.ok_or(serde::de::Error::custom("no property found"))?
.try_into()
}
#[test]
fn it_works() {
let j_son = json!({"number": 1, "str": "hello"});
// Don't consume j_son, so I can call it many times
assert_eq!(get_property::<i64>(&j_son, "number").unwrap(), 1);
assert_eq!(get_property::<&str>(&j_son, "str").unwrap(), "hello");
} |
There should be an impl for both |
I also would like to see this. I have an use-case where I want to delay fully deserializing a |
The
InnerType
can bebool
,u64
,i64
,f64
,String
,Vec<Value>
or other types implementedas_{type}
, to build a more convient API.The text was updated successfully, but these errors were encountered: