Wednesday, August 26, 2009

Another way to include version property in your JSON

For those want to include domain class version property in JSON, you can get help from GRAILS-4530: Chaning your grails version or Using the way provided by Sigi(See the description).
But I found another way to get that. Instead of using a new DomainClassMarshaller, I just retrieve the DomainClassMarshaller from the ConverterConfiguration. Here is my code:
import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationHolder as CCH
import org.codehaus.groovy.grails.web.converters.marshaller.json.DomainClassMarshaller as DCM
import grails.converters.JSON

class BootStrap {

def init = { servletContext ->

def cfg= CCH.getConverterConfiguration(JSON.class)
def marshellers= cfg.orderedObjectMarshallers
for(m in marshellers){
if(DCM.class.isAssignableFrom(m.class)){
m.includeVersion= true
break
}
}
}
def destroy = {
}
}
Maybe it is not as simple as Sigi's solution, but I think it is the complement to this issue.

No comments: