-
Notifications
You must be signed in to change notification settings - Fork 541
Closed
Description
问题描述
在使用2.0.57版本 com.alibaba.fastjson2.JSONObject#from(java.lang.Object)
将含有泛型的java bean 转换为 JSONObject时,返回的结果中泛型数据为空的JSONObject
环境信息
- JDK信息: java version "1.8.0_431"
- 版本信息:2.0.57
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.57</version>
</dependency>
重现步骤
- 使用
com.alibaba.fastjson2.JSONObject#from(java.lang.Object)
方法 - 输入 带有泛型的java bean 数据
- 出现 结果JSONObejct中泛型字段值为
{}
错误
@Data
@AllArgsConstructor
public static class TestClass<T> {
private T generic;
private Long testLong;
}
public static void main(String[] args) {
TestClass<String> testClass = new TestClass<>("hello", 1L);
JSONObject jsonObject = JSONObject.from(testClass);
System.out.println(jsonObject.toString()); // Generic丢失 {"generic":{},"testLong":1}
}
期待的正确结果
预期返回:{"generic":"hello","testLong":1}
疑问:我对 com.alibaba.fastjson2.JSONObject#from(java.lang.Object)
的用法是正确的吗?使用fastjson2将java bean转换为JSONObject时候,推荐的做法是什么?
附加信息
2.0.56版本没有这个问题。