2014-02-10 33 views
0

我有一个包含在类中的3d库,我使用的是我想用杰克森序列化为json的类。杰克逊忽略子类的属性是3:D方

我想杰克逊的对象A,但忽略类C中的字段,无法更改类B和C上的源代码,这可能吗?

class A { 

    B b; 
} 

class B { 
    C c; 

} 



class C { 
int field; 
} 

回答

1

我相信你可以通过使用自定义序列化器来实现解决方案。

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

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

谢谢你工作完美:=) – Trind