Json Extension

The JSON extension adds Jtwig the capability of converting Java objects to a String JSON representation. It includes a function json_encode to perform such operation.

{{ json_encode(object) }}

Under the wood, jtwig json extension will, using the configured mapper, convert the given object into a string.

Configuration

EnvironmentConfigurationBuilder
    .configuration()
        .extensions()
            .add(new JsonExtension(JsonMapperProviderConfigurationBuilder
                .jsonConfiguration()
                .build())
            )
        .and()
    .build()

The configuration allows one to define multiple JsonMapperProvider, which supplies an instance of a mapper. The first use of the function json_encode will cause the mapper to be resolved and stored as singleton. Jtwig iterates over the list of JsonMapperProvider where the first one resolving will be used. By default, Jtwig defines two JsonMapperProvider, the Jackson2JsonMapperProvider and JacksonJsonMapperProvider which use, respectively, Jackson version 2 and 1.

Integration

Integrating Jtwig JSON Extension on your project is quite simple with the help of dependency managers. To check the most recent version, go to bintray.

Gradle

repositories {
    jcenter()
}
dependencies {
    compile 'org.jtwig:jtwig-json-extension:1.X'
}

Maven

<repositories>
    <repository>
        <id>bintray</id>
        <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.jtwig</groupId>
        <artifactId>jtwig-json-extension</artifactId>
        <version>1.X</version>
    </dependency>
</dependencies>

Examples

Check the jtwig-examples project on github for examples using this render engine.