Skip to content
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

[BUG]升级到fastjson2后,反序列化丢失信息 #454

Closed
MengSucess opened this issue Jun 10, 2022 · 4 comments
Closed

[BUG]升级到fastjson2后,反序列化丢失信息 #454

MengSucess opened this issue Jun 10, 2022 · 4 comments
Labels
bug Something isn't working
Milestone

Comments

@MengSucess
Copy link

MengSucess commented Jun 10, 2022

问题描述

不同的注解方法结果不同。

环境信息

  • OS信息: Windows 11
  • JDK信息:Oracle JDK 1.8
  • 版本信息:fastjson2 2.0.6

重现步骤

如何操作可以重现该问题:

  1. 在不同的fastjson.jar 下运行下面的代码重现
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;

import java.io.Serializable;
import java.util.List;

/**
 * fastjson2 新版本测试
 */
public class JsonTest {

    /**
     * fastjson2 这种注解方式可以正常工作
     */
    public class User implements Serializable {

        @JSONField(name = "id")
        private int id;
        @JSONField(name = "title")
        private String title;
        @JSONField(name = "parent_id")
        private Integer parentId;
        @JSONField(name = "display_level")
        private Integer displayLevel;
        @JSONField(name = "item_index")
        private Integer itemIndex;
        @JSONField(name = "child_count")
        private Integer childCount;
        @JSONField(name = "phase_id")
        private Integer phaseId;
        @JSONField(name = "del_state")
        private int delState;
        @JSONField(name = "path")
        private String path;
        @JSONField(name = "update_time")
        private Long updateTime;


        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }


        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }


        public Integer getParentId() {
            return parentId;
        }

        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }


        public Integer getDisplayLevel() {
            return displayLevel;
        }

        public void setDisplayLevel(Integer displayLevel) {
            this.displayLevel = displayLevel;
        }


        public Integer getItemIndex() {
            return itemIndex;
        }

        public void setItemIndex(Integer itemIndex) {
            this.itemIndex = itemIndex;
        }


        public Integer getChildCount() {
            return childCount;
        }

        public void setChildCount(Integer childCount) {
            this.childCount = childCount;
        }


        public Integer getPhaseId() {
            return phaseId;
        }

        public void setPhaseId(Integer phaseId) {
            this.phaseId = phaseId;
        }


        public int getDelState() {
            return delState;
        }

        public void setDelState(int delState) {
            this.delState = delState;
        }


        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }


        public Long getUpdateTime() {
            return updateTime;
        }

        public void setUpdateTime(Long updateTime) {
            this.updateTime = updateTime;
        }
    }

    /**
     * fastjson2 这种方式会丢失字段信息。
     * fastjson1 不会丢失信息
     */
    public class User2 implements Serializable {
        private int id;
        private String title;
        private Integer parentId;
        private Integer displayLevel;
        private Integer itemIndex;
        private Integer childCount;
        private Integer phaseId;
        private int delState;
        private String path;
        private Long updateTime;

        @JSONField(name = "id")
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @JSONField(name = "title")
        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        @JSONField(name = "parent_id")
        public Integer getParentId() {
            return parentId;
        }

        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }

        @JSONField(name = "display_level")
        public Integer getDisplayLevel() {
            return displayLevel;
        }

        public void setDisplayLevel(Integer displayLevel) {
            this.displayLevel = displayLevel;
        }

        @JSONField(name = "item_index")
        public Integer getItemIndex() {
            return itemIndex;
        }

        public void setItemIndex(Integer itemIndex) {
            this.itemIndex = itemIndex;
        }

        @JSONField(name = "child_count")
        public Integer getChildCount() {
            return childCount;
        }

        public void setChildCount(Integer childCount) {
            this.childCount = childCount;
        }

        @JSONField(name = "phase_id")
        public Integer getPhaseId() {
            return phaseId;
        }

        public void setPhaseId(Integer phaseId) {
            this.phaseId = phaseId;
        }

        @JSONField(name = "del_state")
        public int getDelState() {
            return delState;
        }

        public void setDelState(int delState) {
            this.delState = delState;
        }

        @JSONField(name = "path")
        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        @JSONField(name = "update_time")
        public Long getUpdateTime() {
            return updateTime;
        }

        public void setUpdateTime(Long updateTime) {
            this.updateTime = updateTime;
        }
    }

    public static void main(String[] args) {
        String text = "[{\"child_count\":8,\"del_state\":0,\"display_level\":2,\"id\":54,\"item_index\":46,\"parent_id\":1,\"path\":\"/1/54/\",\"phase_id\":1,\"title\":\"八年级上\",\"update_time\":1596423978669}]";
        List<User> list1 = JSON.parseArray(text, User.class);
        if (list1 != null && list1.size() > 0) {
            User user = list1.get(0);
            System.out.println(JSON.toJSONString(user));
            //这里的输出结果如下
            // {"child_count":8,"del_state":0,"display_level":2,"id":54,"item_index":46,"parent_id":1,"path":"/1/54/","phase_id":1,"title":"八年级上","update_time":1596423978669}
        }


        text = "[{\"child_count\":8,\"del_state\":0,\"display_level\":2,\"id\":54,\"item_index\":46,\"parent_id\":1,\"path\":\"/1/54/\",\"phase_id\":1,\"title\":\"八年级上\",\"update_time\":1596423978669}]";
        List<User2> list2 = JSON.parseArray(text, User2.class);
        if (list2 != null && list2.size() > 0) {
            User2 user2 = list2.get(0);
            System.out.println(JSON.toJSONString(user2));
            //fastjson2 这里的输出结果如下,
            //{"del_state":0,"id":54,"path":"/1/54/","title":"八年级上"}
            //而fastjson1输出的结果是完整的,
            // {"child_count":8,"del_state":0,"display_level":2,"id":54,"item_index":46,"parent_id":1,"path":"/1/54/","phase_id":1,"title":"八年级上","update_time":1596423978669}
        }

    }
}

运行环境:
idea maven 项目,使用了spring framework,struts2

@MengSucess MengSucess added the bug Something isn't working label Jun 10, 2022
wenshao added a commit that referenced this issue Jun 10, 2022
@wenshao
Copy link
Member

wenshao commented Jun 10, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.7-SNAPSHOT/
问题已经修复,请帮忙用2.0.7-SNAPSHOT版本验证

@wenshao wenshao added this to the 2.0.7 milestone Jun 10, 2022
@MengSucess
Copy link
Author

好的,我先测试下,原来测试的使用兼容1的fastjson2和独立的fastjson2都存在此问题。下面2个地址的pom引用之后都出同样的问题
https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2/2.0.6
https://mvnrepository.com/artifact/com.alibaba/fastjson/2.0.6

是不是应该推荐使用独立的 fastjson2,兼容1的fastjson2只是过渡期间的版本吧,以后不会一直维护下去吧?

@wenshao
Copy link
Member

wenshao commented Jun 10, 2022

兼容的会一直维护下去

@wenshao
Copy link
Member

wenshao commented Jun 11, 2022

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

@wenshao wenshao closed this as completed Jun 11, 2022
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

2 participants