













mormot2 ORM
unit mormot2.json; // cxg 2026 {$IFDEF fpc} {$MODE DELPHI}{$H+} {$ENDIF}interface uses mormot.core.text, mormot.core.json, mormot.core.base, Classes, SysUtils; type { TJson } TJson = record //record to json class function json<T>(const aRec: T): rawutf8; overload static; //dynArray(TArray<record>) to json class function json2<T>(const aArray: T): rawutf8; static; //obj to json class function json(const aObj: TObject): rawutf8; overload static; //json to record class function unjson<T>(const json: rawutf8): T; overload static; //json to dynArray class function unjson2<T>(const json: rawutf8): T; static; //json to obj class procedure unjson(const json: rawutf8; aObj: TObject); overload static; end; implementation class function TJson.json(const aObj: TObject): rawutf8; begin Result := mormot.core.text.ObjectToJson(aObj); end; class function TJson.json<T>(const aRec: T): rawutf8; begin result := mormot.core.json.RecordSaveJson(aRec, TypeInfo(T)); end; class function TJson.json2<T>(const aArray: T): rawutf8; begin result := mormot.core.json.DynArraySaveJson(aArray, TypeInfo(T)); end; class procedure TJson.unjson(const json: rawutf8; aObj: TObject); begin mormot.core.json.ObjectLoadJson(aObj, json); end; class function TJson.unjson<T>(const json: rawutf8): T; begin mormot.core.json.RecordLoadJson(result, json, TypeInfo(T)); end; class function TJson.unjson2<T>(const json: rawutf8): T; begin mormot.core.json.DynArrayLoadJson(result, json, TypeInfo(T)); end; end.
演示:
注意:delphi使用相对简单。
unit Unit1; {$mode delphi}{$H+} interface uses mormot.core.rtti, mormot2.json, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure FormCreate(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } type Ttest = packed record //lazarus必须packed,delphi则不需要 f1: string; f2: Integer; end; Ttests = TArray<Ttest>; const //lazarus必须定义,delphi则不需要 _Ttest = 'f1:string f2:Integer'; procedure TForm1.Button1Click(Sender: TObject); var t1: Ttest; begin t1.f1:='汉字'; t1.f2:=99; memo1.Text := TJson.json<Ttest>(t1); //{"f1":"汉字","f2":99} end; procedure TForm1.Button2Click(Sender: TObject); var t2: Ttest; begin t2 := TJson.unjson<Ttest>(memo1.Text); showmessage(t2.f1); //汉字 end; procedure TForm1.Button3Click(Sender: TObject); var t: Ttests; begin setlength(t, 2); t[0].f1:='中国'; t[0].f2:=1; t[1].f1:='湖南'; t[1].f2:=2; memo1.Text := TJson.json2<Ttests>(t); //[{"f1":"中国","f2":1},{"f1":"湖南","f2":2}] end; procedure TForm1.Button4Click(Sender: TObject); var t: Ttests; begin t := TJson.unjson2<Ttests>(memo1.Text); showmessage(t[0].f1); //中国 end; procedure TForm1.FormCreate(Sender: TObject); begin Rtti.RegisterFromText([TypeInfo(Ttest), _Ttest]); //lazarus没有此行,显示binary buffer;delphi则不需要 end; end.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。