как зарегистрировать собственный маршаллер json в grails

Я пытаюсь зарегистрировать пользовательский json-маршаллер, подобный этому

 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
            }

, но я не уверен, используется ли мой пользовательский сериализатор, потому что при отладке управление никогда не переходит к marshalObjectфункции моего пользовательского сериализатора

Мой собственный сериализатор выглядит следующим образом

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);
        }
    }


}

Можно ли отлаживать сериализатор? Если нет, то как я могу исключить свойство из сериализации? Есть какое-то свойство, которое выдает исключение во время сериализации.

8
задан sashkello 13 September 2013 в 14:36
поделиться