自己定义GSON类型适配器

Exception in thread "main"
java.lang.RuntimeException: No-args constructor for class java.sql.Timestamp does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

#关于使用Google GSON 实现Json协议字符协议序列化和反序列化 时间戳到Timestame类型转换失败问题。

解决方式:定义自己的类型适配器。

下面代码演示了两个问题:

1.解决上面说的时间戳到Timestame类型互转问题。

2.扩展了一个基于HTTP协议URL字符串解析到POJO HttpProtocol 的互转。

Code 例如以下:

package com.kevin.luan.service;

import java.lang.reflect.Type;
import java.sql.Timestamp;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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;

/**
 * 实现一个自己定义的实行适配器
 * <p>
 * 基于Google GSON 实现JSON解析
 * </p>
 *
 * @author kevin LUAN
 *
 */
public class GsonTypeAdapterDemo {
	public static void main(String[] args) {
		Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampAdapter()).registerTypeAdapter(HttpProtocol.class, new HttpProtocolAdapter()).create();
		String json = "{\"price\":\"1.1001\",\"times\":\"" + System.currentTimeMillis() + "\",\"protocol\":\"http://www.koudai.com/abc/test.do?url=abc\"}";
		Test pojo = gson.fromJson(json, Test.class);
		System.out.println("JSON TO POJO:" + pojo);
		json = gson.toJson(pojo);
		System.err.println("POJO TO JSON:" + json);
	}

	/**
	 * 实现一个类型适配器(TypeAdapter)
	 *
	 * @author kevin LUAN
	 *
	 */
	public static class TimestampAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp> {

		@Override
		public Timestamp deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
			if (json != null) {
				try {
					return new Timestamp(json.getAsLong());
				} catch (JsonParseException e) {
					throw e;
				}
			}
			return null;
		}

		@Override
		public JsonElement serialize(Timestamp value, Type type, JsonSerializationContext context) {
			if (value != null) {
				return new JsonPrimitive(value.getTime());
			}
			return null;
		}

	}

	/**
	 * 基于HttpProtocol的类型适配器
	 *
	 * @author kevin LUAN
	 *
	 */
	public static class HttpProtocolAdapter implements JsonSerializer<HttpProtocol>, JsonDeserializer<HttpProtocol> {

		@Override
		public HttpProtocol deserialize(JsonElement json, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
			if (json == null) {
				return null;
			} else {
				try {
					return new HttpProtocol(json.toString());
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}
		}

		@Override
		public JsonElement serialize(HttpProtocol src, Type arg1, JsonSerializationContext arg2) {
			return new JsonPrimitive(src.toString());
		}

	}

	/**
	 * 測试
	 * <p>
	 * JSON->POJO
	 * </p>
	 * <p>
	 * POJO->JSON
	 * </p>
	 *
	 * @author kevin LUAN
	 *
	 */
	public static class Test {
		private Float price = 1.0f;
		private Timestamp times;
		private HttpProtocol protocol;

		@Override
		public String toString() {
			return "price:" + price + "|times:" + times + "|protocl:" + protocol + "";
		}

	}

	/**
	 * HTTP协议POJO
	 *
	 * @author kevin LUAN
	 *
	 */
	public static class HttpProtocol {
		private String protocol;
		private String host;
		private int port = -1;
		private String uri;
		private String paramQuery;

		@Override
		public String toString() {
			return protocol + "://" + host + ":" + port + uri + "?" + paramQuery + "}";
		}

		public HttpProtocol(String value) {
			if (value.startsWith("\"") && value.endsWith("\"")) {
				value = value.substring(1, value.length() - 1);
			}
			parserProtocol(value);
		}

		private void parserProtocol(String value) {
			int endIndex = value.indexOf("://");
			if (endIndex != -1) {
				protocol = value.substring(0, endIndex);
				parserHost(value, endIndex + 3);
			}
		}

		private void parserHost(String value, int startIndex) {
			int endIndex = value.indexOf("/", startIndex);
			if (endIndex != -1) {
				host = value.substring(startIndex, endIndex);
				splitHostPort();
				parserUri(value, endIndex);
			} else {
				host = value.substring(startIndex);
				splitHostPort();
			}
		}

		private void splitHostPort() {
			if (host.indexOf(":") != -1) {
				String host_port[] = host.split(":");
				host = host_port[0];
				port = Integer.parseInt(host_port[1]);
			} else {
				port = 80;
			}
		}

		private void parserUri(String value, int startIndex) {
			if (value.indexOf("?", startIndex) == -1) {
				uri = value.substring(startIndex);
			} else {
				int endIndex = value.indexOf("?", startIndex);
				uri = value.substring(startIndex, endIndex);
				parserQuery(value, endIndex);
			}
		}

		private void parserQuery(String value, int startIndex) {
			if (value.indexOf("?", startIndex) != -1) {
				int paramQueryIndex = value.indexOf("?", startIndex);
				paramQuery = value.substring(paramQueryIndex + 1);
			}
		}

	}
}

执行Main结果:

JSON TO POJO:price:1.1001|times:2014-06-22 13:06:54.138|protocl:http://www.koudai.com:80/abc/test.do?url=abc}

POJO TO JSON:{"price":1.1001,"times":1403413614138,"protocol":"http://www.koudai.com:80/abc/test.do?url\u003dabc}"}

时间: 2024-08-25 15:11:25

自己定义GSON类型适配器的相关文章

自定义GSON类型适配器

Exception in thread "main" java.lang.RuntimeException: No-args constructor for class java.sql.Timestamp does not exist. Register an InstanceCreator with Gson for this type to fix this problem. #关于使用Google GSON 实现Json协议字符协议序列化和反序列化 时间戳到Timestame类

Gson 基础教程 —— 自定义类型适配器(TypeAdapter)

1,实现一个类型适配器(TypeAdapter) 自定义类型适配器需要实现两个接口: JsonSerializer<T> JsonDeserializer<T> 和两个方法: [java] view plaincopy //序列化 public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context); [java] view plaincopy //反序列化 public T de

使用typedef语句定义数组类型

使用typedef语句定义数组类型     1. 一维数组类型的定义格式 typedef <元素类型关键字><数组类型名>[<常量表达式>]; 例如: (1) typedef int vector[10]; (2) typedef char strings[80]; (3) typedef short int array[N]; 第一条语句定义了一个元素类型为int,含有10个元素的数组类型vector,若不使用typedef保留字,则就变成了数组定义,它只定义了一个元

[转]Android中常用适配器及定义自己的适配器

一,适配器. 顾名思义,就是把一些数据给弄得适当,适合以便于在View上显示.可以看作是界面数据绑定的一种理解.它所操纵的数据一般都是一些比较复杂的数据,如数组,链表, 数据库,集合等.适配器就像显示器,把复杂的东西按人可以接受的方式来展现. 那么适配器是怎么处理得到的数据,并把它显示出来的呢.其实很简单,说白了适配器它也是一个类,在类里面它实现了父类的这几个方法:   publicint getCount() //得到数据的行数 public Object getItem(int positi

#define与typedef定义的类型名的区别

1.可以用其他类型说明符对#define定义的类型名进行扩展,但对typedef所定义的类型名不能这样做. 例如: #define peach int unsigned peach i; //加上unsigned类型说明符,正确! typedef int banana; unsigned banana i; //加上unsigned类型说明符,错误! 2.在连续的变量声明中,用typedef定义的类型能够保证声明中的所有变量均为同一种类型,而用#define定义的类型则无法保证. 例如: #de

java基础:定义不同类型的变量

注意: 在定义long类型的值是末尾需要加l或者L: 在定义单精度浮点型的数据变量是需要在末尾加f或F: 作用域: 变量定义在哪一级大括号中,哪个大括号的范围就是这个变量的作用域.相同的作用域中不能定义两个同名变量. 初始化值: 没有初始化值不能直接使用(只要在使用前赋值就行,并不一定必须要在定义时候给值) 在一行上建议之定义一个变量 (可以定义多个,但是不建议)

C#简单问题,不简单的原理:不能局部定义自定义类型(不含匿名类型)

今天在进行代码测试时发现,尝试在一个方法中定义一个委托,注意是定义一个委托,而不是声明一个委托变量,在编写的时候没有报错,VS也能智能提示,但在编译时却报语法不完整,缺少方括号,但实际查询并没有缺少,想不通原因,将委托定义移到类中,报错消失,编译成功了. 先看一下报错的源码:(实际上不只委托类型,所有的自定义类型均报错) class Class2 { public void Test() { delegate void testDel(string p); //是错误的 event testDe

Android EventBus的简单使用基本的使用步骤就是如下4步,点击此链接查看例子及介绍。 定义事件类型: `public class MyEvent {}` 定义事件处理方法: `public

基本的使用步骤就是如下4步,点击此链接查看例子及介绍. 定义事件类型: `public class MyEvent {}` 定义事件处理方法: `public void onEventMainThread` 注册订阅者: `EventBus.getDefault().register(this)` 发送事件: `EventBus.getDefault().post(new MyEvent())` 一.实现 **EventBus**使用方法很简单,但用一个东西,如果不了解它的实现用起来心里总是没底

SQL 用户定义表类型,在存储过程里使用数据类型作參数

在数据库编程里使用数据类型,能够提高代码的重用性.它们常常被使用在方法和存储过程中.使用数据类型,我们能够避免在存储过程里定义一串的參数,让人眼花缭乱,它就相当于面向对象语言里.向一个方法里传入一个对象,而该对象有各种属性,存储过程仅仅须要获取这个对象就能获取到各个參数,然后做出对应的处理.有所不同的是SQL的表类型是能够包括多条数据的.到底是怎么一回事,且看以下的样例. 1. 首先我创建了一个学生表,包括四个字段,主键是从1開始的自增长型. GO CREATE TABLE STUDENT( I