unit uMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.ComCtrls, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Samples.Spin, Vcl.Buttons;
type
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;
TfrmJSONText = class(TForm)
btn1: TButton;
mmolog: TMemo;
SpeedButton1: TSpeedButton;
seTestNumber: TSpinEdit;
lbl1: TLabel;
procedure FormCreate(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure Log(const S: string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmJSONText: TfrmJSONText;
TestNumber: Integer;
implementation
uses
System.Diagnostics
{$IF CompilerVersion>31.0}
, System.JSON.Serializers, System.JSON.Types
{$ENDIF}
;
{$R *.dfm}
procedure TfrmJSONText.btn1Click(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 := 0 to TestNumber - 1 do
begin
Foo := TObj1.Create;
try
Foo.field_s := 'Hello World';
Foo.field_i := 42;
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 := 1 to TestNumber - 1 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 TfrmJSONText.FormCreate(Sender: TObject);
const
// D2010~D10.3
DelphiIDEVers: array[21..33] 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', 'Delphi 10.3 Rio');
begin
{$IFDEF WIN64}
Caption := Caption + ' (64-bit)';
{$ENDIF}
{$IFDEF WIN32}
Caption := Caption + ' (32-bit)';
{$ENDIF}
Caption := Caption + ' - ' + DelphiIDEVers[Trunc(CompilerVersion)];
{$IF CompilerVersion<32.0} //版本小于Delphi 10.2 ,button Enabled false
btn1.Enabled := False;
{$ENDIF}
end;
procedure TfrmJSONText.Log(const S: string);
begin
mmoLog.Lines.Add(S);
end;
{ TObj1 }
constructor TObj1.Create;
begin
inherited;
end;
end.