x-stream / xstream

Serialize Java objects to XML and back again.
http://x-stream.github.io
Other
749 stars 227 forks source link

Convert in mother class won't work #329

Closed quantrpeter closed 1 year ago

quantrpeter commented 1 year ago

Hi I try to serialize an object with ArrayList to my own class, when add converter to the mother class, it won't work

public class Data {

    public ArrayList<Vertex> vertices = new ArrayList();

    public static void main(String str[]) {
        Data data = new Data();
        data.vertices.add(new NorGate("nor 1"));
        data.save(new File("data.xml"));
    }

    public void save(File file) {
        XStream xstream = new XStream();
        xstream.alias("data", Data.class);
        xstream.alias("norGate", NorGate.class);
        xstream.autodetectAnnotations(true);
        String xml = xstream.toXML(this);
        try {
            IOUtils.write(xml, new FileOutputStream(file), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Data load(File file) {
        try {
            XStream xstream = new XStream(new StaxDriver());
            xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
            xstream.allowTypesByWildcard(new String[]{"hk.quantr.logic.*"});
            xstream.alias("Data", Data.class);
            Data data = (Data) xstream.fromXML(file);
            return data;
        } catch (Exception ex) {
            file.delete();
            Data data = new Data();
            data.save(file);
            return data;
        }
    }
}
public class NorGate extends Vertex {

    public NorGate(String name) {
        super(name);
        width = 6;
        height = 4;
        inputPins = 2;
        setSize(width, height);

        super.setInput();
        super.setOutput();

        properties.put("Name", name);
        properties.put("No. Of Inputs", 2);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        BasicStroke stroke = new BasicStroke(this.gridSize / 5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        LogicGateDrawer.drawNorGate(g, this, this.getX() * gridSize, this.getY() * gridSize, this.gridSize, stroke);
    }
}
@XStreamConverter(value = VertexConverter2.class, strings = {"value"})
public class Vertex extends JComponent {

    public boolean isSelected;
    public int gridSize = 10;
    public String name;
    public int height;
    public int width;
    public int inputPins;
    public ArrayList<Input> inputs = new ArrayList();
    public Output output;
    public LinkedHashMap<String, Object> properties = new LinkedHashMap();

    public Vertex(String name) {
        this.name = name;
    }

    public void setInput() {
        int cnt = 1;
        inputs.clear();
        for (int i = -inputPins / 2; i <= inputPins / 2; i++) {
            if (inputPins % 2 == 0 && i == 0) {
                continue;
            }
            Input in = new Input(this.name + " input " + cnt++);
            in.setLocation(this.getX(), this.getY() + getHeight() / 2 + i);
            inputs.add(in);
        }
    }

    public void setOutput() {
        if (true) {
            output = new Output(this.name + " output");
            output.setLocation(this.getX() + this.width, this.getY() + getHeight() / 2);
        }
    }

    @Override
    public void setLocation(int x, int y) {
        super.setLocation(x, y);
        setInput();
        setOutput();
    }

    @Override
    public void paint(Graphics g) {
        if (isSelected) {
            g.setColor(Color.blue);
            g.drawRect(getX() * gridSize, getY() * gridSize, getWidth() * gridSize, getHeight() * gridSize);
        } else {
//          g.setColor(Color.white);
//          g.clearRect(getX()-2, getY()-2, getWidth()+4, getHeight()+4);
        }
    }
}
public class VertexConverter2 implements SingleValueConverter {

    @Override
    public String toString(Object o) {
        return "hello 2";
    }

    @Override
    public Object fromString(String str) {
        return null;
    }

    @Override
    public boolean canConvert(Class type) {
        return true;
    }

}

thanks

joehni commented 1 year ago

Hi,

well, you register a converter that claims to handle any type (canConvert returns true), i.e. it will also be responsible for type Data.

Regards, Jörg

quantrpeter commented 1 year ago

thanks