fastjson 有父类继承的情况下,且父类是泛型,子类解析出问题,

pwuypxnk  于 2021-11-27  发布在  Java
关注(0)|答案(6)|浏览(483)

有父类继承的情况下,且父类是泛型,子类解析出问题,比如某一个字段每次编译运行都不是一个类型,有时候是JSON,有时候是JSONARRAY。

deyfvvtc

deyfvvtc2#

能提供重现问题的代码么?

e37o9pze

e37o9pze3#

json:
{\"success\":true,\"errorMsg\":{},\"data\":[\"111118\"],\"singleData\":\"444444441dsadasdasd\"}

parent abstract class:

public abstract class BaseResponse<T> {
    private boolean success = true;
    private Map<String, String> errorMsg = new HashMap();
    private List<T> data;

    public BaseResponse() {
    }

    public List<T> getData() {
        return this.data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Map<String, String> getErrorMsg() {
        return this.errorMsg;
    }

    public void setErrorMsg(Map<String, String> errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Set<String> errorCodes() {
        return this.errorMsg.keySet();
    }

    public Collection<String> errorMsgs() {
        return this.errorMsg.values();
    }

    public void addError(String code, String msg) {
        this.setSuccess(false);
        this.errorMsg.put(code, msg);
    }

    public void addError(Map<String, String> errorMsgMap) {
        this.setSuccess(false);
        this.errorMsg.putAll(errorMsgMap);
    }

    public T getSingleData() {
        return this.data != null && !this.data.isEmpty()?this.data.get(0):null;
    }

    public void setData(T data) {
        if(this.data == null) {
            this.data = new ArrayList();
        }

        this.data.add(data);
    }

    public String buildMsg(String sep) {
        StringBuilder sb = new StringBuilder();
        if(!this.errorMsg.isEmpty()) {
            Iterator var3 = this.errorMsg.entrySet().iterator();

            while(var3.hasNext()) {
                Entry entry = (Entry)var3.next();
                sb.append((String)entry.getKey()).append(":");
                sb.append((String)entry.getValue()).append(sep);
            }
        }

        return sb.toString();
    }

    public String toString() {
        return "result:" + this.success + ";" + this.buildMsg(";") + "data:" + (this.getData() != null?ToStringBuilder.reflectionToString(this.getData()):"");
    }
}
public class StringResponse extends BaseResponse<String> {
    public StringResponse() {
    }

    public StringResponse(String data) {
        this.setData(data);
    }

    public StringResponse(List<String> dataList) {
        this.setData(dataList);
    }
}

运行

public class Test {

    public static void main(String[] args) {
        String str = "{\"success\":true,\"errorMsg\":{},\"data\":[\"11111111111112222\"],\"singleData\":\"11111111111112222\"}";
        StringResponse result = JSON.parseObject(str, StringResponse.class);
        JSONObject o = JSON.parseObject(str);
        System.out.println(result.getSingleData());
 }
}
44u64gxh

44u64gxh4#

多运行几次 会出现差别

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.8.sec01</version>
        </dependency>
3hvapo4f

3hvapo4f5#

@wenshao 代码已经给出了

jtoj6r0c

jtoj6r0c6#

请问解决了没啊,这父类泛型反序列化能否支持,但是当前子类是没有泛型的。。。。

相关问题