type
TIngredient = class(TPersistent) // class for the property 'obj' and the TCollection
private
fid_ingredient: String;
fnom_ingredient: String;
Fprix_ingredient: Currency;
published
property id_ingredient : String read fid_ingredient write fid_ingredient;
property nom_ingredient : String read fnom_ingredient write fnom_ingredient;
property prix_ingredient : Currency read Fprix_ingredient write Fprix_ingredient;
end;
{ TProduit }
TProduit = class(TCollectionItem) // class for the property 'obj' and the TCollection
private
fid_produit: String;
fnom_produit: String;
Fqte: Double;
Fprixp: Currency;
fingredient : TIngredient;
public
constructor Create;
destructor Destroy; override;
published
property id_produit : String read fid_produit write fid_produit;
property nom_produit : String read fnom_produit write fnom_produit;
property qte : Double read Fqte write Fqte;
property prix : Currency read Fprixp write Fprixp;
property ingredient : TIngredient read fingredient;
//property total_commande: Currency read Ftotal_commande write Ftotal_commande;
//property client: TStrings read fclient;
end;
{ TProduits }
TProduits = class(TCollection) // class for the property 'obj' and the TCollection
private
Fproduit: TProduit;
public
constructor Create;
destructor Destroy; override;
published
property produit : TProduit read Fproduit write Fproduit;
end;
TVille = class(TPersistent) // class for the property 'obj' and the TCollection
private
fid_ville: String;
//fdate_commande: TDate;
//ftotal_commande: Currency;
fnom_ville: String;
published
property id_ville : String read fid_ville write fid_ville;
property nom_ville : String read fnom_ville write fnom_ville;
//property total_commande: Currency read Ftotal_commande write Ftotal_commande;
//property client: TStrings read fclient;
end;
{ TClient }
TClient = class(TPersistent) // class for the property 'obj' and the TCollection
private
fid_client: String;
fnom : String;
//ftotal_commande: Currency;
// fpayment: TStrings;
fville : TVille;
public
constructor Create;
destructor Destroy; override;
published
property id_client : String read fid_client write fid_client;
property nom : String read fnom write fnom;
// property payment : TStrings read fpayment;
property ville : TVille read fville;
//property date_commande: TDate read Fdate_commande write Fdate_commande;
//property total_commande: Currency read Ftotal_commande write Ftotal_commande;
//property client: TStrings read fclient;
end;
{ TResult }
TResult = class(TPersistent) // class for the property 'obj' and the TCollection
private
fid_commande: String;
fdate_commande: String;
ftotal_commande: Currency;
fclient: TClient;
fproduits: TCollection;
fpayment : TStrings;
public
constructor Create;
destructor Destroy; override;
published
property id_commande : String read Fid_commande write Fid_commande;
property date_commande: String read Fdate_commande write Fdate_commande;
property total_commande: Currency read Ftotal_commande write Ftotal_commande;
property client: TClient read fclient write fclient;
property payment: TStrings read fpayment;
property produits: TCollection read fproduits ;
end;
{ TBaseObject }
TBaseObject = class(TPersistent) // class for the JSON structure
private
fcode: String;
fmessage: String;
fresult: TResult;
public
constructor Create;
destructor Destroy; override;
published // all properties have published
property code: String read fcode write fcode;
property message: String read fmessage write fmessage;
property result: TResult read fresult write fresult;
end;
implementation
{$R *.lfm}
{ TProduits }
constructor TProduits.Create;
begin
Fproduit:=TProduit.Create;
end;
destructor TProduits.Destroy;
begin
inherited Destroy;
end;
{ TProduit }
constructor TProduit.Create;
begin
fingredient:=TIngredient.Create;
end;
destructor TProduit.Destroy;
begin
fingredient.Free;
inherited Destroy;
end;
{ TClient }
constructor TClient.Create;
begin
fville:= TVille.Create;
end;
destructor TClient.Destroy;
begin
inherited Destroy;
end;
{ TResult }
constructor TResult.Create;
begin
fclient := TClient.Create;
fproduits := TCollection.Create(TProduit);
fpayment := TStringList.Create;
end;
destructor TResult.Destroy;
begin
fclient.Free;
inherited Destroy;
end;
{ TBaseObject }
constructor TBaseObject.Create;
begin
fResult := TResult.Create;
end;
destructor TBaseObject.Destroy;
begin
fResult.Free;
inherited Destroy;
end;
procedure TForm1.DeStreamTest;
var
DeStreamer: TJSONDeStreamer;
o: TBaseObject;
no: TResult;
pd : TProduit;
s: String;
Ing : TIngredient;
begin
Memo1.lines.add('DeStream test');
Memo1.lines.add('======================================');
// DeStreamer object and target object create
DeStreamer := TJSONDeStreamer.Create(nil);
o := TBaseObject.Create;
try
DeStreamer.JSONToObject(Memo2.Text, o);
Memo1.lines.add(o.code);
Memo1.lines.add(o.message);
Memo1.lines.add('ID Commande : '+o.result.id_commande);
Memo1.lines.add('Toatal Commande : '+CurrToStr(o.result.total_commande));
Memo1.lines.add('Client : Code : '+o.result.client.id_client);
Memo1.lines.add('Client : Nom : '+o.result.client.nom);
Memo1.lines.add('Client : Ville : Code : '+o.result.client.ville.id_ville);
Memo1.lines.add('Client : Ville : nom ville : '+o.result.client.ville.nom_ville);
for s in o.result.payment do
Memo1.lines.add('Client : Payement : '+ s);
for TCollectionItem(pd) in o.result.produits do
Begin
Memo1.lines.add('Produit : '+pd.qte.ToString()+' '+pd.nom_produit+' Px: '+CurrToStr(pd.prix));
Memo1.lines.add('Produit : Ingrédient : '+pd.ingredient.nom_ingredient+' Px: '+CurrToStr(pd.ingredient.prix_ingredient));
end;
finally
o.Destroy;
DeStreamer.Destroy;
end;
end;