In this section you will learn how to convert a value to JSON using the Jackson library and much more.
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars-jackson2</artifactId>
<version>1.0.0</version>
</dependency>
The Jackson helper convert a JavaObject to JSON.
handlebars.registerHelper("json", JacksonHelper2.INSTANCE);
ObjectMapper
:
ObjectMapper om = ...;
handlebars.registerHelper("json", new JacksonHelper2(om));
{{json this}}
{{json this view="foo.MyFullyQualifiedClassName"}}
handlebars.registerHelper("json", new JacksonHelper2()
.viewAlias("myView", foo.MyFullyQualifiedClassName.class);
...
{{json this view="myView"}}
escapeHTML
if your JSON has HTML content.
{{json this escapeHTML=true}}
The Jackson module has a custom ValueResolver that knows how to extract values from aJsonNode
object.
JsonNodeValueResolver
:
Context context = Context
.newBuilder(model)
.resolver(JsonNodeValueResolver.INSTANCE)
.build();
As you see, Handlebars.java provides you a very nice integration with Jackson the most widely used JSON library for Java.
Thank you, for reading the Handlebars.java blog.