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
//! Helper that returns a JSON string.
use crate::{
    error::HelperError,
    helper::{Helper, HelperValue},
    parser::ast::Node,
    render::{Context, Render},
};

use serde_json::{to_string, to_string_pretty, Value};

/// Convert to a JSON string,
///
/// Accepts a single argument which is converted to a JSON string and returned.
///
/// The optional hash parameter `pretty` when *truthy* will pretty print the 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.hash("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))
    }
}