0

I am trying to find a way to serialize and deserialize the ClassToSerialize below built like:

    [Serializable]
    public interface IFoo
    {
    }

    [Serializable]
    public class BaseFoo : IFoo
    {
    }
    [Serializable]
    public class Foo1 : BaseFoo
    {
        public string Foo1_Member1 { get; set; }
        public int Foo1_Member2 { get; set; }
    }
    [Serializable]
    public class Foo2 : BaseFoo
    {
        public IList<IFoo> Foo2_Member3 { get; set; }
        public string Foo2_Member4 { get; set; }
    }
    [Serializable]
    public class ClassToSerialize
    {
        public string Class_Member1;
        public IFoo Foo;
    }

The class will be passed to an API and on deserialization the instances of Foo1 and Foo2 need to be reconstructed and placed into the "Foo" property and down the chain into IList< IFoo > or a similar object.

I've been stuck with this for a day or so and I am out of ideas!

MihaiP.
  • 123
  • 1
  • 10
  • Can you provide examples of things you have tried? – AJ X. Nov 14 '16 at 23:45
  • What technique / library are you using to serialize your classes? I don't see any of the usual attributes like `[Serializable]` or `[DataContract]`. I know DataContractSerializer has KnownTypes to deal with this problem, but that won't do you much good if you're not using DataContractSerializer. – Joe White Nov 14 '16 at 23:48
  • I've tried everything from implementing XML Attributes to a Custom implementation of `ISerializable`. However, I am looking to get a validated direction on which to focus my efforts. – MihaiP. Nov 14 '16 at 23:49
  • @Joe - I would prefer to have native .NET code to accomplish this. My latest approach is implementing `ISerializable` on `Foo1` and `Foo2`. Is this the best way of doing it? Thinking of it, this will be a `[DataContract]` for the API. – MihaiP. Nov 14 '16 at 23:51
  • ? DataContractSerializer *is* native .NET code. I've never used ISerializable so I can't compare the two approaches, but with DataContractSerializer I think all you'd have to do (besides adding `[DataContract]` and `[DataMember]` attributes) is add `[KnownType(typeof(Foo1))]` and `[KnownType(typeof(Foo2))]` attributes to ClassToSerialize. – Joe White Nov 14 '16 at 23:57
  • 1
    Can you even serialize something that's been declared as being of an interface type? You can't instantiate an `IFoo`. – Ann L. Nov 14 '16 at 23:58
  • That is why I am asking if it wouldn't be better to handle both serialization and deserialization in a custom manner. If I use `JsonConvert.SerializeObject(o)` I get a string representation like: `{ "Class_Member1":"1", "Foo":{"Foo2_Member3":[{"Foo1_Member1":"1","Foo1_Member2":1},{"Foo1_Member1":"2","Foo1_Member2":2}],"Foo2_Member4":"4"} }` However, the object is not properly deserialized on the other side of the API. – MihaiP. Nov 15 '16 at 00:23

1 Answers1

0

For other people trying to do accomplish something similar: I've ended up implementing ISerializable on BaseFoo, Foo1, Foo2 and ClassToSerialize.

I have added a FooType Type field to the interface and implemented it on the base class.

In the implementation I am deserializing first to BaseFoo and then, I'm deserializing again based on type either to Foo1 or Foo2.

I would love to hear a better approach.

MihaiP.
  • 123
  • 1
  • 10