Complex Bean to Map Conversion
SOJO (S implified O ld J ava O bjects) is a Java framework
which converts object graph into a specific structure or representation.
This requires the library sojo-1.0.0.jar to be in classpath.
The following example shows using Converter class.
It converts the given bean to map.
package com.bethecoder.tutorials.sojo.common;
public class Student {
private String name;
private int age;
private String hobby;
public Student () {
}
public Student ( String name, int age, String hobby ) {
super () ;
this .name = name;
this .age = age;
this .hobby = hobby;
}
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 String getHobby () {
return hobby;
}
public void setHobby ( String hobby ) {
this .hobby = hobby;
}
public String toString () {
return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]" ;
}
}
package com.bethecoder.tutorials.sojo.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ComplexBean {
private List<Student> studentList = new ArrayList<Student> () ;
private Map<String, List<Student>> subjectStudentMap = new HashMap<String, List<Student>> () ;
public List<Student> getStudentList () {
return studentList;
}
public void setStudentList ( List<Student> studentList ) {
this .studentList = studentList;
}
public Map<String, List<Student>> getSubjectStudentMap () {
return subjectStudentMap;
}
public void setSubjectStudentMap ( Map<String, List<Student>> subjectStudentMap ) {
this .subjectStudentMap = subjectStudentMap;
}
}
package com.bethecoder.tutorials.sojo.tests;
import java.io.IOException;
import java.util.Arrays;
import net.sf.sojo.core.Converter;
import net.sf.sojo.core.conversion.ComplexBean2MapConversion;
import com.bethecoder.tutorials.sojo.common.ComplexBean;
import com.bethecoder.tutorials.sojo.common.Student;
public class ComplexBeanToMapConversion {
/**
* @param args
*/
public static void main ( String [] args ) throws IOException {
Student student = new Student ( "Sriram" , 2 , "Chess" ) ;
Student student2 = new Student ( "Sudhakar" , 29 , "Painting" ) ;
Student student3 = new Student ( "Charan" , 18 , "Reading books" ) ;
ComplexBean complexBean = new ComplexBean () ;
complexBean.getStudentList () .addAll ( Arrays.asList ( student, student2, student3 )) ;
complexBean.getSubjectStudentMap () .put ( "MATHS" ,
Arrays.asList ( student, student2, student3 )) ;
complexBean.getSubjectStudentMap () .put ( "PHYSICS" ,
Arrays.asList ( student, student2 )) ;
Converter converter = new Converter () ;
converter.addConversion ( new ComplexBean2MapConversion ()) ;
Object result = converter.convert ( complexBean ) ;
System.out.println ( result ) ;
}
}
It gives the following output,
{
~unique-id~=0,
class=com.bethecoder.tutorials.sojo.common.ComplexBean,
studentList=[Student[name = Sriram, age = 2, hobby = Chess],
Student[name = Sudhakar, age = 29, hobby = Painting],
Student[name = Charan, age = 18, hobby = Reading books]],
subjectStudentMap={
MATHS=[Student[name = Sriram, age = 2, hobby = Chess],
Student[name = Sudhakar, age = 29, hobby = Painting],
Student[name = Charan, age = 18, hobby = Reading books]],
PHYSICS=[Student[name = Sriram, age = 2, hobby = Chess],
Student[name = Sudhakar, age = 29, hobby = Painting]]
}
}