#1 2013-01-14 09:44:34

Bascy
Member
From: The Netherlands
Registered: 2012-06-22
Posts: 108

Storing a derived instance of TPersistent in TSQLrecord

A question I have is, what happens when you use some TPersistent in TSQLRecord, and you assign and object for an inherited class to it.

TMyPersistent = class(TPersistent)
  private
    FName: String;
  published
    Name: string read Fname write Fname;
end;

TMyDerived = class(TMyPersistent)
  private
    FAddress: string;
  published
    Address: String read FAddress write FAddress;
end;

TMyRecord = class(TSQLRecord)
  private
    FPers: TMyPersistent;
  published
   Pers: TMyPersistent read FPers write FPers;
end;

What happens when i assign a instance of TMyDerived to TMyRecord.Pers and write that to a database? Will the Address property be written to?
What happens when i read TMyRecord back from the database? Will it create an TMyPersistent object or a TMyDerived object?

Offline

#2 2013-01-14 12:11:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: Storing a derived instance of TPersistent in TSQLrecord

I suspect you need the exact same class (or at least published properties) to be used.

There is no "class type" in the JSON serialization, so at read, it will create a TMyPersistent instance, even if at writing stage, it was a TMyDerived instance.

Offline

#3 2013-01-14 12:20:56

Bascy
Member
From: The Netherlands
Registered: 2012-06-22
Posts: 108

Re: Storing a derived instance of TPersistent in TSQLrecord

Mmm .. too bad :-(
That will force us to go back to the Delphi streaming system

Offline

#4 2013-01-14 16:46:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,603
Website

Re: Storing a derived instance of TPersistent in TSQLrecord

It may be linked to the http://synopse.info/fossil/tktview?name=cacaaf8f33 implementation.

That is, store the class name within the JSON stream.
But you will need the class type to be serialized.
Not truly JSON content...
DataSnap for instance uses some custom format with some additional members for this, but this breaks pretty much the JSON content, and makes it difficult to consume it from AJAX clients, unless you write your own wrapper code.

Custom JSON serialization of classes can be used to by-pass this issue.
That is, you can register you own TMyPersistent serializer.
But the problem may comes from the fact that the framework needs to be extended to create a TMyDerived instance instead of TMyPersistent, depending on the JSON content. This is not possible by now.

I suspect all this may be a bit confusing.
Worth another feature request, since serializing the class name optionally may be the first step to cacaaf8f33 feature.

Offline

Board footer

Powered by FluxBB