unit XData.Json.Serializer;

{$I XData.inc}

interface

uses
  Generics.Collections,
  Bcl.Json.BaseObjectConverter,
  Bcl.Json.Converters,
  Bcl.Json.Serializer,
  Bcl.Json.Writer,
  XData.Aurelius.Model,
  XData.Json.Converters,
  XData.Json.Solver;

type
  TXDataJsonClientSerializer = class(TJsonSerializer)
  private
    FConverters: TXDataJsonConverters;
    function GetForceProxyLoading: boolean;
    procedure SetForceProxyLoading(const Value: boolean);
    function GetForceBlobLoading: boolean;
    procedure SetForceBlobLoading(const Value: boolean);
  public
    constructor Create(AModel: TXDataAureliusModel);
    destructor Destroy; override;
    property ForceProxyLoading: boolean read GetForceProxyLoading write SetForceProxyLoading;
    property ForceBlobLoading: boolean read GetForceBlobLoading write SetForceBlobLoading;
  end;

  TXDataJsonServerSerializer = class(TJsonSerializer)
  private
    FConverters: TXDataJsonConverters;
  public
    constructor Create(AModel: TXDataAureliusModel);
    destructor Destroy; override;
    procedure SetExpandLevel(const Value: Integer);
    procedure SetExpander(Value: IJsonExpander);
    procedure SetSerializeInstanceRef(Value: TInstanceRefSerialization);
    procedure SetSerializeInstanceType(Value: TInstanceTypeSerialization);
  end;

implementation

uses
  XData.Consts;

{ TXDataJsonClientSerializer }

constructor TXDataJsonClientSerializer.Create(AModel: TXDataAureliusModel);
begin
  FConverters := TXDataJsonConverters.Create(AModel);
  FConverters.OnObjectProcessed := Self.ObjectWritten;
  FConverters.EntityFactory.SerializeBlobReferences := false;
  FConverters.EntityFactory.ExpandLevel := MaxInt;
  inherited Create(FConverters, False);
end;

destructor TXDataJsonClientSerializer.Destroy;
begin
  FConverters.Free;
  inherited;
end;

function TXDataJsonClientSerializer.GetForceBlobLoading: boolean;
begin
  Result := FConverters.EntityFactory.ForceBlobLoading;
end;

function TXDataJsonClientSerializer.GetForceProxyLoading: boolean;
begin
  Result := FConverters.EntityFactory.ForceProxyLoading;
end;

procedure TXDataJsonClientSerializer.SetForceBlobLoading(const Value: boolean);
begin
  FConverters.EntityFactory.ForceBlobLoading := Value;
end;

procedure TXDataJsonClientSerializer.SetForceProxyLoading(const Value: boolean);
begin
  FConverters.EntityFactory.ForceProxyLoading := Value;
end;

{ TXDataJsonServerSerializer }

constructor TXDataJsonServerSerializer.Create(AModel: TXDataAureliusModel);
begin
  FConverters := TXDataJsonConverters.Create(AModel);
  FConverters.OnObjectProcessed := Self.ObjectWritten;
  FConverters.EntityFactory.SerializeBlobReferences := True;
  FConverters.EntityFactory.ExpandLevel := 0;
  inherited Create(FConverters, False);
end;

destructor TXDataJsonServerSerializer.Destroy;
begin
  FConverters.Free;
  inherited;
end;

procedure TXDataJsonServerSerializer.SetExpandLevel(const Value: Integer);
begin
  FConverters.EntityFactory.ExpandLevel := Value;
end;

procedure TXDataJsonServerSerializer.SetSerializeInstanceRef(
  Value: TInstanceRefSerialization);
begin
  FConverters.EntityFactory.InstanceRefSerialization := Value;
end;

procedure TXDataJsonServerSerializer.SetSerializeInstanceType(
  Value: TInstanceTypeSerialization);
begin
  FConverters.EntityFactory.InstanceTypeSerialization := Value;
end;

procedure TXDataJsonServerSerializer.SetExpander(Value: IJsonExpander);
begin
  FConverters.EntityFactory.Expander := Value;
end;

end.

