使用子对象渲染对象时,JSON marshaller被忽略

时间:2018-01-09 13:38:13

标签: json grails grails3

目前正在运行grails 3.2.10

鉴于以下域名

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

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

尝试查看表单render Form as JSON的JSON表示时,步骤列表将呈现为ID列表,例如"steps":[{"id":15},{"id":16},{"id":17},{"id":18}]

好的,所以我设置了一个JSON marshaller并在BootStrap中注册了它:JSON.registerObjectMarshaller(new StepMarshaller())

StepMarshaller只是渲染重要的字段。当我直接调用它时,它可以正常工作:

render Step.get(1) as JSON

产生正确的JSON({"docID":"The_ID","stepRef":"1"}

但是,将父Form呈现为JSON仍会返回ID列表(默认行为)。我错过了什么导致Marshaller被子对象忽略了?

1 个答案:

答案 0 :(得分:2)

您可以使用默认的深json转换器:

JSON.use('deep'){ 
   render (form as JSON) 
} 
相关问题