Last active
July 30, 2018 14:52
-
-
Save ComingNine/0f213161e50c23763c9b8cac8b43b13e to your computer and use it in GitHub Desktop.
Cannot serialize record with enum property.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Program TestRecordWithEnumProp; | |
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64 OWNNORMTOUPPER | |
{$IFOPT D+} {$DEFINE DEBUG} {$ENDIF} | |
{$ASSERTIONS ON} | |
{$IFNDEF FPC} {$APPTYPE CONSOLE} {$ENDIF} | |
uses | |
{$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads | |
SysUtils, SynCommons, mORMot; | |
type | |
{$M+} | |
TMyEnum = (meFirst, meSecond); | |
TRec = packed record | |
E: TMyEnum; | |
K: String; | |
V: Extended; | |
end; | |
TObj = class | |
private | |
FE: TMyEnum; | |
FK: String; | |
FV: Extended; | |
published | |
property E: TMyEnum read FE write FE; | |
property K: String read FK write FK; | |
property V: Extended read FV write FV; | |
end; | |
const | |
__TRec = 'E: TMyEnum; K: String; V: Extended'; | |
begin | |
// ESynException: 'Unregistered ptCustom for TJSONRecordTextDefinition.AddItem(E: TMYENUM)' | |
TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TRec), __TRec); | |
// OK. | |
TJSONSerializer.RegisterClassForJSON([TObj]); | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Program TestRecordWithEnumProp; | |
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64 OWNNORMTOUPPER | |
{$IFOPT D+} {$DEFINE DEBUG} {$ENDIF} | |
{$ASSERTIONS ON} | |
{$IFNDEF FPC} {$APPTYPE CONSOLE} {$ENDIF} | |
uses | |
{$I SynDprUses.inc} // use FastMM4 on older Delphi, or set FPC threads | |
SysUtils, SynCommons, mORMot; | |
type | |
{$M+} | |
TMyEnum = (meFirst, meSecond); | |
TRec = packed record | |
E: TMyEnum; | |
K: String; | |
V: Extended; | |
end; | |
TObj = class | |
private | |
FE: TMyEnum; | |
FK: String; | |
FV: Extended; | |
published | |
property E: TMyEnum read FE write FE; | |
property K: String read FK write FK; | |
property V: Extended read FV write FV; | |
end; | |
const | |
__TRec = 'E: Byte; K: String; V: Extended'; | |
var | |
Rec: TRec; | |
Obj: TObj; | |
begin | |
// Writeln(sizeof(TMyEnum)); | |
TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TRec), __TRec).Options := [soWriteHumanReadable]; | |
(* | |
{ | |
"E": 0, | |
"K": "", | |
"V": 0 | |
} | |
*) | |
Writeln(RecordSaveJSON(Rec, TypeInfo(TRec))); | |
TJSONSerializer.RegisterClassForJSON([TObj]); | |
Obj := TObj.Create; | |
(* | |
{ | |
"ClassName":"TObj", | |
"E": "meFirst", | |
"K": "", | |
"V": 0 | |
} | |
*) | |
Writeln(ObjectToJSON(Obj, [woHumanReadable, woStoreClassName])); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment