AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Interface mit TJsonSerializer

Ein Thema von Nathan · begonnen am 18. Aug 2017 · letzter Beitrag vom 21. Aug 2017
 
Nathan

Registriert seit: 6. Nov 2003
Ort: Birmensdorf / Zürich
29 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#1

Interface mit TJsonSerializer

  Alt 18. Aug 2017, 10:55
Hallo

Experimentiere gerade mit der Klasse TJsonSerializer aus System.JSON.Serializers von Tokyo. Scheint ziemlich gut und einfach zu bedienen zu sein.
Da bei mir jede Klasse von einem Interface kommt, sind Properties immer an ein Interface gebunden. Der Serializer serialisiert aber nur Klassen, keine Interface.
Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.JSON.Serializers,
  System.JSON.Types,
  System.SysUtils;

{$M+}

type
  IFooOne = interface
    ['{1DA05420-61BD-404F-BD6F-15D638FE0CB5}']
    function GetOneProp(): Integer;
    procedure SetOneProp(Value: Integer);

    property OneProp: Integer read GetOneProp write SetOneProp;
  end;

  IFooSecond = interface
    ['{0D31D4A7-F31A-4B1A-8767-0DBD08BD97EA}']
    function GetSecondProp(): string;
    procedure SetSecondProp(const Value: string);

    function GetSecondObj(): IFooOne;
    procedure SetSecondObj(const Value: IFooOne);

    function ToJson(): string;

    property SecondProp: string read GetSecondProp write SetSecondProp;
    property SecondObj: IFooOne read GetSecondObj write SetSecondObj;
  end;


  TFooOne = class(TInterfacedObject, IFooOne)
  strict private
    FOneProp: Integer;
  private
    function GetOneProp(): Integer;
    procedure SetOneProp(Value: Integer);
  end;

  [JsonSerialize(TJsonMemberSerialization.&In)]
  TFooSecond = class(TInterfacedObject, IFooSecond)
  strict private
    [JsonName('SecondProp')] [JsonIn] FSecondProp: string;
    [JsonName('SecondObj')] [JsonIn] FSecondObj: IFooOne;
  private
    function GetSecondProp(): string;
    procedure SetSecondProp(const Value: string);

    function GetSecondObj(): IFooOne;
    procedure SetSecondObj(const Value: IFooOne);
  public
    function ToJson(): string;
  end;

{$M-}

{ TFooOne }

function TFooOne.GetOneProp: Integer;
begin
  Result := FOneProp;
end;

procedure TFooOne.SetOneProp(Value: Integer);
begin
  FOneProp := Value;
end;

{ TFooSecond }

function TFooSecond.GetSecondProp(): string;
begin
  Result := FSecondProp;
end;

function TFooSecond.GetSecondObj(): IFooOne;
begin
  Result := FSecondObj;
end;

procedure TFooSecond.SetSecondProp(const Value: string);
begin
  FSecondProp := Value;
end;

procedure TFooSecond.SetSecondObj(const Value: IFooOne);
begin
  FSecondObj := Value;
end;

function TFooSecond.ToJson: string;
var
  Serializer: TJsonSerializer;
begin
  Serializer := TJsonSerializer.Create;
  try
    Serializer.Formatting := TJsonFormatting.Indented;
    Result := Serializer.Serialize(Self);
  finally
    Serializer.Free;
  end;
end;


var
  Return: string;
  FooImpl1: IFooOne;
  FooImpl2: IFooSecond;

begin
  ReportMemoryLeaksOnShutdown := True;

  FooImpl1 := TFooOne.Create;
  FooImpl1.OneProp := 4711;

  FooImpl2 := TFooSecond.Create;
  FooImpl2.SecondProp := 'My second property.';
  FooImpl2.SecondObj := FooImpl1;
  Return := FooImpl2.ToJson;
  Writeln(Return);
  Writeln('');

  Writeln('Press any key...');
  ReadLn;
end.
Das Ergebnis sieht dann so aus:
Code:
{
  "SecondProp": "My second property.",
  "SecondObj": {}
}
anstatt:
Code:
{
  "SecondProp": "My second property.",
  "SecondObj": {
    "FOneProp": 4711,
    "FRefCount": 2
  }
}
Weiß jemand wie ich das Interface Property Serialisieren kann ohne eine Instanzvariable daraus zu machen und mich dann um die Freigabe zu kümmern?
Die Doku von Embarcadero zu TJsonSerializer ist nicht gerade voll mit Beispielen.
Danke schon mal.
Nathan Chanan Thurnreiter
  Mit Zitat antworten Zitat
 


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 03:48 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