delphi JSON序列化(五)
  trFW46kwzceA 2024年01月10日 19 0

关于TJSONConverters的使用

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Rest.JSON.Types, Rest.JsonReflect;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TValueObject = class
  private
    FValue: string;
    FCreateTime: TDateTime;
  public
    constructor Create;
    property Value: string read FValue write FValue;
    property CreateTime: TDateTime read FCreateTime write FCreateTime;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  REST.Json, System.Generics.Collections;


procedure TForm1.FormCreate(Sender: TObject);
begin
  var ce := TConverterEvent.Create(TValueObject, 'FValue'); // 使用此构造函数
  ce.StringConverter := function(Data: TObject; Field: string): string
    begin
      Result := 'haha';
    end;

  TJSONConverters.AddConverter(ce);

  Memo1.Text := TJson.ObjectToJsonString(TValueObject.Create);
end;

{ TValueObject }

constructor TValueObject.Create;
begin
  inherited Create;
  FValue := 'test value';
  FCreateTime := Now;
end;

end.

结果: {"value":"haha","createTime":"2024-01-10T17:15:33.588Z"}

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年01月10日 0

暂无评论

推荐阅读
  ZGYAxb2wjd2Z   23小时前   7   0   0 Delphi
  trFW46kwzceA   2024年01月13日   16   0   0 Delphi
  trFW46kwzceA   2024年01月11日   19   0   0 Delphi
  jl63rPptnaFE   2024年02月21日   17   0   0 Delphi
  trFW46kwzceA   2024年01月10日   20   0   0 Delphi
trFW46kwzceA