AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

TJSON.JSONToObject DateTime als LocalTime

Ein Thema von Hobbycoder · begonnen am 1. Nov 2023 · letzter Beitrag vom 1. Nov 2023
 
Hobbycoder

Registriert seit: 22. Feb 2017
1.002 Beiträge
 
#17

AW: TJSON.JSONToObject DateTime als LocalTime

  Alt 1. Nov 2023, 10: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
 

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:52 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz