是的,你可以很容易地通過使用名爲marshallers實現它。即註冊相同的命名空間的編組BootStrap.groovy中:
JSON.createNamedConfig("foo") {
it.registerObjectMarshaller(new CustomDataMarshaller)
}
的Marshaller代碼:只有您想即使用它的任何具體的實例或從結果中返回
class CustomDataMarshaller implements ObjectMarshaller<JSON> {
@Override
boolean supports(Object object) {
return object instanceof Currency // Or directly BasicDbObject if you want to marshall the whole MongoDb result
}
@Override
void marshalObject(Object object, JSON converter) throws ConverterException {
// Convert here
}
}
現在使用這個編組MongoDB調用:
class MyController {
def test() {
def data // Any data as you want to marshal
JSON.use("foo") {
respond(data)
// OR
// render(data as JSON)
}
}
}