Serialize - Deserialize

Topics: graph, serialization
Nov 21, 2008 at 11:21 AM
Hi,

if I have a simple Adjacency Graph (AdjacencyGraph<int, Edge<int>>) can I Serialize and then Deserialize it?

Can I use .net Serialization?

Thanks.
Nov 21, 2008 at 8:42 PM
This should work in 3.0
Nov 22, 2008 at 10:30 AM
Does it work for undirect Path?
Nov 22, 2008 at 2:19 PM
Hi serialize and deserialize using the functions below. When I deserialize it returns null value. Why?

public static class FileSerializer

{
public static void Serialize( string filename, object objectToSerialize )
{
if (objectToSerialize == null)
throw new ArgumentNullException("objectToSerialize cannot be null");
Stream stream = null;
try
{
stream = File.Open(filename, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, objectToSerialize);
}
finally
{
if (stream != null)
stream.Close();
}
}

public static T Deserialize<T>( string filename )
{
T objectToSerialize = default(T);
Stream stream = null;
try
{
stream = File.Open(filename, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();
objectToSerialize = (T)bFormatter.Deserialize(stream);
}
catch (Exception err)
{
//MessageBox.Show("The application failed to retrieve the inventory - " + err.Message);
}
finally
{
if (stream != null)
stream.Close();
}
return objectToSerialize;
}
}

Nov 23, 2008 at 1:00 AM

You can use the SerializationExtensions class to take care of this.

 

var g = ...; // some graph
// serialize
var stream = new MemoryStream();
g.SerializeToBinary(stream);

// deserializestream.Position = 0;
var result = stream.DeserializeFromBinary<TVertex, TEdge, TGraph>();

 

Nov 23, 2008 at 10:42 AM
Using this:

var g = new AdjacencyGraph<int, Edge<int>>();
var stream = new MemoryStream();
g.SerializeToBinary(stream);

returns this error:

Error 16 'QuickGraph.AdjacencyGraph<int,QuickGraph.Edge<int>>' does not contain a definition for 'SerializeToBinary' and no extension method 'SerializeToBinary' accepting a first argument of type 'QuickGraph.AdjacencyGraph<int,QuickGraph.Edge<int>>' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\All Users.WINDOWS\Documenti\Cotrap\CotrapClassi\Funzioni.cs 642 15 CotrapClassi


Can you help me?
Nov 23, 2008 at 5:35 PM
SerializeToBinary is an extension method from the SerializationExtensions class. I think it lives under QuickGraph.Serialization, but you can always use Ctrl + . to figure it out.