I'm migrating a Grails 2.2.5 app to Grails 4.0.2, and I have it working OK but now want to start taking advantage of new features that came in with Grails 3 and 4, such as improvements in the area of JSON responses. I've been having a play with JSON Views and the use of respond, and have some test methods working. Still struggling in some areas, though. For example, I have in multiple places in controllers code which produces a Map, which I then return via render map as JSON. This works fine, with the JSON representation of the objects in the map being determined by customer marshallers using JSON.registerObjectMarshaller(MyClass).
My understanding is that the use of respond and JSON Views for such things is now being encouraged, in preference to the render xxx as JSON approach (please correct me if I'm wrong). So two questions arise from this:
For my 'weekSummary' method in my SaleController, for this to work I presumably need to create a 'weekSummary.gson' file in /sale/views. What should that gson file look like? I've tried with e.g.
model { Map map } json map
but this just renders null. How do I handle maps in JSON views?
- What advantages does this give me over
render xxx as JSON, given that it is a little more cumbersome (having to create a new GSON file)?