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]JSON.parseObject做泛型解析时出现解析成JSONObject的问题 #1676

Closed
cbib-guoguifang opened this issue Jul 24, 2023 · 6 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@cbib-guoguifang
Copy link

问题描述

在使用JSON.parseObject做解析时,如果是包含泛型的时候解析泛型有时候会正确解析,有时候会解析成JSONObject,但是使用fastjson1时每次都是正确的

环境信息

  • OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson2 2.0.37]

重现步骤

按以下代码执行,多次执行会出现截图中不同的结果,有时候解析是正确的Test,有时候解析是JSONObject

ParameterizedType parameterizedType = new ParameterizedType() {
            @Override
            public Type[] getActualTypeArguments() {
                return new Type[] {Test.class};
            }
            
            @Override
            public Type getRawType() {
                return com.baomidou.mybatisplus.extension.plugins.pagination.Page.class;
            }
            
            @Override
            public Type getOwnerType() {
                return null;
            }
        };
        String s = "{\"current\":1,\"hitCount\":false,\"optimizeCountSql\":true,\"orders\":[],\"pages\":1,\"records\":[{\"name\": \"test\"}],\"searchCount\":true,\"size\":10,\"total\":1}";
        Page fastJson2PageObject = JSON.parseObject(s, parameterizedType);
        System.out.println(fastJson2PageObject.getRecords().get(0).getClass());
        
        Page fastJson1PageObject = com.alibaba.fastjson.JSON.parseObject(s, parameterizedType);
        System.out.println(fastJson1PageObject.getRecords().get(0).getClass());

期待的正确结果

跟FastJson1一样的解析结果,每次都能正确解析泛型类型

附加信息

image
image
image

@cbib-guoguifang cbib-guoguifang added the bug Something isn't working label Jul 24, 2023
@cbib-guoguifang cbib-guoguifang changed the title [BUG] [BUG]JSON.parseObject做泛型解析时出现解析成JSONObject的问题 Jul 24, 2023
wenshao added a commit that referenced this issue Jul 24, 2023
@cbib-guoguifang
Copy link
Author

cbib-guoguifang commented Jul 28, 2023

请问一下这个问题啥时候能修复呢,本来项目打算将fastjson1升级到fastjson2,结果升级完测试的时候发现这个致命问题,现在升也升上不去,还原下次再升级又得重新测试一遍,麻烦修复一下这个问题,谢谢 @wenshao

@wenshao
Copy link
Member

wenshao commented Jul 28, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.38-SNAPSHOT/ 问题已修复,请用2.0.38-SNAPSHOT版本帮忙验证,预计在这个周末发布新版本

@wenshao wenshao added this to the 2.0.38 milestone Jul 28, 2023
@wenshao wenshao added the fixed label Jul 28, 2023
wenshao added a commit that referenced this issue Jul 28, 2023
@cbib-guoguifang
Copy link
Author

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.38-SNAPSHOT/ 问题已修复,请用2.0.38-SNAPSHOT版本帮忙验证,预计在这个周末发布新版本

刚测试了一下问题依旧,并没有修复,麻烦再看看,谢谢

@wenshao wenshao modified the milestones: 2.0.38, 2.0.39 Jul 29, 2023
@wenshao wenshao removed the fixed label Jul 29, 2023
@wenshao
Copy link
Member

wenshao commented Jul 29, 2023

@wenshao wenshao modified the milestones: 2.0.39, 2.0.40 Aug 15, 2023
@cbib-guoguifang
Copy link
Author

cbib-guoguifang commented Aug 25, 2023

@wenshao 这个问题我使用你最新的2.0.40-SNAPSHOT版本还是依旧有问题,经过我多次验证,发现在实现一个接口的时候,如果set方法返回值是接口类型及泛型时会出现这个问题,在你的testcase上复现了一下问题,比较代码如下
`public class Issue1676 {

public static void main(String[] args) {
    test1();
    test2();
}

private static void test1() {
    ParameterizedType parameterizedType = new ParameterizedType() {
        @Override
        public Type[] getActualTypeArguments() {
            return new Type[] {Bean.class};
        }
        
        @Override
        public Type getRawType() {
            return PageImpl.class;
        }
        
        @Override
        public Type getOwnerType() {
            return null;
        }
    };
    String s = "{\"current\":1,\"hitCount\":false,\"optimizeCountSql\":true,\"orders\":[],\"pages\":1,\"records\":[{\"name\": \"test\"}],\"searchCount\":true,\"size\":10,\"total\":1}";
    PageImpl fastJson2PageObject = com.alibaba.fastjson2.JSON.parseObject(s, parameterizedType);
    System.out.println("实现IPage接口的fastJson2解析结果:");
    System.out.println(Objects.equals(Bean.class, fastJson2PageObject.getRecords().get(0).getClass()));
    
    PageImpl fastJson1PageObject = com.alibaba.fastjson.JSON.parseObject(s, parameterizedType);
    System.out.println("实现IPage接口的fastJson1解析结果:");
    System.out.println(Objects.equals(Bean.class, fastJson1PageObject.getRecords().get(0).getClass()));
}

private static void test2() {
    ParameterizedType parameterizedType = new ParameterizedType() {
        @Override
        public Type[] getActualTypeArguments() {
            return new Type[] {Bean.class};
        }
        
        @Override
        public Type getRawType() {
            return Page.class;
        }
        
        @Override
        public Type getOwnerType() {
            return null;
        }
    };
    
    String s = "{\"current\":1,\"hitCount\":false,\"optimizeCountSql\":true,\"orders\":[],\"pages\":1,\"records\":[{\"name\": \"test\"}],\"searchCount\":true,\"size\":10,\"total\":1}";
    Page fastJson2PageObject = com.alibaba.fastjson2.JSON.parseObject(s, parameterizedType);
    System.out.println("不实现IPage接口的fastJson2解析结果:");
    System.out.println(Objects.equals(Bean.class, fastJson2PageObject.getRecords().get(0).getClass()));
}

public static class Bean {
    
    private String name;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}

public interface IPage<T> extends Serializable {
    
    List<T> getRecords();
    
    // 问题出现在这里,如果这里返回结果是IPage<T>,那么fastjson2就会解析有问题,但fastjson1不会有问题;如果返回结果是void也不会有问题
    IPage<T> setRecords(List<T> records);
    
}

public static class PageImpl<T> implements IPage<T> {
    
    protected List<T> records;
    
    @Override
    public List<T> getRecords() {
        return this.records;
    }
    
    @Override
    public PageImpl<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }
}

public static class Page<T> {
    private List<T> records;

    public List<T> getRecords() {
        return records;
    }

    public Page<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }
}

}`
结果截图:
image

@wenshao
Copy link
Member

wenshao commented Sep 3, 2023

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

@wenshao wenshao closed this as completed Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants