Sto cercando di registrare un marshaller JSON personalizzato come questocome registrare un JSON marshaller personalizzato in graal
JSON.createNamedConfig("dynamic",{
def m = new CustomJSONSerializer()
JSON.registerObjectMarshaller(Idf, 1, { instance, converter -> m.marshalObject(instance, converter) })
})
and then using it like this
JSON.use("dynamic"){
render inventionList as JSON
}
ma non sono sicuro se è in uso la mia serializzatore personalizzato perché quando sono il debug di controllo mai va a marshalObject
funzione della mia serializzatore personalizzato
mio serializzatore personalizzato è la seguente
import grails.converters.deep.JSON
import java.beans.PropertyDescriptor
import java.lang.reflect.Field
import java.lang.reflect.Method
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException
import org.codehaus.groovy.grails.web.converters.marshaller.json.GroovyBeanMarshaller
import org.codehaus.groovy.grails.web.json.JSONWriter
class CustomJSONSerializer extends GroovyBeanMarshaller{
public boolean supports(Object object) {
return object instanceof GroovyObject;
}
public void marshalObject(Object o, JSON json) throws ConverterException {
JSONWriter writer = json.getWriter();
println 'properties '+BeanUtils.getPropertyDescriptors(o.getClass())
for(PropertyDescriptor property:BeanUtils.getProperyDescriptors(o.getClass())){
println 'property '+property.getName()
}
try {
writer.object();
for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
String name = property.getName();
Method readMethod = property.getReadMethod();
if (readMethod != null && !(name.equals("metaClass")) && readMethod.getName()!='getSpringSecurityService') {
Object value = readMethod.invoke(o, (Object[]) null);
writer.key(name);
json.convertAnother(value);
}
}
for (Field field : o.getClass().getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
writer.key(field.getName());
json.convertAnother(field.get(o));
}
}
writer.endObject();
}
catch (ConverterException ce) {
throw ce;
}
catch (Exception e) {
throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
}
}
}
E 'possibile eseguire il debug le s erializer? Altrimenti, come posso escludere una proprietà dalla serializzazione? C'è una proprietà che genera un'eccezione durante la serializzazione.
ho risolto importando alcuni file mancanti come BeanUtils e modificatore –