Skip to content

Commit

Permalink
Added a Demo getting JSON from a API
Browse files Browse the repository at this point in the history
  • Loading branch information
JensBorrisholt committed Feb 1, 2024
1 parent 35ee294 commit a4be65f
Show file tree
Hide file tree
Showing 7 changed files with 1,969 additions and 0 deletions.
107 changes: 107 additions & 0 deletions Dummy JSON demos/DTO/DummyJson.ProductsDTO.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
unit DummyJson.ProductsDTO;

interface

uses
Pkg.Json.DTO, System.Generics.Collections, REST.Json.Types;

{$M+}

type
TProduct = class(TJsonDTO)
private
FBrand: string;
FCategory: string;
FDescription: string;
FDiscountPercentage: Double;
FId: Integer;
[JSONName('images')]
FImagesArray: TArray<string>;
[JSONMarshalled(False)]
FImages: TList<string>;
FPrice: Integer;
FRating: Double;
FStock: Integer;
FThumbnail: string;
FTitle: string;
function GetImages: TList<string>;
protected
function GetAsJson: string; override;
published
property Brand: string read FBrand write FBrand;
property Category: string read FCategory write FCategory;
property Description: string read FDescription write FDescription;
property DiscountPercentage: Double read FDiscountPercentage write FDiscountPercentage;
property Id: Integer read FId write FId;
property Images: TList<string> read GetImages;
property Price: Integer read FPrice write FPrice;
property Rating: Double read FRating write FRating;
property Stock: Integer read FStock write FStock;
property Thumbnail: string read FThumbnail write FThumbnail;
property Title: string read FTitle write FTitle;
public
destructor Destroy; override;
end;

TProductsDTO = class(TJsonDTO)
private
FLimit: Integer;
[JSONName('products'), JSONMarshalled(False)]
FProductsArray: TArray<TProduct>;
[GenericListReflect]
FProducts: TObjectList<TProduct>;
FSkip: Integer;
FTotal: Integer;
function GetProducts: TObjectList<TProduct>;
protected
function GetAsJson: string; override;
published
property Limit: Integer read FLimit write FLimit;
property Products: TObjectList<TProduct> read GetProducts;
property Skip: Integer read FSkip write FSkip;
property Total: Integer read FTotal write FTotal;
public
destructor Destroy; override;
end;

implementation

{ TProducts }

destructor TProduct.Destroy;
begin
GetImages.Free;
inherited;
end;

function TProduct.GetImages: TList<string>;
begin
Result := List<string>(FImages, FImagesArray);
end;

function TProduct.GetAsJson: string;
begin
RefreshArray<string>(FImages, FImagesArray);
Result := inherited;
end;

{ TRoot }

destructor TProductsDTO.Destroy;
begin
GetProducts.Free;
inherited;
end;

function TProductsDTO.GetProducts: TObjectList<TProduct>;
begin
Result := ObjectList<TProduct>(FProducts, FProductsArray);
end;

function TProductsDTO.GetAsJson: string;
begin
RefreshArray<TProduct>(FProducts, FProductsArray);
Result := inherited;
end;

end.
91 changes: 91 additions & 0 deletions Dummy JSON demos/Lib/DummyJson.Lib.Enumerator.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
unit DummyJson.Lib.Enumerator;

interface

uses
System.Classes, System.Generics.Collections,

Pkg.Json.DTO;

{$M+}

Type
TListeEumerator<TElement: TJsonDTO> = class(TComponent)
strict private
FDto: TJsonDTO;
FElements: TList<TElement>;
FCurrentIndex: Integer;
procedure DoChange;
function IndexInrange: Boolean;
function GetCurrent: TElement;
private
FOnChange: TNotifyEvent;
published
property Current: TElement read GetCurrent;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
public
constructor Create(aOwner: TComponent; aDTO: TJsonDTO; aElements: TList<TElement>); reintroduce;
destructor Destroy; override;
Function Next: TElement;
Function Previous: TElement;
end;

implementation

{ TListeEumerator<TDto, TElements> }

constructor TListeEumerator<TElement>.Create(aOwner: TComponent; aDTO: TJsonDTO; aElements: TList<TElement>);
begin
inherited Create(aOwner);
FDto := aDTO;
FElements := aElements;
FCurrentIndex := 0;
DoChange;
end;

destructor TListeEumerator<TElement>.Destroy;
begin
FDto.Free;
inherited;
end;

procedure TListeEumerator<TElement>.DoChange;
begin
if Assigned(FOnChange) then
FOnChange(Self);
end;

function TListeEumerator<TElement>.GetCurrent: TElement;
begin
if not IndexInrange then
Exit(nil);

Result := FElements[FCurrentIndex];
end;

function TListeEumerator<TElement>.IndexInrange: Boolean;
begin
if FElements.Count = 0 then
Exit(False);

Result := (FCurrentIndex >= 0) and (FCurrentIndex < FElements.Count);
end;

function TListeEumerator<TElement>.Next: TElement;
begin
FCurrentIndex := (FCurrentIndex + 1) mod FElements.Count;
Result := Current;
DoChange;
end;

function TListeEumerator<TElement>.Previous: TElement;
begin
FCurrentIndex := FCurrentIndex - 1;
if FCurrentIndex < 0 then
FCurrentIndex := FElements.Count - 1;

Result := Current;
DoChange;
end;

end.
Loading

0 comments on commit a4be65f

Please sign in to comment.