环境说明:
JDK 1.5以上,GSon 2.2.2。
阅读对象:
假设读者已经了解Java
注解的使用以及如何创建一个指定注解类型,读者对
JSON、GSON有基本了解,对Java
反射机制有所了解。
需求概述:开发一个日志记录、显示功能,要求当对指定的实体类进行新增、删除、修改操作时,将变化的内容记录到日志表中。
格式要求:
字段中文名:字段值,比如:用户名:张三。
首先新建两个实体类:
package com.gson.tutorial;
import java.util.Date;
public class MySuperEntity {
	private long id;
	private String name;
	private Date date;
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
}
package com.gson.tutorial;
import java.util.Date;
import com.google.gson.annotations.Expose;
import com.gson.tutorial.annotation.FieldComment;
public class MyEntity {
	@Expose
	@FieldComment("用户ID")
	private long id;
	@Expose
	@FieldComment("用户名")
	private String userName;
	private String address;
	private Date date;
	private MySuperEntity entity;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public MySuperEntity getEntity() {
		return entity;
	}
	public void setEntity(MySuperEntity entity) {
		this.entity = entity;
	}
}
上面的类中,字段上的@Expose是GSON的注解,表示该字段在进行
JSON转换时保留,也就是没有该注解的字段将在进行JSON转换时被忽略。
@FieldComment注解是一个
自定义的注解类型,用于填写字段的中文说明,该自定义注解的源码为:
package com.gson.tutorial.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldComment {
	String value();
}
下面写一个测试用例:
package com.gson.tutorial;
import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		
		MyEntity entity = new MyEntity();
		entity.setId(123456L);
		entity.setUserName("Test User");
		entity.setAddress("金茂中路");
		entity.setDate(new Date());
		
		String json = gson.toJson(entity);
		System.out.println("Using Expose:" + json);
		
		gson = new Gson();
		json = gson.toJson(entity);
		System.out.println("Unusing Expose:" + json);
	}
}
输出结果为:
Using Expose:{"id":123456,"userName":"Test User"}
Unusing Expose:{"id":123456,"userName":"Test User","address":"金茂中路","date":"Sep 23, 2012 11:33:42 PM"}
可以看到,如果使用:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
方式进行JSON转换,那么被@Expose注解的字段将被转成JSON,其他字段将被忽略。而传统的GSON转换方式则不会理会@Expose注解。
接着进行格式转换输出:
package com.gson.tutorial.annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.gson.tutorial.MyEntity;
public class Main {
	/**
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String[] args) throws ClassNotFoundException,
			IllegalAccessException, InvocationTargetException {
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		MyEntity entity = new MyEntity();
		entity.setId(123456L);
		entity.setUserName("Test User");
		entity.setAddress("金茂中路");
		entity.setDate(new Date());
		String json = gson.toJson(entity);
		System.out.println("Using Expose:" + json);
		// -------------------------------------
		String className = "com.gson.tutorial.MyEntity";
		Class classDef = Class.forName(className);
		Object logObj = gson.fromJson(json, classDef);
		Field[] fields = classDef.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			boolean isPresent = fields[i]
					.isAnnotationPresent(FieldComment.class);
			if (isPresent) {
				// 取注解中的文字说明
				FieldComment comment = fields[i]
						.getAnnotation(FieldComment.class);
				String fieldComment = comment.value();
				// 取对象中字段的值
				fields[i].setAccessible(true); // 设置为可访问private字段
				Object fieldValue = fields[i].get(logObj);
				String content = String.format("%s:%s", fieldComment,
						fieldValue);
				System.out.println(content);
			}
		}
	}
}
输出结果:
Using Expose:{"id":123456,"userName":"Test User"}
用户ID:123456
用户名:Test User