Serialisieren mehrerer Klassen scheitert - warum?

Dann lies dir die Dokumentation durch und stell es nach deinen Bedürfnissen ein.
 
Also 1. Xstream scheitert auch und kann die Klasse nicht serialisieren:

Ich versuch jetzt nochmal ein Beispiel:

Wir haben einen mit zyklen behafteten Graphen, den ich durch folgende 3 Klassen abbilde:

Code:
public class DtNetz implements Serializable {

    private static final long serialVersionUID = 1L;
    
    private HashMap<String, DtHalt> haltlist = new HashMap<String, DtHalt>();
    //Der Stirng ist eine "ID"....
    
    public DtNetz() {

    }    
}

Hier die Knoten
Code:
public class DtHalt implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private String attribut_1;
    private String attribut_2;
    private String attribut_3;
    
    private transient GeoPosition geoPosition;
    
    private HashMap<DtHalt, DtKante> hm_connected_to = new HashMap<DtHalt, DtKante>();
    
    public DtHalt() {        
    }
}

und noch die Kanten

Code:
public class DtKante implements Serializable {
    
    private Integer distance = null;
    
    private ArrayList<String> al_1 = new ArrayList();
    
    public void DtKante() {
    }

}

Wenn ich diesen Graphen befülle... ca 16.000 Knoten, dann kann ich das ganze nicht serialisieren.... Ich serialisiere natürlich nur das Netz und Java hangelt sich ja dann automatisch durch.
Und bei den Halten wird dann irgendwie immer im Kreis gerannt oder so... ich hab keine Ahnung!
Jedenfalls kann XStream diesen Graphen auch nicht serialisieren
 
Hallo,

ich meinte eigentlich, dass du ein minimales lauffähiges Beispiel angeben solltest.

Ansonsten würde ich dir empfehlen den Graphen in einer anderen Form beispielsweise als Adjazenz Liste / Matrix abzuspeichern.

Wobei, es könnte sein das du selber einen Serialisierungs-Bug eingebaut hast... hast du eigene writeObject, writeReplace Methoden implementiert?

Gruß Tom
 
XStream kann von sich aus bei entsprechender Konfiguration komplexe Graphen serialisieren. Das habe ich in meiner Diplomarbeit so verwendet. Ich weiß ehrlich gesagt nicht wo das Problem ist einmal die Dokumentation zu lesen und die entsprechenden Einstellungen vorzunehmen.
 
OK - diese Seite habe ich gefunden:

http://xstream.codehaus.org/graphs.html

gut - denke das hat mit meinem Thema zu tun.

Nur leider funktioniert es - egal welche Einstellung - NICHT

Code:
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:78)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:157)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:148)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:118)
    at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:129)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:100)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:58)
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:68)
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:78)
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:63)
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.writeItem(AbstractCollectionConverter.java:64)
    at com.thoughtworks.xstream.converters.collections.MapConverter.marshal(MapConverter.java:57)
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:68)
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:78)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:157)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:148)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:118)
    at

Vielleicht wärst du zeja ja so gnädig und würdest mir helfen - da du es ja schon mal offensichtlich gemacht hast.

Was habe ich bis jetzt konfiguriert... hmm naja
Code:
fos = new FileOutputStream("Netz.xml");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            XStream xstream = new XStream();
            xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
            xstream.toXML(hm_Halte, bos);
so lasse ich das in eine Datei schreiben... ich habe alle REFERENZ-Modelle durchprobiert, aber bringt irgendwie nix.

Ich weiss auch nicht, was du mit Konfigurieren meinst - überall auf der Seite steht im wesentlichen, dass XStream alles out of the box kann. Wenn du mir nur einen Tipp geben könntest, zu welchem Thema ich weitersuchen sollte - einfach so in der API Blättern halte ich für uneffektiv!
 
Vielleicht ist bei dir noch was anders, aber probier das mal:

Java:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.XStream;

public class XStreamTest {

	public static void main(String[] args) throws IOException {
		final Graph graph = new Graph();

		final Node a = graph.addNode("a");
		final Node b = graph.addNode("b");
		final Node c = graph.addNode("c");

		new Edge(a, b);
		new Edge(b, c);
		new Edge(c, a);

		final FileOutputStream fos = new FileOutputStream("Netz.xml");
		final BufferedOutputStream bos = new BufferedOutputStream(fos);
		final XStream xstream = new XStream();
		xstream.alias("edge", Edge.class);
		xstream.alias("node", Node.class);
		xstream.alias("graph", Graph.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		xstream.toXML(graph, bos);
		bos.close();
	}

	public static final class Graph {

		private final Map<String, Node> graph = new HashMap<String, Node>();

		public Node addNode(String name) {
			final Node node = new Node(name);
			graph.put(name, node);
			return node;
		}
	}

	public static final class Edge {

		private final Node inNode;
		private final Node outNode;

		public Edge(final Node inNode, final Node outNode) {
			this.inNode = inNode;
			this.outNode = outNode;

			inNode.addOutEdge(this);
			outNode.addInEdge(this);
		}

	}

	public static final class Node {

		private final List<Edge> inEdges = new ArrayList<Edge>();
		private final List<Edge> outEdges = new ArrayList<Edge>();

		private final String name;

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

		public final void addInEdge(Edge edge) {
			this.inEdges.add(edge);
		}

		public final void addOutEdge(Edge edge) {
			this.outEdges.add(edge);
		}

	}
}
 
Zurück