Einzelnen Beitrag anzeigen

Hobbycoder

Registriert seit: 22. Feb 2017
955 Beiträge
 
#19

AW: TJSON.JSONToObject DateTime als LocalTime

  Alt 1. Nov 2023, 11:53
Hier mal die Unit der Klasse

Delphi-Quellcode:
unit uUserstatus;

interface

uses System.Classes, system.sysutils, System.Generics.Collections, REST.Json;

type
  TUserStatus=(usStarted, usStopped, usInIdle, usInWork);

  TUserEntry=class
  private
    FDuration: Cardinal;
    FStatus: TUserStatus;
    FTime: TDateTime;
    FUsername: string;
    FGuid: string;
    FUserGuid: string;
    procedure SetDuration(const Value: Cardinal);
    procedure SetStatus(const Value: TUserStatus);
    procedure SetTime(const Value: TDateTime);
    procedure SetUsername(const Value: string);
    procedure SetGuid(const Value: string);
    procedure SetUserGuid(const Value: string);
  public
    procedure Assign(Source: TObject);
    function ToJson: string;
    procedure FromJson(Str: string);
  published
    property UserGuid: string read FUserGuid write SetUserGuid;
    property Guid: string read FGuid write SetGuid;
    property Username: string read FUsername write SetUsername;
    property Time: TDateTime read FTime write SetTime;
    property Status: TUserStatus read FStatus write SetStatus;
    property Duration: Cardinal read FDuration write SetDuration;
  end;

  TUserList=class(TObjectList<TUserEntry>)
  end;

implementation

{ TUserEntry }

procedure TUserEntry.Assign(Source: TObject);
begin
  if Source is TUserEntry then
  begin
    self.FUserGuid:=TUserEntry(Source).UserGuid;
    self.FGuid:=TUserEntry(Source).Guid;
    self.FUsername:=TUserEntry(Source).Username;
    self.FTime:=TUserEntry(Source).Time;
    self.FStatus:=TUserEntry(Source).Status;
    self.FDuration:=TUserEntry(Source).Duration;
  end;
end;

procedure TUserEntry.FromJson(Str: string);
var
  UE: TUserEntry;
begin
  UE:=TJson.JsonToObject<TUserEntry>(Str, [joIgnoreEmptyStrings, joIgnoreEmptyArrays]);
  try
    Self.Assign(UE);
  finally
    UE.Free;
  end;
end;

procedure TUserEntry.SetDuration(const Value: Cardinal);
begin
  FDuration := Value;
end;

procedure TUserEntry.SetGuid(const Value: string);
begin
  FGuid := Value;
end;

procedure TUserEntry.SetStatus(const Value: TUserStatus);
begin
  FStatus := Value;
end;

procedure TUserEntry.SetTime(const Value: TDateTime);
begin
  FTime := Value;
end;

procedure TUserEntry.SetUserGuid(const Value: string);
begin
  FUserGuid := Value;
end;

procedure TUserEntry.SetUsername(const Value: string);
begin
  FUsername := Value;
end;

function TUserEntry.ToJson: string;
begin
  Result:=TJson.ObjectToJsonString(Self, [joIgnoreEmptyStrings, joIgnoreEmptyArrays]);
end;

end.
Gesendet wirde über IdHTTP
Delphi-Quellcode:
    UEStr:=FUserEntry.ToJson;
    Response:=FHttp.Post('http://'+FHost+':'+FPort.ToString+TIdURI.ParamsEncode('/upe.php?userentry='+UEStr), ss);
und empfangen über IdHTTPServer
Delphi-Quellcode:
    UEStr:=ARequestInfo.Params.Values['userentry'];
    if UEStr<>'then
    begin
      UE:=TUserEntry.Create;
      try
        UE.FromJson(UEStr);
        UserSettingsList.LoadFromFile(FPath+'usersettings.dat');
        DoUpdateEntry(UE);
      finally
        UE.Free;
      end;
    end;
Gruß Hobbycoder
Alle sagten: "Das geht nicht.". Dann kam einer, der wusste das nicht, und hat's einfach gemacht.
  Mit Zitat antworten Zitat