Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#6

AW: JSON String to Object mit factory

  Alt 11. Dez 2018, 10:03
Das Zauberwort ist polymorphic und damit lässt sich dann für Delphi das finden

GRIJJY Blog: Efficient and easy-to-use JSON and BSON library (sieht Abschnitt Polymorphism)
Delphi-Quellcode:
var
  Dog: TDog;
  Animal, Rehydrated: TAnimal;
  Json: String;
begin
  Dog.Weight := 30;
  Dog.FurColor := 'Blond';
  Animal := Dog;
  TgoBsonSerializer.Serialize(Animal, Json); // Result:
  // { "_t" : "TDog", "Weight" : 30.0, "FurColor" : "Blond" }

  TgoBsonSerializer.Deserialize(Json, Rehydrated);
  // This will actually create a TDog instance (instead of TAnimal)
end;
  Mit Zitat antworten Zitat