N
N
ndbn2016-07-13 20:50:37
Delphi
ndbn, 2016-07-13 20:50:37

Deserializing an abstract class?

Greetings.
There is a set of Delphi XE2 classes:

TBaseAbstractClass = class abstract
  name : string;
end;

TDerivedClassStr = class(TBaseAbstractClass)
  value : string;
end;

TDerivedClassFloat = class(TBaseAbstractClass)
  value : float;
end;

TMyObject = class
    objs : array of TBaseAbstractClass;
end;

There is JSON:
{
 [
    {"name" : "name1", "value" : "value1"},
    {"name" : "name1", "value" : "value2"}
 ]
}

It can be deserialized as TMyObject, but then the values ​​are lost, and typecasting causes an error (which is not at all obvious to me).
Deserialization is done by the superobject library .
Is there a way to store the value value, but in a way that does not create:
TMyObject = class
    objs : array of TDerivedClassStr;
end;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RomanYankovsky, 2016-08-05
@ndbn

Didn't use superobject, but how does he guess what type of class needs to be deserialized? It's not obvious from your JSON. You need to store the class name in an array when serializing and when deserializing create them using RTTI.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question