Abstract Lists cant be serialized

Jan 31, 2010 at 6:30 PM

I have a generic list where type is an abstract class which json doesnt like so I created a customcreationconverter but it still doesnt like it and the types in the subclass dont ever get returned. . Is it possible for you to handle abstract or iterfaces in lists/dictionaries automatically by not relying on the type in the list  and assume the list value types are different from the list type so when you reload the list values, you read the list value type from json and reconstuct them based on the saved type and not on the type decaration of the list. The serialize part seems to be working fine so i think its just the deserialize portion that would need to change.

Attached is a sample of what im trying to do, do you know if there is a way for it to handle this.

thank

scott

 

 public abstract class BaseClass
   {
      public abstract int Size { get; }
   }


public class  Subclass1 : BaseCLass
   {
 
      private int m_i;
      private int m_j;
      private int m_k;

      public Subclass1(int ni, int nj, int nk)
      {
         m_ni = ni;
         m_nj = nj;
         m_nk = nk;
      }

      public Subclass1()
      {
      }

      [JsonIgnore]
      public int Ni
      {
         get { return m_ni; }
      }

      [JsonIgnore]
      public int Nj
      {
         get { return m_nj; }
      }

      [JsonIgnore]
      public int Nk
      {
         get { return m_nk; }
      }

      [JsonIgnore]
      public override int Size
      {
         get { return m_ni*m_nj*m_nk; }
      }
   }


public testClass
{
     private IList<BaseClass> m_baselist = new List<BaseClass>();


     public BaseList
     {
        get { return m_baselist; }
}

//serialize
testClass t1 = new testCLass();
t1.BaseList.Add(new SubClass1); //put the subclass into a baseclass list
MemoryStream ms = new MemoryStream();
JsonSerializer js = new JsonSerializer();
       
BsonWriter bw = new BsonWriter(ms);
js.Serialize(bw, t1);

//deserialize
Byte[] bytes = ms.GetBuffer();
Byte[] bytes2 = (Byte[])bytes.Clone();
int len = bytes2.Length;

MemoryStream ms2 = new MemoryStream(bytes2);


BsonReader br = new BsonReader(ms2);

testClass t4 = (testClass)js.Deserialize(br, typeof(TestClass));

 

Apr 23, 2010 at 4:44 PM

I'm having somewhat of the same problem.

In your case, the serializer won't be able to create an instance of an abstract class, so your base class will have to be a concrete class. Second, I think you'll need to add a set accessor to your BaseList property in testClass in order for it to be de-serialized. Finally, the serializer will de-serialize your testClass, but your BaseList property will only contain instances of the BaseClass type, and will not create the derived type since it knows nothing about the derived type. I am looking into how you can resolve the property back to the original derived type, and when I get a solution I will post it.

Hope this helps.

Apr 23, 2010 at 5:07 PM

Check my thread here for the solution:

 

http://json.codeplex.com/Thread/View.aspx?ThreadId=210326