Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering 'as JSON' in Grails with conditional operator doesn't render correctly

Came across this strange result today trying to render a list of objects as JSON in Grails 2.0.4...(i know i'm gonna regret asking this on account of something right under my nose...updated 5/26, my prediction was correct, see below :-))

This works fine; the JSON renders correctly in the browser...

def products = [] //ArrayList of Product objects from service       
def model = (products) ? [products:products] : [products:"No products found"] 
render model as JSON

..so why doesn't this shortened version without model work?

def products = []       
render ((products) ? [products:products] : [products:"No products found"]) as JSON

The resulting JSON from the above code is output as a single line of text, so I suspect it's not picking up as JSON, but it's parenthesized correctly, so what's the deal?

['products':[com.test.domain.Product : null, com.test.domain.Product...]

like image 972
raffian Avatar asked May 25 '13 21:05

raffian


People also ask

What is conditional rendering in react?

Conditional Rendering. In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending on the state of your application. Conditional rendering in React works the same way conditions work in JavaScript.

How do you render conditional expressions in JSX?

Conditional Rendering 1 Element Variables. You can use variables to store elements. ... 2 Inline If with Logical && Operator. You may embed expressions in JSX by wrapping them in curly braces. ... 3 Inline If-Else with Conditional Operator. ... 4 Preventing Component from Rendering. ...

How to conditionally render elements inline in JavaScript?

Another method for conditionally rendering elements inline is to use the JavaScript conditional operator condition ? true : false. In the example below, we use it to conditionally render a small block of text. It can also be used for larger expressions although it is less obvious what’s going on:

What is response rendering method?

To render different forms of responses from simple text responses, to view and templates. A multi-purpose method for rendering responses to the client which is best illustrated with a few examples! Warning - this method does not always support multiple parameters.


3 Answers

This is a normal behavior of render. When you provide arguments to render without braces like

render model as JSON

It makes an implicit adjustment setting up the content-type to text/json. But in the later case, you have unknowingly made the render to use the braces like [mark on the first brace after render makes render use the normal render()]

render ((products) ? [products:products] : [products:"No products found"]) as JSON.

In the above case, you have to pass in the named parameters to render mentioning the contentType, text or model, status etc. So in order to render the inline control logic as JSON in browser/view you have to do like below:

render(contentType: "application/json", text: [products: (products ?: "No products found")] as JSON)

You can also use content-type as text/json. I prefer application/json.

UPDATE
Alternative Simplest Way:
render([products: (products ?: "No products found")] as JSON)

like image 69
dmahapatro Avatar answered Oct 13 '22 05:10

dmahapatro


The essence of your problem here is that the groovy compiler interprets

render x as JSON

to mean

render (x as JSON)

but it interprets

render (x) as JSON

to mean

(render x) as JSON

If a method name (in this case render) is followed immediately by an opening parenthesis, then only code up to the matching closing parenthesis is considered to be the argument list. This is why you need an extra set of parentheses to say

render ((x) as JSON)
like image 3
Ian Roberts Avatar answered Oct 13 '22 06:10

Ian Roberts


Don't know the reason. Try to use like this:

render(contentType: 'text/json') {[
    'products': products ? : "No products found"
]}
like image 1
Mr. Cat Avatar answered Oct 13 '22 05:10

Mr. Cat