2

I have a similar Problem to Serializing a HashSet

I have a Class with a Member of type Dictionary<String,HashSet<T>>

When i serialize the Object with BinaryFormatter, and then deserialize it, it comes up empty.

I don't know how, or where to call the workaround posted here.

Any Hints? thanks in advance.

edit: i tried to convert the hashset to a list, as one of the comments in the other thread suggested.

the object looks like this:

public class THashSet : HashSet<T> , ISerializable
{

    public THashSet(SerializationInfo info, StreamingContext context)
    {
        var list = (List<T>)info.GetValue("hashset", typeof(List<T>));     
        foreach (T t in list)
            this.Add(t);
    }

    public override void GetObjectData(SerializationInfo info,StreamingContext context)
    {
        info.AddValue("hashset", this.ToList<T>());
    }

when the object containing the THashSet is deserialized (and the constructor is called), the list is recovered correctly, as i see in the debugger.

but after the serializer is done, the object only contains an empty hashset.

Community
  • 1
  • 1
Dr. Wummi
  • 111
  • 7
  • 2
    Use some other serializer such as [Json.Net](http://json.codeplex.com/). It works well for your case. – L.B Oct 12 '12 at 07:28
  • sadly i can't use another serializer as this is a small detail in a rather huge project. everything else is serializing well – Dr. Wummi Oct 12 '12 at 07:55

1 Answers1

1

Suppose for your T object you haven't override methods GetHashCode and Equals. You need to do it.

UPD: All depends from your object implementation. And object is not so easy as you say. Your sitation, object:

[Serializable]
public class DataClass
{       
    public DataClass()
    {
    }

    public DataClass(string name, string description)
    {
        Name = name;
        Description = description;

        this.Dictionary = new Dictionary<string, HashSet<DataClass>>();
    }

    public string Name { get; set; }

    public string Description { get; set; }

    public Dictionary<string, HashSet<DataClass>> Dictionary { get; set; }
}

And serialization/deserialization code:

DataClass dataClass = new DataClass("name", "descr");
dataClass.Dictionary.Add("key1", new HashSet<DataClass>() { new DataClass("sample11", "descr11"), new DataClass("sample12", "descr12") });
dataClass.Dictionary.Add("key2", new HashSet<DataClass>() { new DataClass("sample21", "descr21"), new DataClass("sample22", "descr22") });
dataClass.Dictionary.Add("key3", new HashSet<DataClass>() { new DataClass("sample31", "descr31"), new DataClass("sample32", "descr32") });

byte[] serialized;
var formatter = new BinaryFormatter();

using (MemoryStream stream = new MemoryStream())
{
    formatter.Serialize(stream, dataClass);
    serialized = stream.ToArray();
}
using (MemoryStream streamDeserial = new MemoryStream())
{
    streamDeserial.Write(serialized, 0, serialized.Length);
    streamDeserial.Seek(0, SeekOrigin.Begin);
    var dictDeserial = formatter.Deserialize(streamDeserial) as DataClass;
} 

This code works well.

Regfor
  • 8,515
  • 1
  • 38
  • 51
  • i implemented those functions, but without results – Dr. Wummi Oct 12 '12 at 08:26
  • @Dr.Wummi Could you please post your T object or similar object with these functions implementation. Having these objects, I also could try to reproduce your problem by me. – Regfor Oct 12 '12 at 09:32
  • the object only contains public string members, and a few constructors that only set some of the strings to default values. this object serializes fine in the other methods it is used (ArrayLists, Lists, and dictionaries) – Dr. Wummi Oct 12 '12 at 10:49
  • thanks for your answer. your method works as expected. i don't know why but when the object has a member like `public Dictionary> Dictionary { get; set; }` it works, but if i have a custom class that simply derives from the hashset like `public class THashset : HashSet` and the object has a member like `public Dictionary Dictionary { get; set; }` it doesn't.. but thanks, this fixed the problem – Dr. Wummi Oct 15 '12 at 07:54