JFinal的Model进行json反序列化

在使用JFinal进行开发时,我们可能会需要解决这样的问题:Model进行json的序列化与反序列化。

官方已经提供了序列化的方法Model.toJson()非常方便,反序列化就得自己实现一下了。

之前我一直都是把Model序列化成的json字符串,反序列化成map,然后再调用Model.setAttrs(map)。这样就有类型转换问题,最后反序列化得到的Model,原本的日期类型成了字符串,Long可能会成为Integer类型,再调用getLong()和getDate()就会报错。所以,必须要在反序列化进行属性类型的精确转换。通过TableMapping.me().getTable(modelClass) 可以得到model对应的Table;table有个columnTypeMap成员变量,是列名和类型的映射;利用这个特性就可以实现精确转换了。

下面是具体代码实现,使用了fastjson:

public static <T> T jsonToModel(String str, Class<? extends Model<?>> clazz) {
		// 使用fastjson先反序列化成json对象
		JSONObject json = JSON.parseObject(str);
		// 获取table
		Table table = TableMapping.me().getTable(clazz);
		// 得到属性类型的map
		Map<String, Class<?>> typeMap = table.getColumnTypeMap();
		Model<?> model = null;
		try {
			model = clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		Set<Entry<String, Class<?>>> enterSet = typeMap.entrySet();
		for (Entry<String, Class<?>> entry : enterSet) {
			String attr = entry.getKey();
			Class<?> type = entry.getValue();
			if (Short.class.equals(type)) {
				// 短整型
				model.set(attr, json.getShort(attr));
			} else if (Integer.class.equals(type)) {
				// 整型
				model.set(attr, json.getInteger(attr));
			} else if (Long.class.equals(type)) {
				// 长整型
				model.set(attr, json.getLong(attr));
			} else if (Float.class.equals(type)) {
				// 浮点型
				model.set(attr, json.getFloat(attr));
			} else if (Double.class.equals(type)) {
				// 双精度浮点型
				model.set(attr, json.getDouble(attr));
			} else if (BigDecimal.class.equals(type)) {
				// big decimal
				model.set(attr, json.getBigDecimal(attr));
			} else if (String.class.equals(type)) {
				// 字符串
				model.set(attr, json.getString(attr));
			} else if (java.sql.Date.class.equals(type)) {
				// 年月日的日期类型
				Date date = json.getDate(attr);
				model.set(attr,
						date == null ? null : new java.sql.Date(date.getTime()));
			} else if (Time.class.equals(type)) {
				// 年月日的日期类型
				Date date = json.getDate(attr);
				model.set(attr, date == null ? null : new Time(date.getTime()));

			} else if (Timestamp.class.equals(type)) {
				// 时间戮
				Date date = json.getDate(attr);
				model.set(attr,
						date == null ? null : new Timestamp(date.getTime()));
			} else if (Boolean.class.equals(type)) {
				// 布尔型
				model.set(attr, json.getBoolean(attr));
			}
			// 其它的忽略,可能还有字节数组类型
			// 具体model的属性可能有哪些类型可以参考 TableBuilder.doBuild()代码

		}

		return (T) model;
	}

本人水平有限,可能代码有不妥不优雅之处,希望各位不要拍砖,多提点建议。


转载于:https://my.oschina.net/taijunfeng/blog/354181

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值