mormot2 json例子
mormot2 json例子
适用于DELPHI和LAZARUS。mormot json序列走的是RTTI路线,依赖DELPHI和FPC的RTTI。
unit Unit1; /// <author>cxg 2023-2-8</author> /// mormot2 json demo interface uses mormot.core.text, mormot.core.json, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TTest = record //结构体 id: Integer; name: string; end; TObj = class //对象 private fid: Integer; fname: string; published property id: Integer read fid write fid; property name: string read fname write fname; end; TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); //record to json begin var t: TTest; t.id := 1; t.name := '名字'; Memo1.Text := mormot.core.json.RecordSaveJson(t, TypeInfo(TTest)); //{"id":1,"name":"name"} end; procedure TForm1.Button2Click(Sender: TObject); //json to record begin var t: TTest; mormot.core.json.RecordLoadJson(t, Memo1.Text, TypeInfo(TTest)); ShowMessage(t.name); //名字 end; procedure TForm1.Button3Click(Sender: TObject); //object to json begin var o: TObj := TObj.Create; o.id := 2; o.name := '名字'; Memo1.Text := mormot.core.text.ObjectToJson(o); //{"id":2,"name":"名字"} o.Free; end; procedure TForm1.Button4Click(Sender: TObject); //json to object begin var o: TObj := TObj.Create; mormot.core.json.ObjectLoadJson(o, Memo1.Text); ShowMessage(o.name); //名字 o.Free; end; end.
unit Unit1; {$IFDEF fpc} {$MODE DELPHI}{$H+} {$ENDIF} interface uses mormot.core.text, mormot.core.json, mormot.Net.sock, mormot.net.async, mormot.net.http, mormot.net.server, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type TTest = record //结构体 id: Integer; name: string; end; TTests = record msg: string; t: tarray<TTest>; end; { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); private FServer: THttpAsyncServer; public end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin FServer:= THttpAsyncServer.Create('1234', nil, nil, 'pn'); end; procedure TForm1.Button1Click(Sender: TObject); //record to json var tt: ttests; begin setlength(tt.t, 1); tt.msg:= '测试'; tt.t[0].id := 1; tt.t[0].name := '名字'; Memo1.Text := mormot.core.json.RecordSaveJson(tt, TypeInfo(TTests)); //{"id":1,"name":"name"} end; procedure TForm1.Button2Click(Sender: TObject); //json to record var tt: TTests; begin mormot.core.json.RecordLoadJson(tt, Memo1.Text, TypeInfo(TTests)); ShowMessage(tt.t[0].name); //名字 end; end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/17101359.html