Add an is_castable() function to stop casting to number for booleans in the @size helper.

This commit is contained in:
Tom Alexander
2020-06-14 18:27:28 -04:00
parent e54e20d254
commit a622a7e1bc
4 changed files with 45 additions and 5 deletions

View File

@@ -317,6 +317,17 @@ impl Loopable for serde_json::Value {
}
impl Sizable for serde_json::Value {
fn is_castable(&self) -> bool {
match self {
serde_json::Value::Null => true,
serde_json::Value::Bool(_) => false,
serde_json::Value::Number(_) => true,
serde_json::Value::String(_) => true,
serde_json::Value::Array(_) => true,
serde_json::Value::Object(_) => true,
}
}
fn get_size<'a>(&'a self) -> Option<IceResult<'a>> {
match self {
serde_json::Value::Null => {
@@ -368,7 +379,7 @@ impl Castable for serde_json::Value {
(serde_json::Value::Array(_), "number") => None,
(serde_json::Value::Object(_), "number") => None,
(serde_json::Value::String(text), "string") => Some(IceResult::from_borrowed(self)),
(serde_json::Value::String(_), "string") => Some(IceResult::from_borrowed(self)),
(serde_json::Value::Number(num), "string") => Some(IceResult::from_owned(
serde_json::Value::String(num.to_string()),
)),