Jul 182011
 

The ?js_string doesn’t work quite right for JSON since it ends up escaping single-quotes. This isn’t quite right for JSON since it only allows Strings to be specified using double-quotes. Therefore, in order to properly escape Strings in FreeMarker for JSON, you need to write a custom method and add it to the model. Here’s my code for a JSON escaper method:

The JSONBuilder is a class that I wrote for creating JSON using the builder pattern. However, the escape method looks like this:

Then I add this method to my model and render the template:

My templates look like this: