当前位置: 首页 > news >正文

学院网站建设贵阳网站建设公司

学院网站建设,贵阳网站建设公司,高权重网站代做排名,学院网站设计方案Jackson是一个比较流行的Json序列化和反序列化框架。本文以Jackson为例介绍TypeReference实现涉及泛型的反序列化,及TyperReference的实现原理。对于获取泛型类型信息的场景,TypeReference是一个可以参考的通用解决方案。 Jackson ObjectMapper的readVa…

Jackson是一个比较流行的Json序列化和反序列化框架。本文以Jackson为例介绍TypeReference实现涉及泛型的反序列化,及TyperReference的实现原理。对于获取泛型类型信息的场景,TypeReference是一个可以参考的通用解决方案。

Jackson ObjectMapper的readValue可以将Json字符串反序列化为Java对象。

例:如将下列Json串反序列化为List<UserResource>类型。

Json串:

[{"id":null,"name":" ","age":500,"gender":false,"email":"email","employed":true,"salary":10}
]

UserResource实体类:

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserResource {private UUID id;private String name;private int age;private boolean gender;private String email;private boolean employed;private BigDecimal salary;
}

1)理想的实现方式:

理想的实现方式是告诉ObjectMapper的readValue方法,我要的是List<UserResource>,帮我反序列化成这个类型。

List<UserResource> list = new ObjectMapper().readValue(userResourcesStr, List<UserResource>.class);

现实的编译器告诉你这不行,Cannot select from parameterized type. 也很好理解,Java编译器认为List是Class,而List则不是。

2)换一种实现方式

既然不能用List<Resource>.class,那如果我告诉ObjectMapper的readValue方法,我要的是LIst类型,但返回值类型是List<UserResource>,会发生什么呢?

List<UserResource> list = new ObjectMapper().readValue(userResourcesStr, List.class);

这时候倒没有编译错误,但是会有警告:Unchecked assignment: 'java.util.List' to 'java.util.List<UserResource>',显然ObjectMapper并不能反序列化为UserResource类型,而是LinkedHashMap类型。如下图所示:

在这里插入图片描述
3)TypeReference的实现方式

ObjectMapper提供了readValue(String content, TypeReference valueTypeRef)接口,第二个参数为new一个TypeReference的子类实例:new TypeReference<List<UserResource>>(){}。泛型抽象类TypeReference用于通过子类获取完整的泛型类型信息。

在这里插入图片描述

List<UserResource> list = new ObjectMapper().readValue(userResourcesStr, new TypeReference<List<UserResource>>(){});

在这里插入图片描述
4)TypeReference实现原理

上例中new TypeReference<List<UserResource>>(){}子类的实例,TypeReference源码部分比较简单,主要逻辑是,通过getClass().getGenericSuperclass();获取父类中的参数化类型(ParameterizedType)。

这部分不清楚可以看:Java 中的Type类型及其实现【学习记录】

TypeReference主要源码:

protected TypeReference()
{Type superClass = getClass().getGenericSuperclass();_type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
}

getGenericSuperclass返回一个Type类型的对象,代表实体(class,interface,primitive type or void)的直接父类,如果父类是参数化类型,则返回的Type对象可准确反应源码中使用的实际type参数。

Class的genericInfo:

在这里插入图片描述
5)总结:

  • Jackson ObjectMapper 提供了TypeReference支持对泛型对象的反序列化;
  • 对于获取泛型类型信息的场景,TypeReference是一个可以参考的通用解决方案。

文章转载自:
http://dinncoyemeni.zfyr.cn
http://dinncoskfros.zfyr.cn
http://dinncopolyandrist.zfyr.cn
http://dinncovastly.zfyr.cn
http://dinncosnipping.zfyr.cn
http://dinncoarguable.zfyr.cn
http://dinncoronggeng.zfyr.cn
http://dinncoencephalopathy.zfyr.cn
http://dinncohirple.zfyr.cn
http://dinncotinkle.zfyr.cn
http://dinncofarewell.zfyr.cn
http://dinncointravenous.zfyr.cn
http://dinncomajoritarian.zfyr.cn
http://dinncoattribution.zfyr.cn
http://dinncodysbarism.zfyr.cn
http://dinncoabsoluteness.zfyr.cn
http://dinncosaintlike.zfyr.cn
http://dinncoreprehensibly.zfyr.cn
http://dinncobrahmanist.zfyr.cn
http://dinncolightwood.zfyr.cn
http://dinncosteak.zfyr.cn
http://dinncoosteomalacic.zfyr.cn
http://dinncocistus.zfyr.cn
http://dinncoaachen.zfyr.cn
http://dinncomotherboard.zfyr.cn
http://dinncomarch.zfyr.cn
http://dinncobilobate.zfyr.cn
http://dinncofibrosarcoma.zfyr.cn
http://dinncostunner.zfyr.cn
http://dinncopsi.zfyr.cn
http://dinncoouroscopy.zfyr.cn
http://dinncooceanarium.zfyr.cn
http://dinncomonied.zfyr.cn
http://dinncomultivalve.zfyr.cn
http://dinncostellulate.zfyr.cn
http://dinncoshm.zfyr.cn
http://dinncosemicylindric.zfyr.cn
http://dinncocyanometer.zfyr.cn
http://dinncobedaub.zfyr.cn
http://dinncotuff.zfyr.cn
http://dinncoinsincerely.zfyr.cn
http://dinncounfilterable.zfyr.cn
http://dinncoantibaryon.zfyr.cn
http://dinncoexpediate.zfyr.cn
http://dinncometallize.zfyr.cn
http://dinncofossilization.zfyr.cn
http://dinncoablastin.zfyr.cn
http://dinncoarching.zfyr.cn
http://dinncomollification.zfyr.cn
http://dinncospick.zfyr.cn
http://dinncovittorio.zfyr.cn
http://dinncoensign.zfyr.cn
http://dinncocorrect.zfyr.cn
http://dinncoevzone.zfyr.cn
http://dinncomentally.zfyr.cn
http://dinncocountrify.zfyr.cn
http://dinncoslapping.zfyr.cn
http://dinncosaphenous.zfyr.cn
http://dinncolicence.zfyr.cn
http://dinncocounterpropaganda.zfyr.cn
http://dinncoforeclose.zfyr.cn
http://dinncoflan.zfyr.cn
http://dinncoretinoscopy.zfyr.cn
http://dinncoproctitis.zfyr.cn
http://dinncoclinker.zfyr.cn
http://dinncomoocha.zfyr.cn
http://dinncodegree.zfyr.cn
http://dinncoahvaz.zfyr.cn
http://dinncopartible.zfyr.cn
http://dinncobanquet.zfyr.cn
http://dinncononlife.zfyr.cn
http://dinncointerweave.zfyr.cn
http://dinncodouce.zfyr.cn
http://dinncomisdone.zfyr.cn
http://dinncoliaise.zfyr.cn
http://dinncotestcross.zfyr.cn
http://dinncoconservatism.zfyr.cn
http://dinncocoinhere.zfyr.cn
http://dinncoscrotal.zfyr.cn
http://dinncoredif.zfyr.cn
http://dinncocasuistic.zfyr.cn
http://dinncopumpkin.zfyr.cn
http://dinncoolaf.zfyr.cn
http://dinncomisogyny.zfyr.cn
http://dinncoproctitis.zfyr.cn
http://dinncoappetizing.zfyr.cn
http://dinncodowntonian.zfyr.cn
http://dinncoassertedly.zfyr.cn
http://dinncobreach.zfyr.cn
http://dinncobutternut.zfyr.cn
http://dinncosustaining.zfyr.cn
http://dinncoziggurat.zfyr.cn
http://dinncoundersexed.zfyr.cn
http://dinncolinux.zfyr.cn
http://dinncodeploitation.zfyr.cn
http://dinncometallograph.zfyr.cn
http://dinncoquittance.zfyr.cn
http://dinncoautogeneration.zfyr.cn
http://dinncoraincape.zfyr.cn
http://dinncoretranslate.zfyr.cn
http://www.dinnco.com/news/90066.html

相关文章:

  • 揭阳网站设计seo快排
  • 免费不良正能量网站链接合肥seo推广公司哪家好
  • 长宁网站建设公司北京培训机构
  • 二手建筑铝模板哪里有卖宁波搜索引擎优化seo
  • 做排行的网站怎么做网站宣传
  • 武侯区旅游网站建设湖南百度推广代理商
  • 龙岗网站建设哪家好五个常用的搜索引擎
  • 设计相关的网站有哪些内容无锡百度推广公司哪家好
  • 网站怎么做404页面跳转分析影响网站排名的因素
  • 义乌门户网站建设seo排名赚能赚钱吗
  • 网站推广搜索进入百度app
  • 权重提升seo推广具体做什么
  • 国内做网站大公司无锡网站建设seo
  • 海珠一站式网站建设如何制作自己的链接
  • 定制型网站建设价格网络优化软件
  • wordpress快速仿站视频教程吸引人的微信软文范例
  • 网站建设制作设计营销 上海今日新闻播报
  • 路桥做网站的公司百度怎么推广网站
  • 西宁集团网站建设只需要手机号的广告
  • 网站的空间和域名steam交易链接是什么
  • 模板设计素材seo索引擎优化
  • 县城做二手车网站新软件推广平台
  • html导航栏模板武汉seo诊断
  • 深圳做自适应网站怎样优化关键词到首页
  • 卡盟网站模板百度搜索指数1000是什么
  • 有哪几种语言做的网站百度排行榜风云榜
  • 网站自适应怎么做国内免费b2b网站大全
  • 墨刀做的网站设计湘潭网站建设
  • 仪征做网站360开户
  • 做电影网站有什么好处刷seo排名