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

使用fastjson1.2.21时,在反序列化泛型对象类型时会报NullPointerException的错误 #918

Closed
Viyond opened this issue Nov 23, 2016 · 2 comments
Labels
Milestone

Comments

@Viyond
Copy link

Viyond commented Nov 23, 2016

环境说明:
jdk:1.8
fastjson:1.2.21

能够重现的case程序如下:

序列化执行类

public class JsonSerializer {
    private static final String NAMESPACE = "teslaSpace";
    private static final Charset CHARSET = Charset.forName("UTF-8");

    public static <T> void serialize(T obj, OutputStream out) {
        setTeslaJson();
        JSONWriter writer = null;
        try {
            writer = new JSONWriter(new OutputStreamWriter(out, CHARSET.newEncoder().onMalformedInput(CodingErrorAction.IGNORE)));
            writer.config(SerializerFeature.QuoteFieldNames, true);
            writer.config(SerializerFeature.SkipTransientField, true);
            writer.config(SerializerFeature.SortField, true);
            writer.config(SerializerFeature.WriteEnumUsingToString, false);
            writer.config(SerializerFeature.WriteClassName, true);
            writer.config(SerializerFeature.DisableCircularReferenceDetect, true);
            writer.writeObject(obj);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public static <T> T deserialize(byte[] in) {
        setTeslaJson();
        return (T) JSON.parse(in, 0, in.length, CHARSET.newDecoder(), Feature.AllowArbitraryCommas,
                Feature.IgnoreNotMatch, Feature.SortFeidFastMatch, Feature.DisableCircularReferenceDetect,
                Feature.AutoCloseSource);
    }


    private static void setTeslaJson() {
        if (!JSON.DEFAULT_TYPE_KEY.equals(NAMESPACE)) {
            JSON.setDefaultTypeKey(NAMESPACE);
        }
        if (!SerializeConfig.globalInstance.getTypeKey().equals(NAMESPACE)){
            SerializeConfig.globalInstance.setTypeKey(NAMESPACE);
        }
    }

}

### 对象类如下public class Person {
    private String name;
    private Integer age;

    public Person(){}

    public Person(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Result<<TYPE>> {//假设泛型类型为TYPE, github页面样式错误,不能显示出
    private TYPE data;

    public Result(){}

    public TYPE getData() {
        return data;
    }

    public void setData(TYPE data) {
        this.data = data;
    }
}

public class ApiResponse<<TYPE>> {//假设泛型类型为TYPE, github页面样式错误,不能显示出
    private Result<<TYPE>> result; //假设泛型类型为TYPE, github页面样式错误,不能显示出

    public ApiResponse(){}

    public Result<TYPE> getResult() {
        return result;
    }

    public void setResult(Result<TYPE> result) {
        this.result = result;
    }
}

### 测试类如下public class JsonSerialTest {

    public static void main(String[] args) {
        try{
            Person person = new Person("test1", 10);
            serialTest(person);

            ApiResponse<Object> apiResponse = new ApiResponse<Object>();
            Result<Object> result = new Result<Object>();
            result.setData(new Object());
            apiResponse.setResult(result);

            serialTest(apiResponse);

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

    private static void serialTest(Object object){
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try{
            JsonSerializer.serialize(object, bos);

            Object desRes = JsonSerializer.deserialize(bos.toByteArray());
            System.out.println("反序列化的结果为:" + desRes);
        }finally {
            try{
                bos.close();
            }catch (IOException e){
            }
        }
    }
}

程序输出结果如下:

反序列化的结果为:Person{name='test1', age=10}
java.lang.NullPointerException
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:51)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:689)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:570)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:189)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:60)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:549)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:189)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:185)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:355)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1309)
at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1275)
at com.alibaba.fastjson.JSON.parse(JSON.java:178)
at com.alibaba.fastjson.JSON.parse(JSON.java:162)
at tesla.client.test.json.JsonSerializer.deserialize(JsonSerializer.java:49)
at tesla.client.test.json.JsonSerialTest.serialTest(JsonSerialTest.java:34)
at tesla.client.test.json.JsonSerialTest.main(JsonSerialTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

@wenshao 帮忙查看,非常感谢!!

@Viyond Viyond changed the title fastjson1.2.21反序列化泛型报NullPointerException错误 使用fastjson1.2.21时,在反序列化泛型对象类型时会报NullPointerException的错误 Nov 23, 2016
@orika
Copy link

orika commented Nov 23, 2016

DING

@wenshao wenshao added the bug label Nov 27, 2016
@wenshao wenshao added this to the 1.2.22 milestone Nov 27, 2016
wenshao added a commit that referenced this issue Dec 5, 2016
@wenshao
Copy link
Member

wenshao commented Dec 10, 2016

问题已经修复,请使用1.2.22版本

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

3 participants