今天在盒子闲逛,无意中看到有人说XE7自带的Json对象序列化很慢,帖子在这里:http://bbs.2ccc.com/topic.asp?topicid=464378;经过测试的确如此。
     但是 D10.2后,自带的 Json 做了优化,性能大大的提高了100多倍。

和其他json库对比了序列化和反序列化性能,JsonDataObjects 性能最好,但是只支持简单的对象,不支持结构体,QJson 则不支持动态数组,不支持 Attributes (RTTI),比如需要排除某些字段,System.JSON 和 XSuperObject 可支持。总体来说,个人比较喜欢10.2新增的TJsonSerializer,使用方便,无需第三方库。

全部代码如下:

unit Unit1;
{$DEFINE XSuperObject}
{$DEFINE QJson }
{$DEFINE JsonDataObjects} interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls,
Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin
{$IF CompilerVersion>31.0}, System.JSON.Types, System.JSON.Serializers{$ENDIF}; type {$IF CompilerVersion>31.0}
[JsonSerialize(TJsonMemberSerialization.&Public)]
{$ENDIF} TObj1 = class
private
F_i: Integer;
f_d: TDateTime;
f_s: string;
// f_a: TArray<string>;
public
constructor Create;
published
property field_s: string read f_s write f_s;
property field_i: Integer read F_i write F_i;
property field_d: TDateTime read f_d write f_d;
// property field_a: TArray<string> read f_a write f_a;
end; TForm1 = class(TForm)
btnObjectToJsonString: TButton;
btnJsonSerializer: TButton;
mmoLog: TMemo;
seTestNumber: TSpinEdit;
lbl1: TLabel;
btnXSuperObject: TButton;
btnQJson: TButton;
btnJsonDataObjects: TButton;
btnParseFile: TButton;
dlgOpen1: TOpenDialog;
procedure btnObjectToJsonStringClick(Sender: TObject);
procedure btnJsonSerializerClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnXSuperObjectClick(Sender: TObject);
procedure btnQJsonClick(Sender: TObject);
procedure btnJsonDataObjectsClick(Sender: TObject);
procedure btnParseFileClick(Sender: TObject);
private
procedure Log(const S: string);
public
{ Public declarations }
end; var
Form1: TForm1;
TestNumber: Integer; implementation uses
System.Diagnostics,
System.IOUtils,
REST.JSON,
System.JSON,
{$IFDEF JsonDataObjects}
JsonDataObjects,
{$ENDIF}
{$IFDEF XSuperObject}
XSuperObject,
{$ENDIF}
{$IFDEF QJson}
qjson,
{$ENDIF}
rtcMW.System.JSONHelper; {$R *.dfm} function GetFileSize(const AFileName: string): Int64;
var
AttributeData: TWin32FileAttributeData;
begin
if not GetFileAttributesEx(PChar(AFileName), GetFileExInfoStandard, @AttributeData) then
RaiseLastOSError;
Int64Rec(Result).Lo := AttributeData.nFileSizeLow;
Int64Rec(Result).Hi := AttributeData.nFileSizeHigh;
end; procedure TForm1.btnObjectToJsonStringClick(Sender: TObject);
var
Foo: TObj1;
I: Integer;
sw: TStopwatch;
JSON: string;
begin
TestNumber := seTestNumber.Value; sw := TStopwatch.StartNew;
for I := to TestNumber - do
begin
Foo := TObj1.Create;
try
Foo.field_s := 'Hello World';
Foo.field_i := ;
Foo.field_d := Now;
JSON := REST.JSON.TJson.ObjectToJsonString(Foo);
finally
Foo.Free;
end;
end;
Log('TJson.ObjectToJsonString:' + sw.ElapsedMilliseconds.ToString + ' ms ' + JSON); sw := TStopwatch.StartNew;
for I := to TestNumber - do
begin
Foo := REST.JSON.TJson.JsonToObject<TObj1>(JSON);
try
finally
Foo.Free;
end;
end;
Log('TJson.ObjectToJsonString:' + sw.ElapsedMilliseconds.ToString + ' ms');
Log('=======================');
end; procedure TForm1.btnXSuperObjectClick(Sender: TObject);
{$IFDEF XSuperObject}
var
Foo: TObj1;
I: Integer;
sw: TStopwatch;
AJson: string;
{$ENDIF}
begin
{$IFDEF XSuperObject}
TestNumber := seTestNumber.Value;
sw := TStopwatch.StartNew; for I := to TestNumber - do
begin
Foo := TObj1.Create;
try
Foo.field_s := 'Hello World';
Foo.field_i := ;
Foo.field_d := Now;
AJson := XSuperObject.TJson.SuperObject<TObj1>(Foo).AsJSON();
finally
Foo.Free;
end; end;
Log('XSuperObject.TJson.SuperObject:' + sw.ElapsedMilliseconds.ToString + ' ms ' + AJson); for I := to TestNumber - do
begin
Foo := XSuperObject.TJson.Parse<TObj1>(AJson);
try
finally
Foo.Free;
end; end;
Log('XSuperObject.TJson.Parse:' + sw.ElapsedMilliseconds.ToString + ' ms');
Log('=======================');
{$ENDIF} end; procedure TForm1.btnJsonDataObjectsClick(Sender: TObject);
{$IFDEF JsonDataObjects}
var
Foo: TObj1;
I: Integer;
sw: TStopwatch;
AJson: string;
JsonObj: JsonDataObjects.TJsonObject;
{$ENDIF}
begin
{$IFDEF JsonDataObjects}
TestNumber := seTestNumber.Value;
sw := TStopwatch.StartNew; JsonObj := JsonDataObjects.TJsonObject.Create;
for I := to TestNumber - do
begin
Foo := TObj1.Create;
try
Foo.field_s := 'Hello World';
Foo.field_i := ;
Foo.field_d := Now;
JsonObj.FromSimpleObject(Foo);
AJson := JsonObj.ToJSON();
finally
Foo.Free;
end; end;
Log('JsonDataObjects.FromSimpleObject:' + sw.ElapsedMilliseconds.ToString + ' ms ' + AJson);
JsonObj.Free; sw := TStopwatch.StartNew;
for I := to TestNumber - do
begin
JsonObj := JsonDataObjects.TJsonObject.Parse(AJson) as JsonDataObjects.TJsonObject;
Foo := TObj1.Create;
try
JsonObj.ToSimpleObject(Foo);
finally
Foo.Free;
JsonObj.Free;
end;
end;
Log('JsonDataObjects.ToSimpleObject:' + sw.ElapsedMilliseconds.ToString + ' ms');
Log('=======================');
{$ENDIF}
end; procedure TForm1.btnParseFileClick(Sender: TObject); var
JsonObj: System.JSON.TJsonObject;
JsonObj1: JsonDataObjects.TJsonObject;
{$IFDEF QJson}
JsonObj2: TQJson;
{$ENDIF}
sw: TStopwatch;
FileName: string;
begin
if not dlgOpen1.Execute() then
Exit;
FileName := dlgOpen1.FileName;
Log(Format('%s,FileSize:%d Byte', [FileName, GetFileSize(FileName)]));
Log('======================='); sw := TStopwatch.StartNew;
JsonObj := System.JSON.TJsonObject.LoadFromFile(FileName).AsJsonObject;
try
if JsonObj = nil then
raise Exception.CreateFmt('%s 不是有效的 JSON 文件', [FileName]);
// Log(JsonObj.GetValue<string>('meta[0].field_name'));
Log('System.Json.TJsonObject.LoadFromFile:' + sw.ElapsedMilliseconds.ToString + ' ms');
finally
JsonObj.Free;
end;
{$IFDEF JsonDataObjects}
sw := TStopwatch.StartNew;
JsonObj1 := JsonDataObjects.TJsonObject.Create;
try
JsonObj1.LoadFromFile(FileName);
if JsonObj1 <> nil then
begin
Log('JsonDataObjects.TJsonObject.LoadFromFile:' + sw.ElapsedMilliseconds.ToString + ' ms');
end;
finally
JsonObj1.Free;
end;
{$ENDIF}
{$IFDEF QJson}
sw := TStopwatch.StartNew;
JsonObj2 := TQJson.Create;
try
JsonObj2.LoadFromFile(FileName);
if JsonObj2 <> nil then
begin
// Log(Json.ItemByPath('meta[0].field_name').AsString);
Log('TQJson.LoadFromFile:' + sw.ElapsedMilliseconds.ToString + ' ms');
end;
finally
JsonObj2.Free;
end;
{$ENDIF} end; procedure TForm1.btnJsonSerializerClick(Sender: TObject);
{$IF CompilerVersion>31.0}
var
Foo: TObj1;
I: Integer;
sw: TStopwatch;
AJson: string;
Serializer: TJsonSerializer;
{$ENDIF}
begin
{$IF CompilerVersion>31.0}
TestNumber := seTestNumber.Value;
sw := TStopwatch.StartNew;
Serializer := TJsonSerializer.Create;
try
Serializer.DateTimeZoneHandling := TJsonDateTimeZoneHandling.Utc;
for I := to TestNumber - do
begin
Foo := TObj1.Create;
try
Foo.field_s := 'Hello World';
Foo.field_i := ;
Foo.field_d := Now;
AJson := Serializer.Serialize<TObj1>(Foo);
finally
Foo.Free;
end; end;
Log('TJsonSerializer.Serialize:' + sw.ElapsedMilliseconds.ToString + ' ms ' + AJson); sw := TStopwatch.StartNew;
for I := to TestNumber - do
begin
Foo := Serializer.Deserialize<TObj1>(AJson);
try
finally
Foo.Free;
end;
end;
Log('TJsonSerializer.Deserialize:' + sw.ElapsedMilliseconds.ToString + ' ms'); finally
FreeAndNil(Serializer);
end;
Log('=======================');
{$ENDIF}
end; procedure TForm1.btnQJsonClick(Sender: TObject);
{$IFDEF QJson}
var
Foo: TObj1;
I: Integer;
sw: TStopwatch;
AJson: string;
Serializer: TQJson;
{$ENDIF}
begin
{$IFDEF QJson}
TestNumber := seTestNumber.Value;
sw := TStopwatch.StartNew;
Serializer := TQJson.Create;
for I := to TestNumber - do
begin
Foo := TObj1.Create;
try
Foo.field_s := 'Hello World';
Foo.field_i := ;
Foo.field_d := Now;
Serializer.FromRtti(Foo);
AJson := Serializer.AsJSON;
finally
Foo.Free;
end; end;
Log('TQJson.FromRtti:' + sw.ElapsedMilliseconds.ToString + ' ms ' + AJson); for I := to TestNumber - do
begin
Serializer.Parse(AJson);
Foo := TObj1.Create;
try
Serializer.ToRtti(Foo);
finally
Foo.Free;
end; end;
Serializer.Free;
Log('TQJson.ToRtti:' + sw.ElapsedMilliseconds.ToString + ' ms');
Log('=======================');
{$ENDIF}
end; procedure TForm1.FormCreate(Sender: TObject);
const
// D2010~D10.2
DelphiIDEVers: array [ .. ] of string = (
'Delphi 2010',
'Delphi XE',
'Delphi XE2',
'Delphi XE3',
'Delphi XE4',
'Delphi XE5',
'Delphi XE6',
'Delphi XE7',
'Delphi XE8',
'Delphi 10 Seattle',
'Delphi 10.1 Berlin',
'Delphi 10.2 Tokyo');
begin
{$IFDEF WIN64}
Caption := Caption + ' (64-bit)';
{$ENDIF}
Caption := Caption + ' - ' + DelphiIDEVers[Trunc(CompilerVersion)]; {$IF CompilerVersion<32.0}
btnJsonSerializer.Enabled := False;
{$ENDIF} {$IFNDEF XSuperObject}
btnXSuperObject.Enabled := False;
{$ENDIF}
{$IFNDEF JsonDataObjects}
btnJsonDataObjects.Enabled := False;
{$ENDIF}
{$IFNDEF QJson}
btnQJson.Enabled := False;
{$ENDIF}
ReportMemoryLeaksOnShutdown := True;
end; procedure TForm1.Log(const S: string);
begin
mmoLog.Lines.Add(S);
end; { TObj1 } constructor TObj1.Create;
begin
inherited;
{ SetLength(f_a, 4);
f_a[0] := '全能中间件';
f_a[1] := 'QQ:64445322';
f_a[2] := '淘宝:https://imaps.taobao.com/';
f_a[3] := '没有Unicode?\\//'; }
end; end.

Delphi 10.2 JSON与对象/结构体序列化性能提高100多倍的更多相关文章

  1. JSON还原为结构体

    JSON还原为结构体 1)JSON字符串还原为结构体: 2)访问结构体的字段值: 本例运行效果图: uses SynCommons; const // JSON字符串 JSON1 = '{' + #1 ...

  2. go语言之进阶篇json解析到结构体

    1.json解析到结构体 示例: package main import ( "encoding/json" "fmt" ) type IT struct { ...

  3. [GO]json解析到结构体

    package main import ( "encoding/json" "fmt" ) type IT struct { Company string `j ...

  4. Swift学习笔记(10):类和结构体

    目录: 基本 属性 方法 下标 继承 基本 使用class和struct关键字定义类和结构体. ・类是引用类型,结构体和枚举是值类型 ・值类型被赋予给一个变量.常量或被传递给一个函数时,已值拷贝方式传 ...

  5. go get请求 json字符串转为结构体

    package main import ( "io/ioutil" "fmt" "net/http" "encoding/json ...

  6. json数据转换成结构体

    package main import ( "encoding/json" "fmt" ) type IT1 struct { Company string ` ...

  7. 在线JSON转Go 结构体,在线JSON转Go Struct

    在线转换https://oktools.net/json2go

  8. 结构体序列为JSON

    结构体序列为JSON 本例运行效果图: uses SynCommons; const /// JSON字符串 JSON1 = '{' + #13#10 + '"glossary": ...

  9. 深入delphi编程理解之消息(二)发送消息函数及消息编号、消息结构体的理解

    一.delphi发送消息的函数主要有以下三个: (一).SendMessage函数,其原型如下: function SendMessage( hWnd: HWND; {目标句柄} Msg: UINT; ...

随机推荐

  1. Android学习小结

    自从学习Android以来已经经过三个月了,如今市场对于Android工程师的需求接近饱和,所以学习Android的人也少了很多,很多的培训机构也逐渐将Android课程淘汰,导致学习Android的 ...

  2. CF #365 DIV2 D Mishka and Interesting sum 区间异或+线段树

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  3. 洛谷 P1629 邮递员送信 题解

    P1629 邮递员送信 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要 ...

  4. mvn pom文件引用顺序关系

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. MySQL免安装配置步骤

    此方式为直接解压压缩包安装MySQL 1.下载相关压缩包 官网下载地址:https://dev.mysql.com/downloads/mysql/ 下载后将压缩包解压至自己想要的文件路径即可 2.新 ...

  6. maven依赖 dependency中scope=compile 和 provided区别

    问题再现 上次这边朋友问我一个问题,就是他们在pom.xml中的dependency中,看到有一些是provided的情况,比如如下: <dependency> <groupId&g ...

  7. THOR: Tracking Holistic Object Representations

    THOR: Tracking Holistic Object Representations BMVC 2019 Oral 2019-08-04 10:30:09 Paper: https://arx ...

  8. Python Selenium Webdriver常用方法总结

    Python Selenium Webdriver常用方法总结 常用方法函数 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() 最大化窗口: m ...

  9. PostgreSQL中的一些日志

    1.Write Ahead Log(WAL日志)预写日志(WAL)是保证数据完整性的标准方法.在有关交易处理的大多数(如果不是全部)书籍中都可以找到详细说明. 简而言之,WAL的核心概念是,只有在记录 ...

  10. 《你必须知道的javascript(上)》- 1.作用域和闭包

    1 作用域是什么 1.1 编译原理 分词/词法分析(Tokenizing/Lexing) 将由字符组成的字符串分解成(对编程语言来说)有意义的代码块,这些代码块被称为词法单元(token). 解析/语 ...