unit Azure.CognitiveServices.FindFaces.JSON;

interface

uses
  System.Generics.Collections,
  System.JSON,
  System.SysUtils,
  Azure.CognitiveServices.FindFaces.Types;

type

  TAcsFindFacesJSON = class
  public
    class function ToJSON(const AData: TAcsFindFacesRequest): TJSONObject; overload;
    class procedure Parse(const AObject: TJSONObject; var AData: TAcsFindFacesResponse); overload;
    class procedure Parse(const AArray: TJSONArray; var AData: TArray<TAcsFindFacesItem>); overload;
    class procedure Parse(const AObject: TJSONObject; var AData: TAcsFindFacesItem); overload;
  end;

implementation

class procedure TAcsFindFacesJSON.Parse(const AObject: TJSONObject;
  var AData: TAcsFindFacesResponse);
begin
  AData.TrackingId := AObject.GetValue<string>('TrackingId');
  if not AObject.TryGetValue<string>('CacheId', AData.CacheId) then
    AData.CacheId := EmptyStr;
  AData.Result := AObject.GetValue<Boolean>('Result');
  AData.Count := AObject.GetValue<Integer>('Count');
  Parse(TJSONArray(AObject.GetValue('Faces')), AData.Faces);
end;

class procedure TAcsFindFacesJSON.Parse(const AArray: TJSONArray;
  var AData: TArray<TAcsFindFacesItem>);
var
  LIndex: Integer;
begin
  SetLength(AData, AArray.Count);
  for LIndex := 0 to AArray.Count - 1 do
    Parse(TJSONObject(AArray[LIndex]), AData[LIndex]);
end;

class procedure TAcsFindFacesJSON.Parse(const AObject: TJSONObject;
  var AData: TAcsFindFacesItem);
begin
  AData.Bottom := AObject.GetValue<Integer>('Bottom');
  AData.Left := AObject.GetValue<Integer>('Left');
  AData.Right := AObject.GetValue<Integer>('Right');
  AData.Top := AObject.GetValue<Integer>('Top');
end;

class function TAcsFindFacesJSON.ToJSON(const AData: TAcsFindFacesRequest): TJSONObject;
begin
  Result := TJSONObject.Create;
  if Length(AData.ImageUrl) <= 0 then
    Exit;
  Result.AddPair('DataRepresentation', 'URL');
  Result.AddPair('Value', AData.ImageUrl);
end;

end.
