title: Simple template library with binding capability for Java
body: <p>I want to generate valid JSON with the help of a template engine. In particular, I want the engine to replace the placeholders in the template with the properties of a model class.
  The engine should allow the use converters for complex classes like <code>java.util.Date</code>.
  Additionally, I do not want to explicitly handle lists in the template itself, instead I want any collection-like type to produce valid JSON arrays.</p>
  
  <p>This could be an example of a template:</p>
  
  <pre><code>{
    "uber" :
    {
      "version" : "1.0",
      "data" :
      [
        {
          "rel" : $relations,
          "data" :
          [
            {"name" : "firstname", "value" : $firstname},
            {"name" : "lastname", "value" : $lastname}
          ]
        }
      ]
    }
  }
  </code></pre>
  
  <p>I want to replace the variables (<code>$relations</code>, <code>$firstname</code>, <code>$lastname</code>) in this template from a model that has a relations, a firstname and a lastname property. This should also work for nested properties. (for example: <code>$address.street</code>)</p>
  
  <p>After binding the model, the JSON could look like this:</p>
  
  <pre><code>{
    "uber" :
    {
      "version" : "1.0",
      "data" :
      [
        {
          "rel" : ["person"],
          "data" :
          [
            {"name" : "firstname", "label" : "Firstname", "value" : "Max"},
            {"name" : "lastname", "label" : "Lastname", "value" : "Mustermann"}
          ]
        }
      ]
    }
  }
  </code></pre>
  
  <p>Please note, that the engine needs to take care of using quotation marks when necessary. </p>
  
  <p>Is there any template library that is capable of doing this or do I need to roll my own library?</p>
  
  <p>I have already looked into the following libraries:</p>
  
  <ul>
  <li>Freemarker</li>
  <li>Velocity</li>
  <li>Stringtemplate</li>
  </ul>
  
  <p>And I also read several threads: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8001398/java-template-library-similar-to-zpt-attribute-language">Java Template library similar to ZPT (attribute language)</a></li>
  <li><a href="https://stackoverflow.com/questions/17591568/spring-template-engine-for-rendering-json">Spring template engine for rendering JSON</a></li>
  </ul>
  
  <p>However, I did not find a satisfying solution up to now.</p>
  
  <p><strong>Background information:</strong></p>
  
  <p><a href="http://rawgit.com/uber-hypermedia/specification/master/uber-hypermedia.html" rel="noreferrer">Uber</a> is domain-agnostic hypermedia format that nests <code>data</code> elements in order to encode the data. 
  This format is very cumbersome to produce if you want to serialize it with a library like Jackson because you have to create a lot of objects and nested them via setters which results in a lot of code and is also very error-prone.</p>
  
  <p>That is why I thought of using templates to generate the output and let the user write a template and later bind a model to this template to generate the output. </p>