Using abstract class

Aug 29, 2013 at 1:28 PM
Hi,

I'd love to use this libray.
Unfortunately, it seems to me like there is no support yet for abstract method deserialization (List<abstractClass>), and giving developers a way to describe to fastJson how to deserialize them into concrete classes.

Is this yet doable ? Anything planned on this side? I'd really need that.

thx for the amazing work btw
Coordinator
Aug 29, 2013 at 1:56 PM
I haven't tested this however you can assign polymorphic classes to a list of a base class definition.

Do you have any code samples or exceptions I can work on?

Cheers,
Mehdi
Aug 29, 2013 at 2:05 PM
Edited Aug 30, 2013 at 9:39 AM
Well for instance :

public abstract class abstractClass{
  public string myConcreteType {get;set;}

  public abstractClass(string type){
    this.myConcreteType = type;
  }
}

public abstract class abstractClass<T> : abstractClass{
  public T Value {get;set;}

  public abstractClass(T value, string type) : base(type){
    this.Value = value;
  }
}

public class OneConcreteClass : abstractClass<int>{
  public OneConcreteClass (int value) : base(value, "INT"){}
}
public class OneOtherConcreteClass : abstractClass<string>{
  public OneOtherConcreteClass (string value) : base(value, "STRING"){}
}
How could i deserialize something like List<abstractClass>, but i want the result to have the concrete type and not to lose theit precious T Value in the way !

cheers
Coordinator
Aug 29, 2013 at 6:03 PM
... and what happens currently when you do this?
Aug 30, 2013 at 9:38 AM
Edited Aug 30, 2013 at 9:47 AM
var intField = new OneConcreteClass(1);
var stringField = new OneOtherConcreteClass("lol");
var list = new List<abstractClass>() { intField, stringField};

var json= fastJSON.JSON.Instance.ToJSON(list);
var objects= fastJSON.JSON.Instance.ToObject<List<abstractClass>>(json);
The last line throws an exception : Reflection.cs line 81.
Of course, this cannot find a constructor as the class is abstract.

I could use Instance.Parse I guess. But this bit is usally deep inside another object.

I'd like to be able to "somehow" override the basic behaviour (looking for the constructor), and show him the concrete type that should be used so my objects is directly usable.
Coordinator
Sep 6, 2013 at 2:21 PM
Your code works fine if you create a default constructor for all your classes.
        public abstract class abstractClass
        {
            public abstractClass()
            {

            }
...