Object for Jsonvalue via Datasnap

Asked

Viewed 131 times

0

Ola, I have the following object:

  TResult = Class
  private
    FStatus  : TStatus;
    FMensagem: string;
    FEntidade: string;
    FLista   : TFDJSONDataSets; //tentei esse
    FLista2  : TFDMemTable;     //segunda tentativa - um ou outro...
  public
    constructor Create;
    Destructor Destroy; override;

    property Status  : TStatus         read FStatus write FStatus;
    property Mensagem: string          read FMensagem write FMensagem;
    property Entidade: string          read FEntidade write FEntidade;
    property Lista   : TFDJSONDataSets read FLista write FLista;
    property Lista2  : TFDMemTable     read FLista2 write FLista2;
  End;

on a datasnap server, I need to transport it to the client, so for this I am converting this object with:

function ResultToJSON(AResult: TResult): TJSONValue;
var
  m: TJSONMarshal;
begin
  if Assigned(AResult) then
  begin
    m := TJSONMarshal.Create(TJSONConverter.Create);
    try
      exit(m.Marshal(AResult))
    finally
      m.Free;
    end;
  end
  else
    exit(TJSONNull.Create);
end;

The List or List2 propertys is to carry the result of a query (n records), but in the "decommissioning" of the error, I have tried with Jsonarray...

If anyone can tell me how to transport the result of a query within an object, to be transported via datasnap, I really appreciate it.

addendum: popular list and Lista2:

function GetList(ATexto: string): TResult;
var _cds: TFDMemTable;
begin

  Result := TResult.Create;
  with result do
  begin
    try
      _cds := _objDAO.GetList(ATexto);

      if _cds <> nil then
      begin
        Status   := tsOK;
     //   Lista    := TFDJSONDataSets.Create;          --testes
     //   TFDJSONDataSetsWriter.ListAdd(Lista, _cds);  --testes
     //   Lista2   := _cds;                            --testes
      end
      else
        Status   := tsVazio;
    except
      on e:exception do
      begin
        Status   := tsErro;
        Mensagem := LimpaErro(e.Message);
      end;
    end;
  end;
end;
  • 1

    Hello. So, in JSON the correct thing is to carry an array of strings or objects. In your case I would define a second class with the fields you want to carry and in your Tresult object.List I would define an array of this second class. This is how I do in an old API in Delphi that I have that transports all kinds of data, lists and structures that I need. It’s no secret if you work like this.

  • What you can’t do is transport a few classes of Delphi by serializing and de-serializing again. That is, it creates a class of its own, de-serializes the object and then you initialize an instance of Tfdjsondatasets or Tfdmemtable and fill it with the already received data. Now transporting them directly will not give.

  • Thanks for your comment... really I did so, I did, I turned Flista into a string and I carry it as an arrayjson, a while ago I had trouble with this conversation, nor remember more of this function that I found and changed to solve the problem of fields memo and blob...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.