Created
September 21, 2024 22:37
-
-
Save raelb/7e84e98e5b85f77ead12a71cf44bb23c to your computer and use it in GitHub Desktop.
serialize record with RecordSaveJson/DynArraySaveJson
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
unit Unit1; | |
{$mode objfpc}{$H+} | |
interface | |
uses | |
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, | |
mormot.core.base; | |
type | |
{ TForm1 } | |
TForm1 = class(TForm) | |
Memo1: TMemo; | |
procedure FormCreate(Sender: TObject); | |
private | |
procedure InitRecords; | |
public | |
end; | |
type | |
TNotebook = packed record | |
ID: TID; | |
Title: RawUTF8; | |
GUID: RawUTF8; | |
CurrentNote: TID; | |
end; | |
TNotebookArray = array of TNotebook; | |
const | |
// exact definition of the DTOs expected JSON serialization | |
_TApiResponse = 'code:integer type:RawUtf8 message:RawUtf8'; | |
_TCategory = 'id:Int64 name:RawUtf8'; | |
_TNotebook = 'id:Int64 title:RawUtf8 GUID:RawUtf8 currentNote:Int64'; | |
var | |
Form1: TForm1; | |
implementation | |
{$R *.lfm} | |
uses | |
mormot.core.json, mormot.core.rtti, typinfo; | |
{ TForm1 } | |
procedure TForm1.FormCreate(Sender: TObject); | |
begin | |
InitRecords; | |
end; | |
procedure TForm1.InitRecords; | |
var | |
Notebooks: TNotebookArray; | |
Notebook: TNotebook; | |
I: Integer; | |
Json: RawUTF8; | |
begin | |
Notebook.ID:= 1; | |
Notebook.GUID:= 'ABC'; | |
Notebook.Title:= 'Notebook Title'; | |
Notebook.CurrentNote:= 0; | |
Memo1.Lines.Text := RecordSaveJson(Notebook, TypeInfo(TNotebook)); | |
Memo1.Lines.Add(''); | |
for i := 0 to 2 do | |
begin | |
SetLength(Notebooks, Length(Notebooks) + 1); | |
Notebooks[I].ID:= I; | |
Notebooks[I].Title:= 'Notebook ' + IntToStr(I); | |
end; | |
Json := DynArraySaveJson(Notebooks, TypeInfo(TNotebookArray)); | |
Memo1.Lines.Add(Json); | |
end; | |
//initialization | |
// Rtti.RegisterFromText(TypeInfo(TNotebook), _TNotebook); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment