惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
月光博客
月光博客
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
U
Unit 42
腾讯CDC
爱范儿
爱范儿
J
Java Code Geeks
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
B
Blog
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - delphi中间件

array of TVarRec core.recordModel.pas 使用泛型序列结构体 DDD建模指导 rabbitMQ VS mqtt redis流的应用场景 redis消费者组 redis流的操作命令 领域服务与领域事件 业务规则和模型 限界上下文与统一语言 领域驱动 mqtt即时通讯 ActiveRecord ORM RAD(速成应用开发) unigui插件框架 工厂流水线式自动生产UNIGUI WEB软件 delphi cs\web一种统一的界面风格 - delphi中间件 - 博客园 动态生成unidbgrid 单据工厂 用json元数据填充模板 rest vs jsonrpc SSE技术详解:使用 HTTP 做服务端数据推送应用的技术 http持久连接 json-rpc 2.0 MCP服务器 RTTI对性能的影响 频繁地创建和销毁对象 TMultiPartFormData 雪花ID
mormot2 ORM
delphi中间件 · 2026-04-03 · via 博客园 - delphi中间件

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.