{"id":11689,"date":"2012-11-21T15:31:00","date_gmt":"2012-11-21T14:31:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=fa5fd88b80c1dd7a2e40830d3ad805a4"},"modified":"2022-07-26T16:42:30","modified_gmt":"2022-07-26T14:42:30","slug":"grails-render-as-json-catch-2","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2012\/11\/21\/grails-render-as-json-catch-2\/","title":{"rendered":"Grails render as JSON catch"},"content":{"rendered":"<p>One of a reasons your controller doesn&#8217;t render a proper response in JSON format might be wrong package name that you use. It is easy to overlook. Import are on top of a file, you look at your code and everything seems to be fine. Except response is still not in JSON format.<\/p>\n<p>Consider this simple controller:<\/p>\n<pre><code>&lt;span class=\"keyword\"&gt;class&lt;\/span&gt; &lt;span class=\"class\"&gt;RestJsonCatchController&lt;\/span&gt; {&lt;br \/&gt;    &lt;span class=\"keyword\"&gt;def&lt;\/span&gt; &lt;span class=\"method\"&gt;grailsJson&lt;\/span&gt;() {&lt;br \/&gt;        &lt;span class=\"method\"&gt;render&lt;\/span&gt;([&lt;span class=\"field\"&gt;first&lt;\/span&gt;: &lt;span class=\"string\"&gt;&#039;foo&#039;&lt;\/span&gt;, &lt;span class=\"field\"&gt;second&lt;\/span&gt;: &lt;span class=\"number\"&gt;5&lt;\/span&gt;] &lt;span class=\"keyword\"&gt;as&lt;\/span&gt; grails.converters.&lt;span class=\"class\"&gt;JSON&lt;\/span&gt;)&lt;br \/&gt;    }&lt;br \/&gt;&lt;br \/&gt;    &lt;span class=\"keyword\"&gt;def&lt;\/span&gt; &lt;span class=\"method\"&gt;netSfJson&lt;\/span&gt;() {&lt;br \/&gt;        &lt;span class=\"method\"&gt;render&lt;\/span&gt;([&lt;span class=\"field\"&gt;first&lt;\/span&gt;: &lt;span class=\"string\"&gt;&#039;foo&#039;&lt;\/span&gt;, &lt;span class=\"field\"&gt;second&lt;\/span&gt;: &lt;span class=\"number\"&gt;5&lt;\/span&gt;] &lt;span class=\"keyword\"&gt;as&lt;\/span&gt; net.sf.json.&lt;span class=\"class\"&gt;JSON&lt;\/span&gt;)&lt;br \/&gt;    }&lt;br \/&gt;}&lt;br \/&gt;\n<\/code><\/pre>\n<p>And now, with finger crossed&#8230; We have a winner!<\/p>\n<pre><code>&lt;span class=\"keyword\"&gt;$&lt;\/span&gt; curl localhost:8080\/example\/restJsonCatch\/grailsJson&lt;br \/&gt;{\"first\":\"foo\",\"second\":5}&lt;br \/&gt;&lt;span class=\"keyword\"&gt;$&lt;\/span&gt; curl localhost:8080\/example\/restJsonCatch\/netSfJson&lt;br \/&gt;{first=foo, second=5}&lt;br \/&gt;\n<\/code><\/pre>\n<p>As you can see only <code>grails.converters.&lt;span class=\"class\"&gt;JSON&lt;\/span&gt;<\/code> converts your response to JSON format. There is no such converter for <code>net.sf.json.&lt;span class=\"class\"&gt;JSON&lt;\/span&gt;<\/code>, so Grails has no converter to apply and it renders Map normally.<\/p>\n<p><strong>Conclusion: always carefully look at your imports if you&#8217;re working with JSON in Grails!<\/strong><\/p>\n<p><strong>Edit:<\/strong> Burt suggested that this is a bug. I&#8217;ve submitted JIRA issue here: <a href=\"http:\/\/jira.grails.org\/browse\/GRAILS-9622\">GRAILS-9622 render as class that is not a codec should throw exception<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"One of a reasons your controller doesn&#8217;t render a proper response in JSON format  might be wrong package name that you use. It is easy to overlook. Import are on top of a file, you look at your code and everything seems to be fine. Except response is still not in JSON format.\nConsider this simple controller:\nclass RestJsonCatchController {def grailsJson() {render([first: 'foo', second: 5] as grails.converters.JSON)    }def netSfJson() {render([first: 'foo', second: 5] as net.sf.json.JSON)    }}\nAnd now, with finger crossed&#8230; We have a winner!\n$ curl localhost:8080\/example\/restJsonCatch\/grailsJson{\"first\":\"foo\",\"second\":5}$ curl localhost:8080\/example\/restJsonCatch\/netSfJson{first=foo, second=5}\nAs you can see only grails.converters.JSON converts your response to JSON format. There is no such converter for net.sf.json.JSON, so Grails has no converter to apply and it renders Map normally.\nConclusion: always carefully look at your imports if you&#8217;re working with JSON in Grails!\nEdit: Burt suggested that this is a bug. I&#8217;ve submitted JIRA issue here: GRAILS-9622 render as class that is not a codec should throw exception\n","protected":false},"author":37,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[594,71,50,283,402],"class_list":{"0":"post-11689","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-api","8":"tag-frontend","9":"tag-groovy","10":"tag-json","11":"tag-rest"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=11689"}],"version-history":[{"count":2,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11689\/revisions"}],"predecessor-version":[{"id":14403,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11689\/revisions\/14403"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=11689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=11689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=11689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}