自定义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}"}

自定义GSON类型适配器,布布扣,bubuko.com

时间: 2024-08-02 11:08:53

自定义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

关于自定义表类型遇到的问题~

有时候存储过程使用自定义表类型,就一个字~方便,类似于整个table直接传入到存储过程里面去玩,比起"万能"的xml~ 还不用解析,直接能用~多方便. 但是!!! 使用自定义表变量,并不一定是一帆风顺的~比如说和如果调用的参数不是有数据库这边来组装,由程序去控制传入的时候,我目前发现的就有以下2个地方十分的需要注意 1.字段顺序! 在我们看来,自定义表变量,就当时一个普通的表进行使用,赋值,写插入,直接insert select 就完事了~但是,如果写在存储过程的传入参数里面,确不一定

无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent&

Spring MVC__自定义日期类型转换器

WEB层采用Spring MVC框架,将查询到的数据传递给APP端或客户端,这没啥,但是坑的是实体类中有日期类型的属性,但是你必须提前格式化好之后返回给它们.说真的,以前真没这样做过,之前都是一口气查询到数据,然后在jsp页面上格式化,最后展示给用户.但是这次不同,这次我纯属操作数据,没有页面.直接从数据库拿数据给它们返数据.它们给我传数据我持久化数据,说到这里一个小问题就默默的来了. 首先把问题还原一下吧(这是一个数据导出功能),下图中用红框圈起来的都是直接从数据库中拿到的数据,但是不幸的是它

WordPress使用自定义文章类型实现任意模板的方法和怎么做邮件回复

主要就是使用了register_post_type 函数. 1.创建插件目录 新建一个文件夹用来存放插件文件,这里我就命名这个文件夹为myMood 2.创php代码文件 在刚才创建的文件夹里面新建一个php文件,命名为myMood,用来书写插件代码 3.添加头部描述 复制代码 代码如下:<?php/*Plugin Name: Movie ReviewsPlugin URI: http://wp.tutsplus.com/Description: Declares a plugin that wi

C# 值类型,自定义值类型(struct,enum)

/*C#值类型 ValueType * 1.有两种自定义值类型:结构,枚举: * 2.值类型(value type)和引用类型(reference type):区别源于复制策略的不同,后者又造成每种类型在内存中以不同的方式存储.值类型:直接包含值,换句话说就是引用的位置就是值在内存中 * 实际存储位置,寻址方式就是直接寻址:值类型存储在栈里面,引用类型存储在堆里面,变量名称存储在栈里面:值类型需要一个内存副本: * 值类型传值得方式是复制:引用是共享同一块内存:引用类型的变量关联了两个存储位置:

Unity3D ShaderLab 创建自定义高光类型

Unity3D ShaderLab 创建自定义高光类型 在上一篇,我们认识了Unity基础的高光实现,本次主要是研究如何对Phong高光类型进行顶点操作,以及在表面着色器中使用Input结构体的新参数进行像素操作. 所以还是新建Shader,再建材质球然后打开编辑器进行shader的编辑. 1.修改Properties Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _MainTint("Diff

自定义委托类型 - .Net自带委托类型

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递. 与其他的类不同,委托类具有一个签名,并且它只能对与其签名匹配的方法进行引用. 一.自定义委托类型 1.语法结构:访问修饰符 delegate 返回类型 委托类型名称(参数列表); 例如: // 声明一个委托类型,两个参数均为int类型,返回值为int类型 public delegate int Calc(int a, int b);自定义的委托可以不带参数,也可以没有返回值. 接下来我们看一个例子怎么使用委托 1.