1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::{
error::HelperError,
helper::{Helper, HelperValue},
parser::ast::Node,
render::{Context, Render},
};
use serde_json::{to_string, to_string_pretty, Value};
pub struct Json;
impl Helper for Json {
fn call<'render, 'call>(
&self,
_rc: &mut Render<'render>,
ctx: &Context<'call>,
_template: Option<&'render Node<'render>>,
) -> HelperValue {
ctx.arity(1..1)?;
let target = ctx.get(0).unwrap();
let pretty =
ctx.is_truthy(ctx.param("pretty").unwrap_or(&Value::Bool(false)));
let value = if pretty {
Value::String(to_string_pretty(&target).map_err(HelperError::from)?)
} else {
Value::String(to_string(&target).map_err(HelperError::from)?)
};
Ok(Some(value))
}
}