Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

fastjson解析json字符串到java对象,当存在通配符继承Comparable时报堆栈溢出。 #1834

Closed
Viyond opened this issue May 8, 2018 · 3 comments
Labels
Milestone

Comments

@Viyond
Copy link

Viyond commented May 8, 2018

fastjson 版本,从1.2.35到最新1.2.46均试过均存在一下问题;
jdk版本:和jdk版本无关

问题描述:

public class FastJsonTest {

    public static void main(String[] args) {
        IndexQuery_Number query_number = new IndexQuery_Number();
        IndexQuery_Comparable query_comparable = new IndexQuery_Comparable();
        List<Integer> keys = Arrays.asList(1234);
        query_number.setKeys(keys);
        query_comparable.setKeys(keys);

        try{
            System.out.println("======= query with number =======");
            String json1 = JSON.toJSONString(query_number);
            System.out.println(json1);
            IndexQuery_Number queryNumber = JSON.parseObject(json1, new TypeReference<IndexQuery_Number>(){});
            System.out.println(queryNumber);

            System.out.println("\n======= query with comparable =======");
            String json2 = JSON.toJSONString(query_comparable);
            System.out.println(json2);
            IndexQuery_Comparable queryComparable = JSON.parseObject(json2, new TypeReference<IndexQuery_Comparable>(){});
            System.out.println(queryComparable);

        }catch (Throwable e){
            e.printStackTrace();
        }
    }

    static class IndexQuery_Comparable{
        List<? extends Comparable> keys;

        public List<? extends Comparable> getKeys() {
            return keys;
        }

        public void setKeys(List<? extends Comparable> keys) {
            this.keys = keys;
        }

        @Override
        public String toString() {
            return "IndexQuery{" +
                    "keys=" + keys +
                    '}';
        }
    }

    static class IndexQuery_Number{
        List<? extends Number> keys;

        public List<? extends Number> getKeys() {
            return keys;
        }

        public void setKeys(List<? extends Number> keys) {
            this.keys = keys;
        }

        @Override
        public String toString() {
            return "IndexQuery{" +
                    "keys=" + keys +
                    '}';
        }
    }

}

问题现象:
通配符继承Comparable会报堆栈溢出,通配符继承Number则完成正常解析。错误结果输入如下:
======= query with number =======
{"keys":[1234]}
IndexQuery{keys=[1234]}

======= query with comparable =======
{"keys":[1234]}
com.alibaba.fastjson.JSONException
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:665)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:633)
at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:42)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:661)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:633)
at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:42)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:661)
………………………………………………
………………………………………………
at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:42)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:661)
Caused by: java.lang.StackOverflowError
at java.lang.Exception.(Exception.java:84)
at java.lang.RuntimeException.(RuntimeException.java:80)
at com.alibaba.fastjson.JSONException.(JSONException.java:33)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:665)

@Viyond
Copy link
Author

Viyond commented May 29, 2018

请问下这个issue有人看吗?

@Viyond
Copy link
Author

Viyond commented Jun 25, 2018

RT

@wenshao wenshao added the bug label Aug 4, 2018
@wenshao wenshao added this to the 1.2.49 milestone Aug 4, 2018
@wenshao
Copy link
Member

wenshao commented Aug 5, 2018

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

@wenshao wenshao closed this as completed Aug 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants