Search code examples
jsongrailsgrails3

JSON marshaller ignored when rendering object with child objects


Currently running grails 3.2.10

Given the following domains

class Form {
  List<Step> steps
  static hasMany = [steps:Step]
}

class Step {
  Form form
  static belongsTo = [Form]
  String docID
  String stepRef
}

When trying to view the JSON representation of the form render Form as JSON, the list of Steps is rendered as a list of IDs, e.g., "steps":[{"id":15},{"id":16},{"id":17},{"id":18}]

Ok, so I set up a JSON marshaller and registered it in BootStrap: JSON.registerObjectMarshaller(new StepMarshaller())

The StepMarshaller just renders the the important fields. When I call this directly, it works just fine:

render Step.get(1) as JSON

yields the correct JSON ({"docID":"The_ID","stepRef":"1"})

However, rendering the parent Form as JSON still returns the list of IDs (the default behavior). What am I missing that's causing the Marshaller to be ignored for child objects?


Solution

  • You may use default deep json converter:

    JSON.use('deep'){ 
       render (form as JSON) 
    }