Gson TypeAdapter用法

Gson TypeAdapter用法

实例:写一个TypeAdapter,注册TypeAdapter来处理Enum类型.

 

实体类:

public enum Color {
	RED("红色",1),GREEN("绿色",2),BLANK("白色",3), YELLO("黄色", 4);
	
	private String name;
	private int index;
	
	private Color(String name, int index) {
		this.name = name;
		this.index = index;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
	
}

 

import java.util.Date;

public class Flower {
	private String name;
	private String PlaceofOrigin;
	private Color color;
	private Date date;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPlaceofOrigin() {
		return PlaceofOrigin;
	}
	public void setPlaceofOrigin(String placeofOrigin) {
		PlaceofOrigin = placeofOrigin;
	}
	public Color getColor() {
		return color;
	}
	public void setColor(Color color) {
		this.color = color;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	@Override
	public String toString() {
		return "Flower [name=" + name + ", PlaceofOrigin=" + PlaceofOrigin + ", color=" + color + ", date=" + date
				+ "]";
	}
	
}

 

import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class EnumSerializer implements JsonSerializer<Color>,JsonDeserializer<Color> {
	JsonPrimitive colorPrimitive;

	public JsonElement serialize(Color color, Type typeOfSrc, JsonSerializationContext context) {
		colorPrimitive = new JsonPrimitive(color.getName());
		return colorPrimitive;
	}
	
	public Color deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
			throws JsonParseException {
		if(json.getAsJsonPrimitive().equals(colorPrimitive)){
			String name = json.getAsJsonPrimitive().getAsString();
		 
			for(int i=0;i<Color.values().length;i++){
				if(Color.values()[i].getName().equals(name)) return Color.values()[i];
			}
			
		}
		
		return null;
	}
	
}

 

测试类:

 

import java.util.Date;
import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class TestEnumGson {

	//无注册自定义TyepAdapter
	@Test
	public void testEnum(){
		System.out.println("-----------------无注册自定义TyepAdapter---------------------");
		Flower flower = new Flower();
		flower.setName("Rose");
		flower.setPlaceofOrigin("China");
		flower.setColor(Color.RED);
		flower.setDate(new Date());
		
		Gson gson = new Gson();
		String flowerStr = gson.toJson(flower);
		System.out.println(flowerStr);
		
		Flower flower1 = gson.fromJson(flowerStr, Flower.class);
		System.out.println(flower1);
	}
	
	//注册自定义TyepAdapter
	@Test
	public void testEnumSerializer(){
		System.out.println("-----------------注册自定义TyepAdapter---------------------");
		Flower flower = new Flower();
		flower.setName("Rose");
		flower.setPlaceofOrigin("China");
		flower.setColor(Color.RED);
		flower.setDate(new Date());
		
		GsonBuilder gsonBuiler = new GsonBuilder();
		gsonBuiler.registerTypeAdapter(Color.class, new EnumSerializer());
		
		Gson gson = gsonBuiler
				.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		String flowerStr = gson.toJson(flower);
		System.out.println(flowerStr);
		
		Flower flower1 = gson.fromJson(flowerStr, Flower.class);
		System.out.println(flower1);
	}
	
}

 

 

结果:

-----------------注册自定义TyepAdapter---------------------
{"name":"Rose","PlaceofOrigin":"China","color":"红色","date":"2016-02-17 10:35:46"}
Flower [name=Rose, PlaceofOrigin=China, color=RED, date=Wed Feb 17 10:35:46 CST 2016]
-----------------无注册自定义TyepAdapter---------------------
{"name":"Rose","PlaceofOrigin":"China","color":"RED","date":"Feb 17, 2016 10:35:46 AM"}
Flower [name=Rose, PlaceofOrigin=China, color=RED, date=Wed Feb 17 10:35:46 CST 2016]

 

PS:  区别看color的值 "红色" 和 "RED", date不一样是因为设置了 gsonBuiler.setDateFormat("yyyy-MM-dd HH:mm:ss")

 

 

参考资料:

Gson官方文档:https://github.com/google/gson/blob/master/UserGuide.md

Gson指南(Gson官方文档中文版):http://www.letiantian.me/2014-05-22-java-gson-json/

Gson进阶(高级用法):

http://itroadmap.sinaapp.com/2015/11/04/gson%E8%BF%9B%E9%98%B6%EF%BC%88%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95%EF%BC%89/
Json转换利器Gson之实例六:http://blog.csdn.net/lk_blog/article/details/7685347

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值