Thursday, June 10, 2010

A Simple Json Utility Class Using Gson

In my current project we planned to go with Gson(Google's json manipulating library) to handle out json manipulation within our application. Hence i wrote this small utility class to ease the use of Gson within the project.



import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* This class is used as a Json utility. The base functionality comes from the Gson<br>
* package from Google. It is made generic due to the fact that many clients may use<br>
* this class at any given moment.
*
* @author dinuka
* @param <T>
*/
public class Jsonutil {

/**
* Null serialize is used because else Gson will ignore all null fields.
*/
private static Gson gson = new GsonBuilder().serializeNulls().create();

/**
* Made private because all methods are static and hence do not need
* object instantiation
*/
private Jsonutil() {}
/**
* To Json Converter using Goolge's Gson Package<br>
* this method converts a simple object to a json string<br>
*
* @param obj
* @return a json string
*/
public static <T> String toJsonObj(T obj) {
return gson.toJson(obj);
}

/**
* Converts a collection of objects using Google's Gson Package
*
* @param objCol
* @return a json string array
*/
public static <T> String toJsonList(List<T> objCol) {
return gson.toJson(objCol);
}

/**
* Returns the specific object given the Json String
* @param <T>
* @param jsonString
* @param obj
* @return a specific object as defined by the user calling the method
*/
public static <T> T fromJsonToObj(String jsonString,Class<T>obj) {
return gson.fromJson(jsonString, obj);
}

/**
* Returns a list of specified object from the given json array
* @param <T>
* @param jsonString
* @param t the type defined by the user
* @return a list of specified objects as given in the json array
*/
public static <T> List<T> fromJsonToList(String jsonString,Type t){
return gson.fromJson(jsonString, t);
}

}


Typical usage of the given utility class is as follow;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.reflect.TypeToken;


public class Test {


public static void main(String[] args) {
Person p = new Person();
p.setFirstName("xxx");
p.setLastName("yyy");

List<Person>pList = new ArrayList<Person>();
pList.add(p);

String jsonStr = JsonUtil.toJsonList(pList);

System.out.println("Person List as Json Array = "+jsonStr);

Type collectionType = new TypeToken<List<Person>>(){}.getType();
List<Person>personList = JsonUtil.fromJsonToList(jsonStr,collectionType);
System.out.println("Person List from json String array = "+personList);

String personJsonStr = JsonUtil.toJsonObj(p);
System.out.println("Json String from person = "+personJsonStr);

Person p1 = JsonUtil.fromJsonToObj(personJsonStr, Person.class);
System.out.println("Person from json String = "+p1);

}



}

class Person{
private String firstName;
private String lastName;
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + "]";
}


}



Thats it guys. If you have any queries or possible enhancements you can see, please do leave a comment.

Thursday, June 3, 2010

How to solve Classpath nightmare in java

Have you ever come across a situation where you modify a class and when you run it you do not see the changes you just committed? Most probably in most cases the issue lies with an older .class file being loaded in from another jar which overrides your current jar. This is definitely a debugging night mare.

Fortunately in java you can find out from which location your class file is loaded from. This article provides the info you need to find out on how to do it the eclipse way or injecting it directly to your code.


Enjoy!!!

Wednesday, June 2, 2010

Free icons for your project

Check this out. Gives a wide variety of free icons you can use in your projects.

Cheers

Monday, May 31, 2010

jLayout provides Swing like Layouts in JS

Found a javascript library called jLayout which provides layout capabilities that is similar to Java's Swing library. Most of the names used are evn similar. Pretty cool i must say. Hoping to try it out in the coming days. Check it out....

Friday, May 28, 2010

Online XML to XSD converter

Found an online tool which can generator xsd from a given sample XML doc. Can be found here.

Friday, May 21, 2010

Spring - How to use Component and Autowired

Just put together a sample which made me understand how to use the above mentioned annotations defined by Spring hence thought i should blog about the same.

First lets take a look at the two classes that are exposed as spring beans.





package chapter3;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("calculator")
public class Calculator {
private Adder adder;

@Autowired
public void setAdder(Adder adder) {
this.adder = adder;
}

public void makeAnOperation(){
int r1 = adder.add(1,2);
System.out.println("r1 = " + r1);
}
}




package chapter3;

import org.springframework.stereotype.Component;

@Component("adder")
public class Adder {
public int add(int a, int b){
return a + b;
}
}

Few pointers to note here are as follows;

  • By annotation the class with @Component you tell the Spring container that these beans are needed to be instantiated and managed by the Spring container. This reduces XML configurations required because you do not need to manually edit the XML file entering bean definitions to each and every bean.
  • The @Autowired element binds an instance of the Adder class when the Spring container scans the Calculator class.
Next we look at the application context XML


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="chapter3"/>
<context:annotation-config/>

</beans>


And finally to run the sample following is the main class;



package chapter3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ComponentCheck {

public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"chapter3AppXML.xml");
Calculator calculator = (Calculator) appContext.getBean("calculator");
calculator.makeAnOperation();

}

}

For all your CSS needs

Check this out. Has so many tutorials and designs that you can use or just learn the true power of CSS. Great site.