#1 2015-06-12 03:33:43

igors233
Member
Registered: 2012-09-10
Posts: 241

Serializing record with classes

Hi all,

I'm trying to serialize some UI for several forms, for example Font settings, ListView columns (order, widths) and several string properties. Here is the record:

  TFrmSerializerRec = packed record
    Font: TFont;
    LV: TListView;
    Param1: string;
    Param2: string;
  end;

I'm writing record with ASerializer.AddRecordJSON and loading with RecordLoadJSON, problem is that I cannot register TFrmSerializerRec with RegisterCustomJSONSerializerFromText, I get error for TFont and TListView, I've tried registering both of them with RegisterClassForJSON, RegisterCustomJSONSerializerFromTextSimpleType and RegisterCustomSerializer but it all fails.
If I call ObjectToJson individually on Font and ListView it works.
I know I can write a custom serializer for this record but I'm wondering is there a way to use existing, system ones, since all those classes already work if called one by one (as opposed when called as a group in a record).

Thanks,
Igor

Offline

#2 2015-06-12 10:46:53

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

Re: Serializing record with classes

Class instance within records are not supported.

Records are not meant to host class instances!
How would you free them?

Use a TSynAutoCreateField class instead.

BTW, I think that serializing VCL objects is not very safe, since they would be created outside the application context.
Use it at your own risk!

Offline

#3 2015-06-12 13:10:01

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: Serializing record with classes

>> So I'll use it same way as this record with these values I want to store?

Yes.

It will create the sub class instances for you, and release them within Destroy.
Nice and easy.

Offline

Board footer

Powered by FluxBB