Jackson对泛型的序列化和反序列化方法汇总
说明:Jackson对于简单泛型是可以正常操作的,但是如果对于太过于复杂的泛型类有时会不成功。目前还在找着更合适的Json库。不过这一点在dotnet原生方案JavaScriptSerializer可以完美解决这一些问题,无论泛型多复杂。
例子如下:
package com.jsoft.springboottest.springboottest1.controller; import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsoft.springboottest.springboottest1.Pager;
import com.jsoft.springboottest.springboottest1.PagerAppoint;
import com.jsoft.springboottest.springboottest1.User; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; @RestController
public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @RequestMapping("/show")
public String show() throws IOException {
ObjectMapper mapper = new ObjectMapper();
User user = new User();
user.setId(1);
Pager<User> pager = new Pager<User>();
List<User> users = new ArrayList<User>();
users.add(user);
pager.setDatas(users);
String json = mapper.writeValueAsString(pager);
// 方式1
Pager<User> userPager1 = mapper.readValue(json, new TypeReference<Pager<User>>() {
});
// 方式2
Type[] types = new Type[1];
types[0] = User.class;
final ParameterizedTypeImpl type = ParameterizedTypeImpl.make(Pager.class, types, Pager.class.getDeclaringClass());
TypeReference typeReference = new TypeReference<Pager>() {
@Override
public Type getType() {
return type;
}
};
Pager<User> userPager2 = mapper.readValue(json, typeReference);
// 方式3
JavaType javaType = mapper.getTypeFactory().constructParametrizedType(Pager.class, Pager.class, User.class);
Pager<User> userPager3 = mapper.readValue(json, javaType);
// 方式4
JavaType javaType1 = mapper.getTypeFactory().constructParametricType(Pager.class, User.class);
Pager<User> userPager4 = mapper.readValue(json, javaType1);
// 方式5,新建另一个指定具体泛型T的参数的类
PagerAppoint userPager5 = mapper.readValue(json, PagerAppoint.class);
// 数组泛型的序列化和反序列化
String json1 = mapper.writeValueAsString(users);
JavaType javaType2 = mapper.getTypeFactory().constructParametricType(List.class, User.class);
List<User> users1 = mapper.readValue(json1, javaType2);
// HashMap
Map<String, User> map = new HashMap<String, User>(16);
map.put("test", user);
String json2 = mapper.writeValueAsString(map);
//
Map<String, User> users2 = mapper.readValue(json2, new TypeReference<Map<String, User>>() {
});
//
JavaType javaType3 = mapper.getTypeFactory().constructParametricType(HashMap.class, String.class, User.class);
Map<String, User> users3 = mapper.readValue(json2, javaType3); return "hello world";
}
}
示例工程:https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest10
参考:
http://www.yiibai.com/jackson/jackson_data_binding_generics.html
http://blog.csdn.net/z69183787/article/details/46235905
http://bbs.csdn.net/topics/391823803
https://www.cnblogs.com/quanyongan/archive/2013/04/16/3024993.html
http://www.hankcs.com/program/json-to-map-java-demo-code.html
http://huangyunbin.iteye.com/blog/2352243
http://www.jianshu.com/p/ca03c2fe36e3
http://www.yiibai.com/jackson/jackson_data_binding_generics.html
http://bbs.csdn.net/topics/391823803
https://www.cnblogs.com/quanyongan/archive/2013/04/16/3024993.html
Jackson对泛型的序列化和反序列化方法汇总的更多相关文章
- ProtoBuf序列化和反序列化方法
最近公司需要将以前的协议全都改成ProtoBuf生成的协议,再将结构体打包和解包过程终于到一些问题 ,无法使用Marshal.SizeOf计算结构体大小,最后找了一下ProtoBuf的文档,可以用它自 ...
- MessagePack Java Jackson Dataformat - POJO 的序列化和反序列化
在本测试代码中,我们定义了一个 POJO 类,名字为 MessageData,你可以访问下面的链接找到有关这个类的定义. https://github.com/cwiki-us-demo/serial ...
- Scala中使用 Jackson API 进行JSON序列化和反序列化
1. 什么是 Json 序列化 和 反序列化 序列化 => 将 Java对象 转换成 json字符串反序列化 => 将 json字符串 转换成 Java对象 2. 依赖包 说明 < ...
- jackson中自定义处理序列化和反序列化
http://jackyrong.iteye.com/blog/2005323 ********************************************** 对于一直用gson的人来说 ...
- SrpingMVC通过JSON注入from数据到实体自定义(LocalDateTime,LocalDate,Boolean类型)字段的序列化、反序列化方法
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingExcept ...
- MessagePack Java Jackson Dataformat - Map 的序列化和反序列化
本测试方法,可以在 https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java ...
- .Net 2.0自带的Json序列化、反序列化方法
public class JsonUtil { public static T DeserializeObject<T>(string json) { ...
- Jackson序列化和反序列化
1,下载Jackson工具包(jackson-core-2.2.3.jar jackson-annotations-2.2.3.jar jackson-databind-2.2.3.jar ) j ...
- jackson对Exception类型对象的序列化与反序列化
发现问题 今天在调试系统错误通知的时候遇到了一个问题.我们在系统异常时候要通过队列系统发送各种通知到团队内部成员. 因此我写了一个通用接口.接口中有传递Exception对象到队列中,再由队列消费者解 ...
随机推荐
- python模块之datetime
相比于time模块,datetime模块的接口则更直观.更容易调用 datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day: ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- MIP启发式算法:Variable neighborhood search
*本文主要记录和分享学习到的知识,算不上原创. *参考文章见链接. 本文主要讲述启发式算法中的变邻域搜索(Variable neighborhood search).变邻域搜索的特色在于邻域结构的可变 ...
- python for data analysis chapter1~2
Q1:numpy与series的区别:index Tab补全(任意路径Tab) 内省(函数:?显示文档字符串,??显示源代码:结合通配符:np.* load *?) %load .py ctrl-c( ...
- 解决获取View的width和Height为0的4种方法
很经常当我们动态创建某些View时,需要通过获取他们的width和height来确定别的view的布局,但是在onCreate()获取view的width和height会得到0.view.getWid ...
- 安装python包
开始--cmd pip install pymongo 直到最后给出提示successfull install pymongo-**(版本号,最新的)
- python学习--Django mvc框架简介
让我们一览 Django 全貌 urls.py 网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数. views.py 处理用户发出的请求,从urls. ...
- PHP 修改配置文件后重启命名
centosPHP配置文件路径: /etc/php.ini 修改完配置文件后需要重启php服务: systemctl restart php-fpm
- chrome console.log API
console.log(object [, object, ...]) Displays a message in the console. You pass one or more objects ...
- hibernate悲观锁,乐观锁
业务逻辑的实现过程中,往往需要保证数据访问的排他性.因此,我们就需要通过一些机制来保证这些数据在某个操作过程中不会被外界修改,这样的机制,在这里,也就是所谓的“锁”,即给我们选定的目标数据上锁,使其无 ...