Gson 進行 json 數據的相關的操作


基於Gson的工具類:

/**
 * 
 * @author Kenny-Tang
 *
 */
public class JSONTool{
	private JSONTool(){}
	
	/**
	 * 將對象轉化成json串。
	 * @param obj
	 * @return
	 */
	public static String toJson(Object obj){
		Gson gson = getGsonInstance(false, null);
		return gson.toJson(obj);
	}
	
	/**
	 * 將json轉換爲對象。
	 * @param classOfT
	 * @param json
	 * @return
	 */
	public static <T> T fromJson(String json, Class<T> classOfT){
		Gson gson = getGsonInstance(false, null);
		return gson.fromJson(json, classOfT);
	}

	/**
	 * 將對象轉換爲json串。
	 * @param obj
	 * @param serializeNulls 是否對空值進行序列化
	 * @param dateFormatPattern 日期序列化格式,如果爲null則使用  "yyyy-MM-dd"。
	 * @return
	 */
	public static String toJson(Object obj, boolean serializeNulls, String dateFormatPattern){
		Gson gson = getGsonInstance(serializeNulls, dateFormatPattern);
		return gson.toJson(obj);
	}
	
	
	/**
	 * Gson 默認對於空值的屬性,在轉換成json對象的時候是不進行序列化的,只有顯示的聲明對空值進行序列化的時候纔會對空值屬性進行處理。<br />
	 * 
	 * @param serializeNulls 是否對空值屬性
	 * @param dateFormatPattern 對於日期的序列化格式
	 * @return
	 */
	public static Gson getGsonInstance(boolean serializeNulls, String dateFormatPattern){
		GsonBuilder builder = new GsonBuilder();
		
		if(serializeNulls) builder.serializeNulls() ;

		if(dateFormatPattern == null || "".equals(dateFormatPattern)){
			dateFormatPattern = "yyyy-MM-dd";
		} 
		builder.setDateFormat(dateFormatPattern);
		
		return builder.create() ;
	}
	
	/**
	 * 指定日期序列化格式。
	 * @param dateFormatPattern
	 * @return
	 */
	public static Gson getGsonInstance(String dateFormatPattern){
		return getGsonInstance(false , dateFormatPattern);
	}
	
	/**
	 * 獲取默認對空值屬性進行序列化,日期序列化格式爲 "yyyy-MM-dd" 的{@linkplain Gson}對象。
	 * @return
	 */
	public static Gson getGsonInstance(){
		return getGsonInstance(false , null);
	}
	
	/**
	 * Gson 默認對於空值的屬性,在轉換成json對象的時候是不進行序列化的,只有顯示的聲明對空值進行序列化的時候纔會對空值屬性進行處理。<br />
	 * @param serializeNulls 是否對空值屬性
	 * @param dateFormatPattern 對於日期的序列化格式
	 * @return
	 */
	public static GsonBuilder getGsonBuilderInstance(boolean serializeNulls, String dateFormatPattern){
		GsonBuilder builder = new GsonBuilder();
		
		if(serializeNulls) builder.serializeNulls() ;

		if(dateFormatPattern == null || "".equals(dateFormatPattern)){
			dateFormatPattern = "yyyy-MM-dd";
		} 
		builder.setDateFormat(dateFormatPattern);
		
		return builder ;
	}
	
}


單元測試類:

public class JSONToolTest extends TestCase{
	private static Logger logger = LoggerFactory.getLogger(JSONToolTest.class.getName());
	
	public void testFromJson(){
		Date birthday = new Date();
		String jsonString = "{\"id\":12,\"name\":\"張三\",\"age\":21,\"gender\":\"M\",\"birthday\":\""+new SimpleDateFormat("yyyy-MM-dd").format(birthday)+"\"}" ;
		Person jsonPerson = JSONTool.fromJson(jsonString, Person.class) ;
		logger.debug("person info : {}" , jsonPerson);
		Person person = new Person();
		person.setAge(21);
		person.setGender(Person.MALE);
		person.setId(12l);
		person.setName("張三");
		person.setBirthday(birthday);
		assertTrue(person.getId().equals(jsonPerson.getId()));
		assertTrue(person.getGender().equals(jsonPerson.getGender()));
		assertTrue(person.getName().equals(jsonPerson.getName()));
	}
	
	public void testToJson(){
		Date birthday = new Date();
		String jsonString = "{\"id\":12,\"name\":\"張三\",\"age\":21,\"gender\":\"M\",\"birthday\":\""+new SimpleDateFormat("yyyy-MM-dd").format(birthday)+"\"}" ;
		Person person = new Person();
		person.setAge(21);
		person.setGender(Person.MALE);
		person.setId(12l);
		person.setName("張三");
		person.setBirthday(birthday);
		String gsonString = JSONTool.toJson(person);
		logger.debug("Person to JSON is : {}",gsonString);
		assertTrue(jsonString.equals(gsonString));
	}
	
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章