Jackson2Helper implements Helper<Object> { @Override public Object apply(final Object context, final Options options) throws IOException { if (context == null) { return options.hash("default", ""); } String viewName = options.hash("view", ""); JsonGenerator generator = null; try { final ObjectWriter writer; if (!Handlebars.Utils.isEmpty(viewName)) { Class<?> viewClass = alias.get(viewName); if (viewClass == null) { viewClass = getClass().getClassLoader().loadClass(viewName); } writer = mapper.writerWithView(viewClass); } else { writer = mapper.writer(); } JsonFactory jsonFactory = mapper.getFactory(); SegmentedStringWriter output = new SegmentedStringWriter(jsonFactory._getBufferRecycler()); generator = jsonFactory.createJsonGenerator(output); Boolean escapeHtml = options.hash("escapeHTML", Boolean.FALSE); if (escapeHtml) { generator.setCharacterEscapes(new HtmlEscapes()); } Boolean pretty = options.hash("pretty", Boolean.FALSE); if (pretty) { writer.withDefaultPrettyPrinter().writeValue(generator, context); } else { writer.writeValue(generator, context); } generator.close(); return new Handlebars.SafeString(output.getAndClear()); } catch (ClassNotFoundException ex) { throw new IllegalArgumentException(viewName, ex); } finally { if (generator != null && !generator.isClosed()) { generator.close(); } } } Jackson2Helper(final ObjectMapper objectMapper); private  Jackson2Helper(); @Override Object apply(final Object context, final Options options); Jackson2Helper viewAlias(final String alias,
      final Class<?> viewClass); static final Helper<Object> INSTANCE; }
@Test public void toJSON() throws IOException { Handlebars handlebars = new Handlebars(); handlebars.registerHelper("@json", Jackson2Helper.INSTANCE); Template template = handlebars.compileInline("{{@json this}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\"title\":\"First Post\",\"body\":\"...\",\"comments\":[]}", result); }
@Test public void toPrettyJSON() throws IOException { Handlebars handlebars = new Handlebars(); handlebars.registerHelper("@json", Jackson2Helper.INSTANCE); Template template = handlebars.compileInline("{{@json this pretty=true}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\n" + " \"title\" : \"First Post\",\n" + " \"body\" : \"...\",\n" + " \"comments\" : [ ]\n" + "}", result); }
@Test public void toJSONViewInclusive() throws IOException { Handlebars handlebars = new Handlebars(); handlebars.registerHelper("@json", Jackson2Helper.INSTANCE); Template template = handlebars .compileInline("{{@json this view=\"com.github.jknack.handlebars.Blog$Views$Public\"}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\"title\":\"First Post\",\"body\":\"...\",\"comments\":[]}", result); }
@Test public void toJSONViewExclusive() throws IOException { Handlebars handlebars = new Handlebars(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); handlebars.registerHelper("@json", new Jackson2Helper(mapper)); Template template = handlebars .compileInline("{{@json this view=\"com.github.jknack.handlebars.Blog$Views$Public\"}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\"title\":\"First Post\"}", result); }
@Test(expected = HandlebarsException.class) public void jsonViewNotFound() throws IOException { Handlebars handlebars = new Handlebars(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); handlebars.registerHelper("@json", new Jackson2Helper(mapper)); Template template = handlebars .compileInline("{{@json this view=\"missing.ViewClass\"}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\"title\":\"First Post\"}", result); }
@Test public void escapeHtml() throws IOException { Handlebars handlebars = new Handlebars(); handlebars.registerHelper("@json", Jackson2Helper.INSTANCE); Map<String, String> model = new HashMap<String, String>(); model.put("script", "<script text=\"text/javascript\"></script>"); assertEquals("{\"script\":\"<script text=\\\"text/javascript\\\"></script>\"}", handlebars .compileInline("{{@json this}}").apply(model)); assertEquals( "{\"script\":\"\\u003Cscript text=\\\"text/javascript\\\"\\u003E\\u003C/script\\u003E\"}", handlebars.compileInline("{{@json this escapeHTML=true}}").apply(model)); }