• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi10.2Tokyo新增JSON类学习——TJsonSerializer

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Delphi 10.3.2 for windows 7 编译通过,源码下载地址:

Tokyo 10.2新增类,效率更高更快

TJsonSerializer

需要引用单元:System.JSON.Serializers

 

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.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap