Advertisement
Guest User

Jackson polymorphic serialization/deserialization

a guest
Nov 24th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.fasterxml.jackson.annotation.JsonSubTypes;
  2. import com.fasterxml.jackson.annotation.JsonTypeInfo;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.databind.SerializationFeature;
  5. import org.junit.Assert;
  6.  
  7. public class TestSerialization {
  8.  
  9.     @JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
  10.     @JsonSubTypes({ @JsonSubTypes.Type(value = AImpl1.class), @JsonSubTypes.Type(value = AImpl2.class)})
  11.     public static interface A {
  12.         public String foo();
  13.     }
  14.  
  15.     public static class AImpl1 implements A {
  16.         String s;
  17.  
  18.         public AImpl1(){};
  19.  
  20.         public AImpl1(String s){
  21.             this.s = s;
  22.         }
  23.         public String getS() {
  24.             return s;
  25.         }
  26.         public void setS(String s) {
  27.             this.s = s;
  28.         }
  29.  
  30.         public String foo() {
  31.             return s;
  32.         }
  33.         @Override public boolean equals(Object o) {
  34.             if (this == o)
  35.                 return true;
  36.             if (o == null || getClass() != o.getClass())
  37.                 return false;
  38.             AImpl1 aImpl1 = (AImpl1) o;
  39.             return !(s != null ? !s.equals(aImpl1.s) : aImpl1.s != null);
  40.  
  41.         }
  42.     }
  43.  
  44.     public static class AImpl2 implements A {
  45.         int i;
  46.  
  47.         public AImpl2(){};
  48.  
  49.         public AImpl2(int i) {
  50.             this.i = i;
  51.         }
  52.         public int getI() {
  53.             return i;
  54.         }
  55.         public void setI(int i) {
  56.             this.i = i;
  57.         }
  58.         public String foo() {
  59.             return Integer.toString(i);
  60.         }
  61.         @Override public boolean equals(Object o) {
  62.             if (this == o)
  63.                 return true;
  64.             if (o == null || getClass() != o.getClass())
  65.                 return false;
  66.  
  67.             AImpl2 aImpl2 = (AImpl2) o;
  68.  
  69.             return i == aImpl2.i;
  70.  
  71.         }
  72.     }
  73.  
  74.     @JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
  75.     @JsonSubTypes({ @JsonSubTypes.Type(value = BImpl.class)})
  76.     public static interface B {
  77.     }
  78.  
  79.     public static class BImpl implements B {
  80.         A a;
  81.  
  82.         public BImpl(){};
  83.  
  84.         public BImpl(A a) {
  85.             this.a = a;
  86.         }
  87.         public A getA() {
  88.             return a;
  89.         }
  90.         public void setA(A a) {
  91.             this.a = a;
  92.         }
  93.         @Override public boolean equals(Object o) {
  94.             if (this == o)
  95.                 return true;
  96.             if (o == null || getClass() != o.getClass())
  97.                 return false;
  98.  
  99.             BImpl b = (BImpl) o;
  100.  
  101.             return !(a != null ? !a.equals(b.a) : b.a != null);
  102.  
  103.         }
  104.     }
  105.  
  106.     @org.junit.Test
  107.     public void testSerialisation() throws Exception {
  108.         ObjectMapper objectMapper = new ObjectMapper();
  109.         objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  110.         A a1 = new AImpl1("ggfd");
  111.         A a2 = new AImpl2(5);
  112.         B b1 = new BImpl(a1);
  113.         B b2 = new BImpl(a2);
  114.         String s1 = objectMapper.writeValueAsString(b1);
  115.         String s2 = objectMapper.writeValueAsString(b2);
  116.         B serializedB1 = objectMapper.readValue(s1, B.class);
  117.         B serializedB2 = objectMapper.readValue(s2, B.class);
  118.         Assert.assertEquals(b1, serializedB1);
  119.         Assert.assertEquals(b2, serializedB2);
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement