#1 2015-09-08 09:53:16

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

How to serialize in ObjectToJSONFile an enum data type?

Hello!

I have the following code:

  TNumber = (
    One = 1,
    Two = 2,
    Three = 3
  );

  TMyNumbers = class
  private
    fNumber: TNumber;
  published
    property Number: TNumber read fNumber; 
  end;

  ...
  var
    MyNumbersObject: TMyNumber;

  ObjectToJSONFile( MyNumbersObject );

  Why doesn't this function serialize the data type? And how can I do it?

  Thank you!

Offline

#2 2015-09-08 10:36:16

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: How to serialize in ObjectToJSONFile an enum data type?

Try with TSynPersistent.

TNumber = (
    One = 1,
    Two = 2,
    Three = 3
  );

  TMyNumbers = class(TSynPersistent) //--> TSynPersistent is better that TPersistent
  private
    fNumber: TNumber;
  published
    property Number: TNumber read fNumber; 
  end;

  ...
  var
    MyNumbersObject: TMyNumber;

  ObjectToJSONFile( MyNumbersObject );

Esteban

Offline

#3 2015-09-08 14:53:05

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

Re: How to serialize in ObjectToJSONFile an enum data type?

A plain class does not have the needed RTTI.

You need inherit from TPersistent or our TSynPersistent class.

Offline

#4 2015-09-09 06:13:29

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: How to serialize in ObjectToJSONFile an enum data type?

Thank you for your help! It works! But only if I change TNumber as follows:

  // NO SERIALIZATION
  TNumber = (
    One = 1,
    Two = 2,
    Three = 3
  );

  // SERIALIZATION
  TNumber = (
    One,
    Two,
    Three
  );

Is it possible to serialize despite of the assigned values?

I would like to use a code like this

  TNumberGreaterThenTen = (
    Ten = 10,
    Twenty = 20,
    Thirty = 30
  );

Last edited by cypriotcalm (2015-09-09 06:36:24)

Offline

#5 2015-09-11 05:51:09

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: How to serialize in ObjectToJSONFile an enum data type?

Hello! Does anybody know the answer? :-) Thank you!

Offline

#6 2015-09-11 06:35:32

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

Re: How to serialize in ObjectToJSONFile an enum data type?

No, it is not possible to do that, due to limitations of how Delphi stores its RTTI for such enums.
There is not any information available to do it.

The workaround is to use one enumeration for storage, and a constant array for your expected numerical values:

type
TNumberGreaterThenTen = (
    Ten,
    Twenty,
    Thirty
  );

const
  GREATERTHANTEN_VALUES: array[TNumberGreaterThenTen] of integer = (10,20,30);

Offline

#7 2015-09-11 06:50:28

cypriotcalm
Member
Registered: 2015-02-18
Posts: 122

Re: How to serialize in ObjectToJSONFile an enum data type?

Ok, thank you for the answer!

Yes, this workaroung is also my approach, but I wanted to avoid this. Thanks, ab!

Offline

Board footer

Powered by FluxBB