tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > SOJO > Map to Bean Conversion

Map to Bean Conversion 

SOJO (Simplified Old Java Objects) 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 map to bean.

File Name  :  
com/bethecoder/tutorials/sojo/common/Student.java 
   
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 + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/sojo/tests/MapToBeanConversion.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.sojo.tests;

import java.util.HashMap;
import java.util.Map;

import net.sf.sojo.core.Converter;
import net.sf.sojo.core.conversion.IterateableMap2BeanConversion;

import com.bethecoder.tutorials.sojo.common.Student;

public class MapToBeanConversion {

  /**
   @param args
   @throws IllegalAccessException 
   @throws InstantiationException 
   */
  public static void main(String[] argsthrows InstantiationException, IllegalAccessException {

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("class", Student.class);
    map.put("name""Sriram");
    map.put("hobby""Chess");
    map.put("age"2);
    
    Converter converter = new Converter();
    converter.addConversion(new IterateableMap2BeanConversion());
    
    Object result = converter.convert(map);
    System.out.println(result);
  }

}
   

It gives the following output,
Student[name = Sriram, age = 2, hobby = Chess]



 
  


  
bl  br