I have a objects filled with data. Now I want to create class with annotation that allows me to create CSV file with specified order of columns or any generic solution for it. My data representation is somewhat complex means its with list of objects.
For Example:
class User {
public String name;
public int age;
List<Relatives> relativeList;
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Relatives> getRelativeList() {
return relativeList;
}
public void setRelativeList(List<Relatives> relativeList) {
this.relativeList = relativeList;
}
}
class Relatives {
public int id;
public String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name= name;
}
public String getName() {
return name;
}
}
I want to create CSV file out of data filled in objects of this class.
For Example
User user = new User();
user.setName("Jackson");
user.setAge(32);
List<Relatives> relativeList = new ArrayList<Relatives>();
Relatives relatives = new Relatives();
relatives.setId(110);
relatives.setName("father");
relativeList.add(relatives);
relatives = new Relatives();
relatives.setId(111);
relativ.add(relatives);
relativeList.setName("mother");
user.setRelativeList(relativeList);
I want to create CSV text like.
"name";"age";"relatives id1";"relatives name1","relatives id2";"relatives name2"
"Jackson";"30";"110";"father";"111";"mother"
As described by Example, If i have 3 elements in relativeList than one more column with name "relatives id3" and "relatives name3" should be added. Which is element + sub-element means relatives + (id or name). Along with this i want to decide the order of columns in generated CSV means age can be settled as a first column before name.
It may possible that exact solution for this is not readily available but any near by solution is really appreciated and will be helpful.