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 

at com.fasterxml.jackson.databind.ser.impl.WritableObjectId.writeAsField(
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(

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;


    @JsonIdentityInfo(property = REFID_PROPERTY, generator = 
    public static class Data extends AbstractSkippable {

        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;

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

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


    public static class SkippableSerializerModifier extends 
BeanSerializerModifier {

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


    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();
        String xml = mapper.writeValueAsString(data);


Any idea?


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 post to this group, send email to
For more options, visit

Reply via email to