Handle rendering json arrays and objects

This commit is contained in:
Tom Alexander 2020-04-12 21:40:34 -04:00
parent 24d2c1831c
commit 7a28e6a9d4
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 11 additions and 2 deletions

View File

@ -0,0 +1 @@
{"name": ["Bob", "Smith"]}

View File

@ -0,0 +1 @@
{"name": {"first": "Alice", "last": "Jones"}}

View File

@ -0,0 +1 @@
{"name": ["Bob", {"title": "sir"}, [1,2,3]]}

View File

@ -0,0 +1 @@
Hello {name}!

View File

@ -79,8 +79,13 @@ impl Renderable for serde_json::Value {
serde_json::Value::Bool(boolean) => Ok(boolean.to_string()),
serde_json::Value::Number(num) => Ok(num.to_string()),
serde_json::Value::String(string) => Ok(string.to_string()),
serde_json::Value::Array(_arr) => Err(RenderError::CantRender { elem: self }),
serde_json::Value::Object(_obj) => Err(RenderError::CantRender { elem: self }),
serde_json::Value::Array(arr) => {
let rendered: Result<Vec<String>, RenderError> =
arr.iter().map(|val| val.render()).collect();
let rendered_slice: &[String] = &rendered?;
Ok(rendered_slice.join(","))
}
serde_json::Value::Object(_obj) => Ok("[object Object]".to_owned()),
}
}
}