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

前端 未结 4 723
不知归路
不知归路 2020-12-11 06:27

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

4条回答
  •  天命终不由人
    2020-12-11 06:53

    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)
    

提交回复
热议问题