Skip to content
Advertisement

Unwanted quotes in substituted freemarker template fields

I’m generating contents out of a Freemarker template but I get quoted values in my substituted fields.

For JSON object

{
  "name" : "Pepster"
}

In template:

Hi ${name}!

I get

Hi "Pepster"!

While I want

Hi Pepster!

The Object I feed it with is a JsonNode-tree obtained by Mapping my object with Jackson annotations:

class Name {
    @JsonProperty("name")
    public String mName;
}

Processor:

final ObjectMapper mapper = new ObjectMapper();
JsonNode jsonDocument = mapper.valueToTree(nameObject);
//...
template.process(jsonDocument, writer);

I have the feeling I’m missing some kind of configuration?

Answer

FreeMarker doesn’t add the quotes, the JsonNode‘s toString() method does, most certainly. You need to use a custom(ized) ObjectWrapper which knowns that it should call getTextValue() (or something like that) to textract the String value. You will have the same issue with JSON number, where the ObjectWrapper should call getNumberValue(). Or, if you don’t wan to invest into a custom ObjectWrapper, ${name.textValue} should certainly work, but it’s kind of awkward if you have many templates.

Advertisement