Skip to content

[BUG] 2.0.57 com.alibaba.fastjson2.JSONObject#from(java.lang.Object) 结果泛型数据丢失 #3569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yan8056 opened this issue May 24, 2025 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@yan8056
Copy link

yan8056 commented May 24, 2025

问题描述

在使用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>

重现步骤

  1. 使用 com.alibaba.fastjson2.JSONObject#from(java.lang.Object) 方法
  2. 输入 带有泛型的java bean 数据
  3. 出现 结果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版本没有这个问题。

@yan8056 yan8056 added the bug Something isn't working label May 24, 2025
@jujn
Copy link
Contributor

jujn commented May 25, 2025

既然2.0.56版本没有这个问题,那应该就是2.0.57的bug,我去研究一下

@wenshao wenshao added this to the 2.0.58 milestone May 25, 2025
jujn added a commit to jujn/fastjson2 that referenced this issue May 25, 2025
jujn added a commit to jujn/fastjson2 that referenced this issue May 25, 2025
jujn added a commit to jujn/fastjson2 that referenced this issue May 27, 2025
jujn added a commit to jujn/fastjson2 that referenced this issue May 27, 2025
wenshao pushed a commit that referenced this issue May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants