2

I have a the class structure as below:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")

    @JsonSubTypes({
            @JsonSubTypes.Type(name = "testA", value = TestA.class),
            @JsonSubTypes.Type(name = "testB", value = TestB.class),
            @JsonSubTypes.Type(name = "testC", value = TestC.class)
    })
    public abstract class Test {

    }


    public class TestA extends Test {
        private String firstName;
        private String secondName;
        private String surName;
    }

    public class TestB extends Test {
        private String adressLine1;
        private String adressLine2;
        private String adressLine3;
    }


    public class TestC extends Test {
        private String hobby1;
        private String hobby2;
        private String hobby3;
    }

The above classes are serialized into array of json elements, but when I de-serialise them back, i want the structure as below:

public class FlatStructure {

   private TestA testA;
   private TestB testB;
   private TestC testC;

   public void setTestA(TestA testA){
     this.testA = testA;   
} 

 public TestA getTestA(){
   return testA;
}
 .....getter and setter for testB and testC... 
} 

is it possible to convert the array of elements of type testA, testB and testC to properties of FlatStructure class?

Moh
  • 21
  • 1
  • 1
    Possible duplicate of [How to use Jackson to deserialise an array of objects](http://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects) – Abby Chau Yu Hoi Jul 22 '16 at 10:04
  • @Abby, thanks for your answer, however I am not trying to deserialise the array of json elements to List. I am trying to deserialise the array of json elements as properties of another object. Hence not duplicate. Please read my post again. – Moh Jul 23 '16 at 20:40

1 Answers1

0

You can add a constructor with the annotation @JsonCreator

, in each class Test

and another one in you flat Structure Class, after that you use an ObjectMapper to create your class FlatStructure

I would recommend to use the annotaions @JsonProperty also on your constructor

check this link

http://buraktas.com/convert-objects-to-from-json-by-jackson-example/

i think that

    public class TestA extends Test {
    .....
    @JsonCreator
    public TestA(@JsonProperty("firstName") String name,
                 @JsonProperty("secondName") String secondeName,                 
                 @JsonProperty("surName") String surName){
       this.firstName=name;
       this.secondeName=secondeName;
       this.surName=surName;

   }



   ... getter, setter .....
}

  public class TestB extends Test {
    .....
        @JsonCreator
    public TestB(@JsonProperty("adressLine1") String adressLine1,
                 @JsonProperty("adressLine2") String adressLine2,                 
                 @JsonProperty("adressLine3") String adressLine3){
       this.adressLine1=adressLine1;
       this.adressLine2=adressLine2;
       this.adressLine3=adressLine3;
   }



   ... getter, setter .....
}

    public class TestC extends Test {
    .....
        @JsonCreator
    public TestC(@JsonProperty("hobby1") String hobby1,
                 @JsonProperty("hobby2") String hobby2,                 
                 @JsonProperty("hobby3") String hobby3){
       this.hobby1=hobby1;
       this.hobby2=hobby2;
       this.hobby3=hobby3;
   }

   ... getter, setter .....
}


public class FlatStructure{
       private TestA testA;
       private TestB testB;
       private TestC testC;

      @JsonCreator
       public FlatStructure(@JsonProperty("testA") testA testa,
                            @JsonProperty("testB") testB testb,                 
                            @JsonProperty("testC") testC testc){
       this.testA =testa;
       this.testB =testb;
       this.testC =testc;
   }


   ... getter, setter .....
}

Should work properly with a mapper

Edit:

Custom JSON Deserialization with Jackson

http://www.baeldung.com/jackson-deserialization

Community
  • 1
  • 1
Koraxos
  • 418
  • 5
  • 13
  • thanks for your answers, but I am looking to write custom deserialiser. Any ideas? – Moh Jul 23 '16 at 20:42
  • Check my edits , I have find two links about custom deserialiser, i think this is what you want. – Koraxos Jul 25 '16 at 07:08