Search code examples
jsongrailsgsp

Render JSON from GSP


I have a few GSP pages that are returning JSON data. It looks like (only a lot larger)

<%@ page contentType="application/json" %>
{
  "foo": "${somevalue}"
}

And I am using this with the page renderer:

groovyPageRenderer.render view: '/renderService/foo', model: [foo: foo]

This works fine except that the values containing double quotes and other special characters are encoded with &quot; instead of \"

How can I force the encoding of these parameters to be JSON instead of HTML/XML ?


Solution

  • You can change the default codec for a particular GSP page with a directive:

    <%@page expressionCodec="javascript" %>
    

    More details in the section on XSS prevention in the Grails user guide.