Einzelnen Beitrag anzeigen

Benutzerbild von Uwe Raabe
Uwe Raabe
Online

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.453 Beiträge
 
Delphi 12 Athens
 
#22

AW: ObjectList serialisieren JSON

  Alt 12. Nov 2021, 10:23
Funktioniert doch tadellos.
Delphi-Quellcode:
type
  TPerson = class
  private
    FName: String;
  public
    property Name: String read FName write FName;
  end;

  JsonObjectListPersonAttribute = class(JsonObjectListAttribute<TPerson>);

type
  [JsonObjectListPerson]
  TPersonList = class(TObjectList<TPerson>)
  end;

type
  TFirma = class
  private
    FName: string;
    [JSONOwned(False)]
    FPersonList: TPersonList;
  public
    constructor Create;
    destructor Destroy; override;
    property Name: string read FName write FName stored False;
    property PersonList: TPersonList read FPersonList write FPersonList;
  end;

  JsonObjectListFirmaAttribute = class(JsonObjectListAttribute<TFirma>);

type
  [JsonObjectListFirma]
  TFirmaList = class(TObjectList<TFirma>)
  end;

type
  { Wrapper }
  TWrapper = class
  private
    [JSONOwned(False)]
    FList: TFirmaList;
  public
    constructor Create;
    destructor Destroy; override;
    property List: TFirmaList read FList write FList;
  end;

...

procedure Test;
const
  cFirmen: TArray<String> = ['Firma1', 'Firma2', 'Firma3'];
  cNamen: TArray<String> = ['Name1', 'Name2', 'Name3'];
  cPersonList = '[{"name":"Name1"},{"name":"Name2"},{"name":"Name3"}]';
  cFirmaList = '[{"name":"Firma1","personList":' + cPersonList + '},' +
               '{"name":"Firma2","personList":' + cPersonList + '},' +
               '{"name":"Firma3","personList":' + cPersonList + '}]';
  cWrapperJSON = '{"list":' + cFirmaList + '}';

  function CheckList(AListe: TPersonList): Boolean; overload;
  var
    I: Integer;
  begin
    Result := (AListe.Count = Length(cNamen));
    if Result then begin
      for I := 0 to AListe.Count - 1 do
        if AListe[I].Name <> cNamen[I] then
          Exit(False);
    end;
  end;

  function CheckList(AListe: TFirmaList): Boolean; overload;
  var
    I: Integer;
  begin
    Result := (AListe.Count = Length(cFirmen));
    if Result then begin
      for I := 0 to AListe.Count - 1 do begin
        if AListe[I].Name <> cFirmen[I] then
          Exit(False);
        if not CheckList(AListe[I].PersonList) then
          Exit(False);
      end;
    end;
  end;

  procedure FillList(Target: TPersonList); overload;
  begin
    for var lName in cNamen do
    begin
      var lPerson := TPerson.Create;
      lPerson.Name := lName;
      Target.Add(lPerson);
    end;
  end;

  procedure FillList(Target: TFirmaList); overload;
  begin
    for var lName in cFirmen do
    begin
      var lFirma := TFirma.Create;
      lFirma.Name := lName;
      FillList(lFirma.PersonList);
      Target.Add(lFirma);
    end;

  end;

var
  lFirma: TFirma;
  lWrapper: TWrapper;
  lJSONString: String;
  lFirmaList: TFirmaList;
begin
  { mit Wrapper }
  lWrapper := TWrapper.Create;
  try
    FillList(lWrapper.List);
    lJSONString := TConvert.ToJSONString(lWrapper);
    Assert(lJSONString = cWrapperJSON);
  finally
    lWrapper.Free;
  end;

  lWrapper := TConvert.FromJSON<TWrapper>(cWrapperJSON);
  try
    Assert(CheckList(lWrapper.List));
  finally
    lWrapper.Free;
  end;

  { Liste direkt }
  lFirmaList := TFirmaList.Create;
  try
    FillList(lFirmaList);
    lJSONString := TConvert.ToJSONString(lFirmaList);
    Assert(lJSONString = cFirmaList);
  finally
    lFirmaList.Free;
  end;

  lFirmaList := TFirmaList.Create;
  try
    lFirmaList.AddRange(TConvert.FromJSONArray<TFirma>(cFirmaList));
    Assert(CheckList(lFirmaList));
  finally
    lFirmaList.Free;
  end;
end;
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat