Einzelnen Beitrag anzeigen

mytbo

Registriert seit: 8. Jan 2007
461 Beiträge
 
#11

AW: Array Properties

  Alt 25. Okt 2022, 17:56
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
  Mit Zitat antworten Zitat