diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-09-07 20:42:54 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-09-07 20:42:54 +0200 |
commit | df2637c0324d2cb3f5925b8595417b08496de4a5 (patch) | |
tree | 53a7c34df3b4e4cf41b73ee5fa97e00a7b8757dc /askama_shared | |
parent | e559ed42102d425f23701e061cdea509a0f325b4 (diff) | |
download | askama-df2637c0324d2cb3f5925b8595417b08496de4a5.tar.gz askama-df2637c0324d2cb3f5925b8595417b08496de4a5.tar.bz2 askama-df2637c0324d2cb3f5925b8595417b08496de4a5.zip |
Return MarkupDisplay from json filter
Diffstat (limited to 'askama_shared')
-rw-r--r-- | askama_shared/src/filters/json.rs | 15 | ||||
-rw-r--r-- | askama_shared/src/generator.rs | 2 |
2 files changed, 11 insertions, 6 deletions
diff --git a/askama_shared/src/filters/json.rs b/askama_shared/src/filters/json.rs index 4443fb4..c388128 100644 --- a/askama_shared/src/filters/json.rs +++ b/askama_shared/src/filters/json.rs @@ -1,6 +1,8 @@ use serde::Serialize; use serde_json; use errors::{Error, Result}; +use MarkupDisplay; + /// Serialize to JSON (requires `serde-json` feature) /// @@ -8,8 +10,11 @@ use errors::{Error, Result}; /// /// This will panic if `S`'s implementation of `Serialize` decides to fail, /// or if `T` contains a map with non-string keys. -pub fn json<S: Serialize>(s: &S) -> Result<String> { - serde_json::to_string_pretty(s).map_err(Error::from) +pub fn json<S: Serialize>(s: &S) -> Result<MarkupDisplay<String>> { + match serde_json::to_string_pretty(s) { + Ok(s) => Ok(MarkupDisplay::Safe(s)), + Err(e) => Err(Error::from(e)), + } } @@ -19,9 +24,9 @@ mod tests { #[test] fn test_json() { - assert_eq!(json(&true).unwrap(), "true"); - assert_eq!(json(&"foo").unwrap(), r#""foo""#); - assert_eq!(json(&vec!["foo", "bar"]).unwrap(), + assert_eq!(json(&true).unwrap().unsafe_string(), "true"); + assert_eq!(json(&"foo").unwrap().unsafe_string(), r#""foo""#); + assert_eq!(json(&vec!["foo", "bar"]).unwrap().unsafe_string(), r#"[ "foo", "bar" diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 2a56083..9ac6964 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -270,7 +270,7 @@ impl<'a> Generator<'a> { self._visit_filter_args(args); self.write(")?"); - if name == "safe" || name == "escape" || name == "e" { + if name == "safe" || name == "escape" || name == "e" || name == "json" { DisplayWrap::Wrapped } else { DisplayWrap::Unwrapped |