欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
Software MyZone 2群(山东):204954191
Software MyZone【Java群】:162865493
【加群请写:Software MyZone或者是firedragonpzy】
淘宝店:【应小心的易淘屋】初次开店,大家多多支持……
群论坛:【火龙论坛】正试运营阶段,欢迎大家多提些建设性意见……
群问答:【火龙问答】正试运营阶段,欢迎大家多提些建设性意见……
摘自:http://blog.csdn.net/lk_blog/article/details/7685347

我的digitalocean推广链接:https://www.digitalocean.com/?refcode=65dfdb158f1a,Everyone you refer gets $10 in credit,从此链接注册,你将获得10美元。

枚举类型给我们的程序带来了好处,如何用Gson来实现与Json的互转呢?请看本文.

本文重点掌握如何自己写一个TypeAdapter及注册TypeAdapter和处理Enum类型.

实体类:

public enum PackageState {
	PLAY, UPDATE, UPDATING, DOWNLOAD, DOWNLOADING,
}
public class PackageItem {
	private String name;
	private PackageState state;
	private String size;

	public String getName() {
		return name;
	}

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

	public PackageState getState() {
		return state;
	}

	public void setState(PackageState state) {
		this.state = state;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	@Override
	public String toString() {
		return "PackageItem [name=" + name + ", size=" + size + ", state="
				+ state + "]";
	}
}

自己写一个转换器实现JsonSerializer< T >接口和jsonDeserializer< T >接口:

mport 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< packagestate >,
		JsonDeserializer < packagestate > {

	// 对象转为Json时调用,实现JsonSerializer < packagestate >接口
	@Override
	public JsonElement serialize(PackageState state, Type arg1,
			JsonSerializationContext arg2) {
		return new JsonPrimitive(state.ordinal());
	}

	// json转为对象时调用,实现JsonDeserializer < packagestate >接口
	@Override
	public PackageState deserialize(JsonElement json, Type typeOfT,
			JsonDeserializationContext context) throws JsonParseException {
		if (json.getAsInt() < PackageState.values().length)
			return PackageState.values()[json.getAsInt()];
		return null;
	}

}

测试类:

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

public class GsonTest6 {

	public static void main(String[] args) {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(PackageState.class,
				new EnumSerializer());
		Gson gson = gsonBuilder.create();
		PackageItem item = new PackageItem();
		item.setName("item_name");
		item.setSize("500M");
		item.setState(PackageState.UPDATING);// 这个 state是枚举值

		String s = gson.toJson(item);
		System.out.println(s);

		System.out.println("--------------------------------");

		PackageItem retItem = gson.fromJson(s, PackageItem.class);
		System.out.println(retItem);
	}
}

输出结果(结果中已经将state的对应枚举类型转为了int类型):

{"name":"item_name","state":2,"size":"500M"}
--------------------------------
PackageItem [name=item_name, size=500M, state=UPDATING]

  1. Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
  2. Json转换利器Gson之实例二-Gson注解和GsonBuilder
  3. Json转换利器Gson之实例三-Map处理(上)
  4. Json转换利器Gson之实例四-Map处理(下)
  5. Json转换利器Gson之实例五-实际开发中的特殊需求处理
  6. Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型
  7. Tags: ,