杰克逊忽略了3:d派对的子类的属性 [英] Jackson ignore attribute on sub class that is 3:d party

查看:75
本文介绍了杰克逊忽略了3:d派对的子类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3d库,该类包含在我要使用jackson序列化为json的类中.

I have a 3d library that is included in a class i uses that i want to serialize to json with jackson.

我想对对象A进行装卸,但是在无法更改类B和C的源代码的情况下忽略了类C中的字段,这可能吗?

I want to jackson the object A, but ignore the field in Class C without being able to change the source code on Class B and C , is this possible?

class A {

   B b;
}

class B {
   C c;

}



class C {
 int field; 
}

推荐答案

我相信您可以通过使用自定义序列化程序来实现解决方案.

I believe you can achieve a solution by using custom serializers.

您可以通过ObjectMapper添加自定义序列化程序.我在下面创建了一个小单元测试,演示了如何实现它:

You can add custom serializers through the ObjectMapper. I have created a small unit test below that demonstrates how it can be achieved:

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.module.SimpleModule;
import org.junit.Test;
import java.io.IOException;

public class JacksonSerializerTest {

    @Test
    public void test() throws Exception {
        C c = new C("initially lowercase string in c");
        B b = new B(c);
        A a = new A(b);

        SimpleModule module = new SimpleModule("MyCustomModule", new Version(1, 0, 0, null));
        module.addSerializer(new CustomSerializerForC());

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(module);

        String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(a);
        System.out.println(pretty);
    }

    public class A {
        private B b;

        public A(B b) {
            this.b = b;
        }

        public B getB() {
            return b;
        }

        public void setB(B b) {
            this.b = b;
        }
    }

    public class B {
        private C c;

        public B(C c) {
            this.c = c;
        }

        public C getC() {
            return c;
        }

        public void setC(C c) {
            this.c = c;
        }
    }

    public class C {
        private String value;

        public C(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    public class CustomSerializerForC extends JsonSerializer<C> {

        @Override
        public Class<C> handledType() {
            return C.class;
        }

        @Override
        public void serialize(C c, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
            String upperCase = c.getValue().toUpperCase();
            jsonGenerator.writeString(upperCase);
        }
    }
}

这篇关于杰克逊忽略了3:d派对的子类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆