JSON 渲染带有子对象的对象时忽略编组器
JSON marshaller ignored when rendering object with child objects
当前 运行 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 编组器并在 BootStrap 中注册了它:JSON.registerObjectMarshaller(new StepMarshaller())
StepMarshaller 仅呈现重要字段。当我直接调用它时,它工作得很好:
render Step.get(1) as JSON
产生正确的 JSON ({"docID":"The_ID","stepRef":"1"}
)
但是,将父表单呈现为 JSON 仍然是 returns ID 列表(默认行为)。我错过了什么导致 Marshaller 被子对象忽略?
您可以使用默认的深度 json 转换器:
JSON.use('deep'){
render (form as JSON)
}
当前 运行 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 编组器并在 BootStrap 中注册了它:JSON.registerObjectMarshaller(new StepMarshaller())
StepMarshaller 仅呈现重要字段。当我直接调用它时,它工作得很好:
render Step.get(1) as JSON
产生正确的 JSON ({"docID":"The_ID","stepRef":"1"}
)
但是,将父表单呈现为 JSON 仍然是 returns ID 列表(默认行为)。我错过了什么导致 Marshaller 被子对象忽略?
您可以使用默认的深度 json 转换器:
JSON.use('deep'){
render (form as JSON)
}