Thursday 21 April 2016

Genson: Convert json to map

Step 1: Get Genson instance
Genson genson = new Genson();

Step 2: Use following snippet to convert json to map.

public static <K, V> Map<K, V> getMap(String json,GenericType<Map<K, V>> map) {
         return genson.deserialize(json, map);
}


Map<Integer, Employee> tempMap = JSONUtil.getMap(json, new GenericType<Map<Integer, Employee>>(){});
public class Employee {
 private String id;
 private String firstName;
 private String lastName;
 private String password;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=")
    .append(firstName).append(", lastName=").append(lastName)
    .append(", password=").append(password).append("]");
  return builder.toString();
 }

}

import java.util.List;
import java.util.Map;

import com.owlike.genson.GenericType;
import com.owlike.genson.Genson;

public class JSONUtil {
 private static Genson genson = new Genson();

 public static String getJson(Object obj) {
  return genson.serialize(obj);
 }

 public static <T> T deserializeJson(String json, Class<T> clazz) {
  return genson.deserialize(json, clazz);
 }

 public static <T> List<T> getListOfObjects(String json,
   GenericType<List<T>> list) {
  return genson.deserialize(json, list);
 }

 public static <K, V> Map<K, V> getMap(String json,
   GenericType<Map<K, V>> map) {
  return genson.deserialize(json, map);
 }
}

import java.util.HashMap;
import java.util.Map;
import com.owlike.genson.GenericType;

public class Test {

 public static void main(String args[]) {
  Employee emp1 = new Employee();
  Employee emp2 = new Employee();
  Employee emp3 = new Employee();

  emp1.setId("E432156");
  emp1.setFirstName("Hari krishna");
  emp1.setLastName("Gurram");
  emp1.setPassword("Password123");

  emp2.setId("32452");
  emp2.setFirstName("Senthi");
  emp2.setLastName("Kumaran");

  emp3.setId("Prason");
  emp3.setPassword("password");
  emp3.setId("1232");

  Map<Integer, Employee> map = new HashMap<>();

  map.put(1, emp1);
  map.put(2, emp2);
  map.put(3, emp3);

  String json = JSONUtil.getJson(map);

  Map<Integer, Employee> tempMap = JSONUtil.getMap(json,
    new GenericType<Map<Integer, Employee>>() {
    });

  System.out.println("After deserialization");
  System.out.println(tempMap);

 }
}


Output
After deserialization
{1=Employee [id=E432156, firstName=Hari krishna, lastName=Gurram, password=Password123], 2=Employee [id=32452, firstName=Senthi, lastName=Kumaran, password=null], 3=Employee [id=1232, firstName=null, lastName=null, password=password]}



Previous                                                 Next                                                 Home

No comments:

Post a Comment