@Test public void toJSON() throws IOException { Handlebars handlebars = new Handlebars(); handlebars.registerHelper("@json", JSONHelper.INSTANCE); Template template = handlebars.compile("{{@json this}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\"title\":\"First Post\",\"body\":\"...\",\"comments\":[]}", result); }
@Test(expected = HandlebarsException.class) public void jsonViewNotFound() throws IOException { Handlebars handlebars = new Handlebars(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.DEFAULT_VIEW_INCLUSION, false); handlebars.registerHelper("@json", new JSONHelper(mapper)); Template template = handlebars.compile("{{@json this view=\"missing.ViewClass\"}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\"title\":\"First Post\"}", result); }
@Test public void toJSONAliasViewExclusive() throws IOException { Handlebars handlebars = new Handlebars(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.DEFAULT_VIEW_INCLUSION, false); handlebars.registerHelper("@json", new JSONHelper(mapper).viewAlias("myView", Public.class)); Template template = handlebars.compile("{{@json this view=\"myView\"}}"); CharSequence result = template.apply(new Blog("First Post", "...")); assertEquals("{\"title\":\"First Post\"}", result); }