-
When I upgrade to leon v2.0.0, I cannot make the following code to compile: #[repr(transparent)]
struct ObjectWrapper<'a>(&'a HashMap<&'a str, serde_json_borrow::Value<'a>>);
impl leon::Values for ObjectWrapper<'_> {
fn get_value(&self, key: &str) -> Option<Cow<'_, str>> {
let (top, keys) = match key.split_once('.') {
Some((top, key)) => (top, key.split('.').collect()),
None => (key, vec![]),
};
let mut val = self.0.get(top)?;
for key in keys {
val = key
.parse::<usize>()
.map_or_else(|_| val.get(key), |idx| val.get(idx));
}
match val {
serde_json_borrow::Value::Null => None,
serde_json_borrow::Value::Bool(v) => {
Some(Cow::Borrowed(if *v { "true" } else { "false" }))
}
serde_json_borrow::Value::Number(v) => {
Some(Cow::Owned(v.as_f64().unwrap().to_string()))
}
serde_json_borrow::Value::Str(v) => Some(Cow::Borrowed(v)),
serde_json_borrow::Value::Array(_) => None,
serde_json_borrow::Value::Object(_) => None,
}
}
} the compile error:
Could you please give me some hints, @NobodyXu Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
NobodyXu
Apr 28, 2023
Replies: 1 comment 1 reply
-
I think this is because of the serde_json_borrow::Index, it requires the key to outlive 'a, this thus somehow make the lifetime of the return value to be relates to the key since you use a subset of key to index the json-value. Try manually look up the key in Value::object, I think this will solve your problem. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
flisky
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is because of the serde_json_borrow::Index, it requires the key to outlive 'a, this thus somehow make the lifetime of the return value to be relates to the key since you use a subset of key to index the json-value.
Try manually look up the key in Value::object, I think this will solve your problem.