Skip to content

Instantly share code, notes, and snippets.

@zedxxx
Created July 4, 2020 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zedxxx/41c7ee76a214bd1985878173228100b7 to your computer and use it in GitHub Desktop.
Save zedxxx/41c7ee76a214bd1985878173228100b7 to your computer and use it in GitHub Desktop.
Synopse RecordSave
uses
SysUtils,
SynCommons;
{$A8}
type
TRec = record
f1: Word;
f2: Byte;
f3: UInt64;
f4: Byte;
f5: Cardinal;
f6: Boolean;
end;
procedure DoTest;
var
VLen: Integer;
VRec: TRec;
VRecNew: TRec;
VBytes: TBytes;
VJson: RawUTF8;
begin
VLen := SizeOf(TRec);
FillChar(VRec, VLen, 88);
FillChar(VRecNew, VLen, 88);
with VRec do begin
f1 := $1234;
f2 := $56;
f3 := Int64(MaxInt) * 4;
f4 := $78;
f5 := MaxInt;
f6 := False;
end;
VBytes := RecordSaveBytes(VRec, TypeInfo(TRec));
RecordLoad(VRecNew, PAnsiChar(VBytes), TypeInfo(TRec));
if not CompareMem(@VRec, @VRecNew, VLen) then begin
raise Exception.Create('FAIL!');
end;
VJson := RecordSaveJson(VRec, TypeInfo(TRec));
Writeln(VJson);
VJson := RecordSaveJson(VRecNew, TypeInfo(TRec));
Writeln(VJson);
end;
@zedxxx
Copy link
Author

zedxxx commented Jul 4, 2020

Output:

{"f1":4660,"f2":86,"f3":8589934588,"f4":120,"f5":2147483647,"f6":false}
{"f1":4660,"f2":86,"f3":8589934588,"f4":120,"f5":2147483647,"f6":false}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment