Using 2.7.4 I tried to use a BeanSerializerModifier to avoid serialization 
of beans meeting some condition.
This works fine. But using also identity references I run into a 
NullPointerException:

at com.fasterxml.jackson.databind.ser.impl.WritableObjectId.writeAsField(
WritableObjectId.java:64)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.
_serializeWithObjectId(BeanSerializerBase.java:596)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(
BeanSerializer.java:148)


Code to reproduce:

public class SerializationTest {

    private static final String REFID_PROPERTY = "ref-id";

    public static abstract class AbstractSkippable {

        private String someData;

        public abstract boolean hasData();

        public String getSomeData() {
            return someData;
        }

        public void setSomeData(String someData) {
            this.someData = someData;
        }

    }

    @JsonIdentityReference
    @JsonIdentityInfo(property = REFID_PROPERTY, generator = 
ObjectIdGenerators.UUIDGenerator.class)
    public static class Data extends AbstractSkippable {

        @Override
        public boolean hasData() {
            return true;
        }

    }

    public static class MyData {

        private Data data1;
        private Data data2;

        public Data getData1() {
            return data1;
        }

        public void setData1(Data data1) {
            this.data1 = data1;
        }

        public Data getData2() {
            return data2;
        }

        public void setData2(Data data2) {
            this.data2 = data2;
        }

    }

    public static class AbstractSkippableSerializer extends JsonSerializer<
AbstractSkippable> {

        private final JsonSerializer<Object> defaultSerializer;

        public AbstractSkippableSerializer(JsonSerializer<Object> 
defaultSerializer) {
            if (defaultSerializer == null) {
                throw new IllegalArgumentException("defaultSerializer must 
not be null");
            }
            this.defaultSerializer = defaultSerializer;
        }

        @Override
        public void serialize(AbstractSkippable value, JsonGenerator gen, 
SerializerProvider serializers) throws IOException {
            defaultSerializer.serialize(value, gen, serializers);
        }

        @Override
        public boolean isEmpty(SerializerProvider provider, 
AbstractSkippable value) {
            return value == null || !value.hasData();
        }

    }

    public static class SkippableSerializerModifier extends 
BeanSerializerModifier {

        @Override
        public JsonSerializer<?> modifySerializer(SerializationConfig config
, BeanDescription beanDesc, JsonSerializer<?> serializer) {
            Class<?> beanClass = beanDesc.getBeanClass();
            if (AbstractSkippable.class.isAssignableFrom(beanClass)) {
                @SuppressWarnings("unchecked")
                JsonSerializer<Object> defaultSerializer = (JsonSerializer<
Object>) serializer;
                return new AbstractSkippableSerializer(defaultSerializer);
            }
            return serializer;
        }

    }

    @Test
    public void test() throws JsonProcessingException {
        JacksonXmlModule mod = new JacksonXmlModule();
        mod.setSerializerModifier(new SkippableSerializerModifier());
        XmlMapper mapper = new XmlMapper(mod);
        MyData myData = new MyData();
        Data data = new Data();
        data.setSomeData("data");
        myData.setData1(data);
        myData.setData2(data);
        String xml = mapper.writeValueAsString(data);
        System.out.println(xml);
    }

}


Any idea?

--Heiko

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to