Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Array Properties (https://www.delphipraxis.net/211709-array-properties.html)

mytbo 25. Okt 2022 17:56

AW: Array Properties
 
Zitat:

Zitat von Rabenrecht (Beitrag 1513824)
Json Serialization ist der einzige Grund, warum ich hier überhaupt mit Arrays rumhühnere, anstatt einfach (Object)Listen zu nutzen.

Wenn es auch mORMot sein darf, dann mit ObjectToJson/ObjectLoadJson so:
Delphi-Quellcode:
uses
  mormot.core.base,
  mormot.core.json,
  mormot.core.text,
  mormot.core.rtti,
  mormot.core.unicode;
 
type
  TItem = class(TObject)
  private
    FIntValue: Integer;
    FStrValue: RawUtf8;
  public
    constructor Create(pmIntValue: Integer; const pmcStrValue: RawUtf8);
  published
    property IntValue: Integer
      read FIntValue;
    property StrValue: RawUtf8
      read FStrValue;
  end;

constructor TItem.Create(pmIntValue: Integer; const pmcStrValue: RawUtf8);
begin
  inherited Create;
  FIntValue := pmIntValue;
  FStrValue := pmcStrValue;
end;

var
  json: RawJson;
  list: TObjectList;
begin
  list := TObjectList.Create(True);
  try
    for var i: Integer := 0 to 4 do
      list.Add(TItem.Create(i, StringToUtf8(i.ToString)));

    json := ObjectToJson(list, [woHumanReadable, woObjectListWontStoreClassName]);
    ShowMessage(Utf8ToString(json));

    list.Clear;
    ObjectLoadJson(list, json, TItem);
    ShowMessage(Format('Count: %d - Item(3): %d, %s',[list.Count, TItem(list[3]).IntValue, Utf8ToString(TItem(list[3]).StrValue)]));
  finally
    list.Free;
  end;
Die JSON Serialisierung gehört zur mORMot DNA.

Bis bald...
Thomas

Dennis07 28. Okt 2022 11:18

AW: Array Properties
 
Zitat:

Zitat von himitsu (Beitrag 1513818)
Ja,
Delphi-Quellcode:
array of ...
deklariert jedes mal einen neuen Typ und untereinander sind diese Typen nicht zuweisungskompatibel,

Jain...
Also wenn es keine Mehrfachdeklaration ist, dann stimmt das. Ansonsten nicht. Ist das gleiche wie bei typisierten Zeigern.

Delphi-Quellcode:
var
  a1: array of ...
  b1: array of ...
  a2, b2: array of ...
begin
  a1 := b1; // geht nicht
  a2 := b2; // geht
end;
Beides ließe sich allerdings mit einem Hardcast zu einem dritten, kompatiblen Typen umgehen.

himitsu 28. Okt 2022 11:30

AW: Array Properties
 
a2 & b2: Jupp, EINE Typdeklaration für zwei Typenbezeichner ... die sind natürlich identisch.

Das
Delphi-Quellcode:
array of ...
selber ist jedes Mal was Neues,
aber
Delphi-Quellcode:
TArray<...>
nicht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:44 Uhr.
Seite 2 von 2     12   

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