Skip to content
New issue

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

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

Already on GitHub? # to your account

[QUESTION]复杂的嵌套bean反序列化无法设置默认值 #367

Closed
q605450469 opened this issue May 27, 2022 · 6 comments
Closed

[QUESTION]复杂的嵌套bean反序列化无法设置默认值 #367

q605450469 opened this issue May 27, 2022 · 6 comments
Assignees
Labels
bug Something isn't working fixed question Further information is requested
Milestone

Comments

@q605450469
Copy link

依赖版本
jdk1.8.0_162

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.28</version>
</dependency>
<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.22</version>
</dependency>
<dependency>
      <groupId>com.alibaba.fastjson2</groupId>
      <artifactId>fastjson2</artifactId>
      <version>2.0.4</version>
</dependency>

DTO

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestDTO {
    
    private String name;
    private int age;
    private SubDTO subDTO = new SubDTO();

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class SubDTO {
        private String id;
        private String nickName;
        private ThiDTO thiDTO = new ThiDTO();
        private FouDTO fouDTO = new FouDTO();
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ThiDTO {
    private String h;
    private String w;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FouDTO {
    private String p;
    private String t;
}

json

{
    "name": "123",
    "age": 3,
    "subDTO": {
        "id": "222",
        "thiDTO": {
            "h": "444"
        }
    }
}

运行代码

String s = "{\"name\":\"123\",\"age\":3,\"subDTO\":{\"id\":\"222\",\"thiDTO\":{\"h\":\"444\"}}}";
TestDTO testDTO2 = JSON.parseObject(s, TestDTO.class, JSONReader.Feature.SupportSmartMatch);
TestDTO testDTO1 = com.alibaba.fastjson.JSON.parseObject(s, TestDTO.class);
System.out.println("testDTO2 = " + testDTO2);
System.out.println("testDTO1 = " + testDTO1);

执行结果

testDTO2 = TestDTO(name=123, age=3, subDTO=TestDTO.SubDTO(id=222, nickName=null, thiDTO=ThiDTO(h=444, w=null), fouDTO=null))
testDTO1 = TestDTO(name=123, age=3, subDTO=TestDTO.SubDTO(id=222, nickName=null, thiDTO=ThiDTO(h=444, w=null), fouDTO=FouDTO(p=null, t=null)))

其中SubDTO是TestDTO的成员内部类,ThiDTO与FouDTO与TestDTO同包类,这种嵌套类结构在使用1.2.x版本可以实现全部成员类的默认值。但是在2版本无法实现fouDTO的默认值。
请问除了将SubDTO从成员内部类提出来外还有别的方式可以实现默认值吗?

@q605450469 q605450469 added the question Further information is requested label May 27, 2022
wenshao added a commit that referenced this issue May 27, 2022
wenshao added a commit that referenced this issue May 27, 2022
@wenshao wenshao added this to the 2.0.6 milestone May 27, 2022
@kraity
Copy link
Collaborator

kraity commented May 29, 2022

你好, 默认值是有的, 但是构造函数里被覆盖为null, 在fastjson2中有两种解决方法

  1. SubDTO 改为静态内部类
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class SubDTO2 {
            private String id;
            private String nickName;
            private ThiDTO thiDTO = new ThiDTO();
            private FouDTO fouDTO = new FouDTO();
        }
  1. SubDTO去掉@AllArgsConstructor注解
        @Data
        @NoArgsConstructor
        public class SubDTO {
            private String id;
            private String nickName;
            private ThiDTO thiDTO = new ThiDTO();
            private FouDTO fouDTO = new FouDTO();
        }

@kraity kraity mentioned this issue May 29, 2022
3 tasks
@kraity
Copy link
Collaborator

kraity commented May 29, 2022

你好, 默认值是有的, 但是构造函数里被覆盖为null, 在fastjson2中有两种解决方法

因为为SubDTO 非静态 内部类时, 创建SubDTO是通过构造是需要传入父TestDTO, 标记@AllArgsConstructor, fastjson2优先使用这个注解创建的构造参数, 那么在JSON文本中没有对应的fouDTO 时, 那么反序列化相当于null, 那么传入构造函数时, SubDTO.fouDTO 本有值的, 缺被覆盖为了null

wenshao added a commit that referenced this issue May 29, 2022
@q605450469
Copy link
Author

你好, 默认值是有的, 但是构造函数里被覆盖为null, 在fastjson2中有两种解决方法

因为为SubDTO 非静态 内部类时, 创建SubDTO是通过构造是需要传入父TestDTO, 标记@AllArgsConstructor, fastjson2优先使用这个注解创建的构造参数, 那么在JSON文本中没有对应的fouDTO 时, 那么反序列化相当于null, 那么传入构造函数时, SubDTO.fouDTO 本有值的, 缺被覆盖为了null

@kraity 感谢解答,目前已经可以达成期望结果。但是我们项目升级希望尽可能低的对原有代码做变更,请问之后会有计划在这方面做兼容或者优化吗?

@wenshao wenshao closed this as completed Jun 4, 2022
@wenshao wenshao reopened this Jun 5, 2022
@wenshao wenshao modified the milestones: 2.0.6, 2.0.7, 2.0.8 Jun 5, 2022
@wenshao wenshao modified the milestones: 2.0.8, 2.0.9 Jun 25, 2022
@wenshao wenshao modified the milestones: 2.0.9, 2.0.10 Jul 10, 2022
@wenshao wenshao modified the milestones: 2.0.10, 2.0.11 Jul 23, 2022
@wenshao wenshao modified the milestones: 2.0.11, 2.0.12 Jul 31, 2022
@wenshao wenshao modified the milestones: 2.0.12, 2.0.13 Aug 20, 2022
@wenshao wenshao modified the milestones: 2.0.13, 2.0.14, 2.0.15 Sep 10, 2022
@wenshao
Copy link
Member

wenshao commented Sep 18, 2022

要支持还比较麻烦,我还要再想想

@wenshao wenshao added the bug Something isn't working label Sep 19, 2022
@wenshao
Copy link
Member

wenshao commented Sep 19, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.15-SNAPSHOT/
问题已修复,请帮忙用2.0.15-SNAPSHOT验证,2.0.15版本预计在10月7日前发布

@wenshao
Copy link
Member

wenshao commented Oct 5, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.15
问题修复,请用新版本

@wenshao wenshao closed this as completed Oct 5, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants