From df2637c0324d2cb3f5925b8595417b08496de4a5 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 7 Sep 2017 20:42:54 +0200 Subject: Return MarkupDisplay from json filter --- askama_shared/src/filters/json.rs | 15 ++++++++++----- 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: &S) -> Result { - serde_json::to_string_pretty(s).map_err(Error::from) +pub fn json(s: &S) -> Result> { + 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 -- cgit