You are not logged in.
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
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
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
Hello! Does anybody know the answer? :-) Thank you!
Offline
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
Ok, thank you for the answer!
Yes, this workaroung is also my approach, but I wanted to avoid this. Thanks, ab!
Offline