dubbo-hessian-lite icon indicating copy to clipboard operation
dubbo-hessian-lite copied to clipboard

wrong generic type for decoding

Open wongoo opened this issue 4 years ago • 1 comments

for type List<Integer[]> , the decoded type will be List<List>, which will cause jackson can't serialize the object, and get the error java.util.ArrayList cannot be cast to [Ljava.lang.Object;

the following code will regenerate the error:


import com.alibaba.com.caucho.hessian.io.HessianInput;
import com.alibaba.com.caucho.hessian.io.HessianOutput;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) throws IOException {

        DemoData data = new DemoData();
        List<Integer[]> list = new ArrayList<>();
        list.add(new Integer[]{1, 2, 3});
        list.add(new Integer[]{4, 5, 6});
        data.setList(list);

        data.setInts(new Integer[]{7, 8, 9});

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(data);
        System.out.println(json);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HessianOutput output = new HessianOutput(out);
        output.writeObject(data);

        ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray());
        HessianInput input = new HessianInput(is);
        Object o = input.readObject();
        System.out.println(o);

        json = objectMapper.writeValueAsString(o);
        System.out.println(json);
    }

}

class DemoData implements Serializable {
    Integer[] ints;
    List<Integer[]> list;

    public Integer[] getInts() {
        return ints;
    }

    public void setInts(Integer[] ints) {
        this.ints = ints;
    }

    public List<Integer[]> getList() {
        return list;
    }

    public void setList(List<Integer[]> list) {
        this.list = list;
    }
}

wongoo avatar Oct 12 '20 05:10 wongoo