一:用于测试的类

User类

import lombok.Data;
import lombok.experimental.Accessors; /**
* @author silentdoer
* @version 1.0
*/
@Data
@Accessors(chain = true)
public class User {
private String name;
private String gender;
}

AppResult类

import lombok.Data;
import lombok.experimental.Accessors; /**
* @author silentdoer
* @version 1.0
*/
@Data
@Accessors(chain = true)
public class AppResult<T> {
private int code;
private T data;
}

二:Fastjson测试代码

String str = "{\"code\":100,\"data\":{\"gender\":\"男\",\"name\":\"中文\"}}";
AppResult<User> o = JSON.parseObject("{\"code\":100,\"data\":{\"gender\":\"男\",\"name\":\"中文\"}}", AppResult.class);
System.out.println(o);
System.out.println(o.getData().getClass()); // flag

在flag行会报java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to User

原因:因为Java的泛型擦除机制,无法像C#一样使用类似AppResult<User>.class,而AppResult.class中的T虽然在字节码里也叫T,但是会被jvm认为是Object的任意子类;

结论:Fastjson检测到要转换的类型是泛型时将会把那部分JSON字符串转换为fastjson的JSONObject对象赋值给data,因此这里报转换错误

三:Gson测试代码

Gson gson = new Gson();
AppResult<User> appResult = gson.fromJson(str, AppResult.class);
System.out.println(appResult);
System.out.println(appResult.getData().getClass());

也是报异常:java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to User

结论:Gson如果检测到要反序列化的对象类型是泛型,那么将用一个gson自己的LinkedTreeMap来存储

四:对于Jackson也是一样

五:实现泛型的转换方式

1.对于Fastjson或Gson都是可以有个TypeToken一样的东西,不过这里用个更简单的方法:

Fastjson:

AppResult<User> o = JSON.parseObject("{\"code\":100,\"data\":{\"gender\":\"男\",\"name\":\"中文\"}}", new AppResult<User>(){}.getClass());

Gson:Gson似乎不行,只能用TypeToken来实现;

Fastjson可以的原理,其实就是因为泛型类虽然无法保留泛型的具体类型,但是其子类在具体化泛型后是可以保留的,因此new一个泛型类的空实现的子类,然后getClass()即可;

Fastjson和Gson零碎总结的更多相关文章

  1. FastJSON、Gson和Jackson性能对比

    Java处理JSON数据有三个比较流行的类库FastJSON.Gson和Jackson.本文将测试这三个类库在JSON序列化和反序列化的方面表现,主要测试JSON序列化和反序列化的速度.为了防止由于内 ...

  2. FastJson和Gson和Json数据解析分析和用法

    首先分析下目前号称最快的FastJson,这个是所有人都验证过的,解析速度确实比较快,不过也需要根据数据量来看,数据量小的时候,Gson性能要稍微优于FastJson,但在数据量大解析的情况下,Fas ...

  3. FastJSON、Gson、Jackson(简单了解使用)

    下载地址(maven) Jackson:http://mvnrepository.com/search?q=jackson FastJson:http://mvnrepository.com/sear ...

  4. json工具包比较 fastjson jackson gson

    对json进行json-object进行相互转化时,笔者接触到三种工具jar,现对其进行比较. fastjson:速度最快,阿里巴巴开源. jackson:springMvc 默认使用. gson:谷 ...

  5. FastJson与Gson小测试

    最近用到Json来传输数据,找到两个比较简单的工具 Gson 和 FastJson随便测试一下两个工具的效率~ 1 package com.json.fast; import java.util.Ar ...

  6. Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)

    一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...

  7. 使用fastjson,gson解析null值的时候键保留

    由于业务需求...所以查阅资料,总结如下: 使用gson实现方法:只需要把new Gson()改为: new GsonBuilder().serializeNulls().create(); 就可以了 ...

  8. Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)

    一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...

  9. fastjson用法&Gson

    <dependency>    <groupId>com.google.code.gson</groupId>   <artifactId>gson&l ...

随机推荐

  1. ln: operation not permitted

    ln: operation not permitted 在挂载的磁盘上建立软链接提示没有操作权限 例如: ln -s aa bb1ln:aa operation not permitted------ ...

  2. python脚本删除文件与目录的命令

    1. 删除文件的命令 import os os.remove(file) os.unlink(file) 2.删除目录的命令 import shutil shutil.rmtree(directory ...

  3. volley的post请求

    //volley发送post请 2 private void volleypost() { 3 String url = "http://apis.juhe.cn/idcard/index? ...

  4. IEDriver executable needs to be available in the path

    解决办法: 去http://selenium-release.storage.googleapis.com/index.html 下载指定IE版本 解压后 拷贝到Python的目录下 重新运行,提示错 ...

  5. gRPC的.netClient客户端第一次连接出现StatusCode=Unavailable的解决办法?

    1.问题还原: The service does not automatically connect, free after a period of time, the first call will ...

  6. 【Linux系列】Ubuntu ping通,xshell无法连接

    现象描述:Ubuntu能Ping通主机,主机也能ping通虚拟机.而且,虚拟机也能上网.只是xshell不能连接. 解决方案: 一:使用管理员身份 设置防火墙. 先查看一下防火墙状态 sudo ufw ...

  7. Python 字符串 (isdigit, isalnum,isnumeric)转

    Python isdigit() 方法检测字符串是否只由数字组成. 语法 isdigit()方法语法: str.isdigit() 参数 无. 返回值 如果字符串只包含数字则返回 True 否则返回 ...

  8. Python之路(第十四篇)os模块

    一.os模块 1.os.getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) import os print(os.getcwd()) 2.os.chdir(path) 改变 ...

  9. centos6.5 yum安装postgresql9.3

    rpm -ivh http://download.postgresql.org/pub/repos/yum/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-2.n ...

  10. JVM新生代到老年代基础了解

    JVM区域总体分两类,heap区和非heap区. heap区又分为: - Eden Space(伊甸园). - Survivor Space(幸存者区). - Old Gen(老年代). 1个Eden ...