Search code examples
unicodefreemarkerorg.json

How to escape unicode symbols in freemarker template processing?


I'm using freemarker templates to convert JSON to XML. It works fine except when the input json contains some unicode symbols. Till now I have encountered en-dash and em-dash and the FTL transformation fails with : lexical error: encountered "u" (117)

The template fails when I'm using ?eval operation on a variable.

I'm using freemarker v2.3.28 and I have set encoding to UTF-8.

P.S. I checked that copyright and trademark symbols are working fine.


Solution

  • The problem is certainly that those characters are escaped in the source JSON as \uXXXX, but the FTL string syntax doesn't have \u escape. ?eval interprets its input as FTL expression, not as a JSON expression. They happen to be similar, but there are differences. (BTW, to my knowledge, en-dash and em-dash need not be escaped in JSON, similarly to copyright, but of course it's allowed to escape any character.)

    What to do? The FreeMarker-ish approach is adding the JSON to your data-model already parsed to Map-s (or Java beans) and List-s and such. The template isn't meant to parse input. If that's not feasible, then you could add your custom method to the data model (maybe as shared variable on the Configuration-level) which does real JSON parsing, and then you call it like eval_json(x).

    Anyway, what's the use case? The desire to parse JSON inside the template comes up often on SO. Maybe the next FreeMarker version should have a ?eval_json, if the use case is valid.