Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 15 total
Thread Help with TServerRequests and JSON...
Tue, Jun 7 2016 3:46 PMPermanent Link

Mario Enríquez

Open Consult

Hello, I'm new to EWB and still trying to fully understand it.

The forms and components aspect are really well made and easy to understand to someone with a Delphi background.

It is the Data/Requests that are becoming a really stop block.

I couldn't find a sample or figure out how to use a TServerRequest to call a rest api (ASP.NET) and parse the returning JSON.

I've read about server modules, datasets etc. but it doesn't apply to my need since I'm somewhat tied to an existing backend (Microsoft WebApi).

Could anybody please share or point me to some sample EWB source code on how to deal with this scenario?

Thanks in advance.

Regards,
Mario
Tue, Jun 7 2016 4:16 PMPermanent Link

Raul

Team Elevate Team Elevate

On 6/7/2016 3:46 PM, Mario Enr�quez wrote:
> It is the Data/Requests that are becoming a really stop block.
> I couldn't find a sample or figure out how to use a TServerRequest to call a rest api (ASP.NET) and parse the returning JSON.

Have you tried the server request sample in the manual ?

http://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=Executing_Request

Any sample source code would essentially be a copy of code that's on
that page. you still need to use proper URL and params for your web service.

It shows you how to create a basic server request and show the data
returned (i would just print out everything returned initially).

I'd start with that to ensure you can connect first - meaning you're not
running into any authentication or cross origin issues (if you're
hosting your EWB app from same IIS server that your WebAPI is on then
all should just work; otherwise you need to ensure IIS allows CORS).

Once you know you see the data coming back you can look into parsing it
- depending on complexity of JSON you have few options but TReader would
be best way.

See "Object Persistence" example or these postings in newsgroups to get
started :

www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_general&page=1&msg=9704

and

www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_general&page=1&msg=5787


Raul
Wed, Jun 8 2016 5:53 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Mario,

<< I couldn't find a sample or figure out how to use a TServerRequest to call a rest api (ASP.NET) and parse the returning JSON. >>

Raul is correct, what you want is the TReader to read (parse) the JSON returned from the TServerRequest:

http://www.elevatesoft.com/manual?action=viewcomp&id=ewb2&comp=TReader

If you want to have EWB do this for you, then you can use a TPersistent-descendant class with published properties that match those of the incoming JSON:

http://www.elevatesoft.com/manual?action=viewcomp&id=ewb2&comp=TPersistent

As Raul says, the Object Persistence example application shows exactly how this is done:

http://www.elevatesoft.com/manual?action=viewtopic&id=ewb2&topic=Example_Applications

If you're still stuck, then post the JSON API spec here and we'll help you further.   The Object Persistence example doesn't cover arrays/list properties, so sometimes that can complicate things.  But, they're easily handled, either manually in your TPersistent-descendant or by using the TCollection class for any list properties:

http://www.elevatesoft.com/manual?action=viewcomp&id=ewb2&comp=TCollection

EWB itself uses the JSON persistence to load forms at runtime.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Jun 8 2016 7:57 PMPermanent Link

Mario Enríquez

Open Consult

Thank you very much folks! That's exactly the information I was looking out but couldn't find (or didn't know how to search... :-| ).

Tim, I must definitly will be working with collections/arrays to fill out a TGrid. I will try to find my way around the TPersistent class and if reach a dead end I'll post the JSON spec.


Regards,
Mario
Tue, Jun 21 2016 5:46 PMPermanent Link

Mario Enríquez

Open Consult

Tim, I review the TPersistent sample project and it clear on how to manage a single instance.

However, I'm having trouble load an array. Could you please post a plain sample on how to deal with arrays/collections?

Here's a sample of the JSON I need to parse.


[
{
  "Compania":"CS",
  "Taller":"001",
  "Codigo":"001",
  "Descripcion":"EQUIPO 1"
},
{
   "Compania":"CS",
   "Taller":"001",
   "Codigo":"003",
   "Descripcion":"EQUIPO 3"
}
]

And the class to represent a single instance of this array should be the following:

  TEPEquipo = class(TPersistent)
  private
     FCompania: string;
     FTaller: string;
     FCodigo: string;
     FDescripcion: double;
  public           
     property Compania: string read FCompania write FCompania;
     property Taller: string read FTaller write FTaller;
     property Codigo: string read FCodigo write FCodigo;
     property Descripcion: double read FDescripcion write FDescripcion;
  end;

Regards,
Mario
Wed, Jun 22 2016 4:58 AMPermanent Link

Matthew Jones

Mario Enríquez wrote:

> However, I'm having trouble load an array. Could you please post a
> plain sample on how to deal with arrays/collections?

http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_general&page=2&msg=5787

That has all the detail, and if you search for the functions like
LoadProperty you will find a few follow up questions and answers. Once
you understand it, it is very easy to do any type of array (I have one
object with 5 arrays, and it needs a flag to tell the creator which
type of object to add to which list, but that's easy to do).

--

Matthew Jones
Thu, Jun 23 2016 10:38 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Mario,

<< However, I'm having trouble load an array. Could you please post a plain sample on how to deal with arrays/collections? >>

If you define a TCollection descendant class (along with making your class a TCollectionItem descendant class), then you can just use the persistence built into the TCollection class to load the JSON:

type

  TEPEquipo = class(TCollectionItem)
  private
     FCompania: string;
     FTaller: string;
     FCodigo: string;
     FDescripcion: string;
  published
     property Compania: string read FCompania write FCompania;
     property Taller: string read FTaller write FTaller;
     property Codigo: string read FCodigo write FCodigo;
     property Descripcion: string read FDescripcion write FDescripcion;
  end;

  TEPEquipos = class(TCollection)
     public
        constructor Create; override;
        procedure LoadFromJSON(const Value: String);
     end;

implementation

{ TEPEquipos }

constructor TEPEquipos.Create;
begin
  inherited Create(TEPEquipo);
end;

procedure TEPEquipos.LoadFromJSON(const Value: String);
var
  TempReader: TReader;
begin
  TempReader:=TReader.Create;
  try
     TempReader.Initialize(Value);
     Load(TempReader);
  finally
     TempReader.Free;
  end;
end;

And, finally, this is how you would use it:

procedure TfrmMain.Button3Click(Sender: TObject);
var
  TempEquipos: TEPEquipos;
begin
  TempEquipos:=TEPEquipos.Create;
  try
     TempEquipos.LoadFromJSON(MultiLineEdit1.Lines.Text); // Or load it from a TServerRequest, etc.
  finally
     TempEquipos.Free;
  end;
end;

If you need to save to JSON, then that's a bit of an issue still because, when working up this example, I noticed that the TCollection and TStrings classes don't have the "Save" portion of persistence implemented.  I've added this for 2.05, so it should be available soon.

Tim Young
Elevate Software
www.elevatesoft.com
Thu, Jun 23 2016 1:04 PMPermanent Link

Mario Enríquez

Open Consult

Thank you very much Tim, it makes sense now.

Regards,
Mario

Tim Young [Elevate Software] wrote:

Mario,

<< However, I'm having trouble load an array. Could you please post a plain sample on how to deal with arrays/collections? >>

If you define a TCollection descendant class (along with making your class a TCollectionItem descendant class), then you can just use the persistence built into the TCollection class to load the JSON:

type

  TEPEquipo = class(TCollectionItem)
  private
     FCompania: string;
     FTaller: string;
     FCodigo: string;
     FDescripcion: string;
  published
     property Compania: string read FCompania write FCompania;
     property Taller: string read FTaller write FTaller;
     property Codigo: string read FCodigo write FCodigo;
     property Descripcion: string read FDescripcion write FDescripcion;
  end;

  TEPEquipos = class(TCollection)
     public
        constructor Create; override;
        procedure LoadFromJSON(const Value: String);
     end;

implementation

{ TEPEquipos }

constructor TEPEquipos.Create;
begin
  inherited Create(TEPEquipo);
end;

procedure TEPEquipos.LoadFromJSON(const Value: String);
var
  TempReader: TReader;
begin
  TempReader:=TReader.Create;
  try
     TempReader.Initialize(Value);
     Load(TempReader);
  finally
     TempReader.Free;
  end;
end;

And, finally, this is how you would use it:

procedure TfrmMain.Button3Click(Sender: TObject);
var
  TempEquipos: TEPEquipos;
begin
  TempEquipos:=TEPEquipos.Create;
  try
     TempEquipos.LoadFromJSON(MultiLineEdit1.Lines.Text); // Or load it from a TServerRequest, etc.
  finally
     TempEquipos.Free;
  end;
end;

If you need to save to JSON, then that's a bit of an issue still because, when working up this example, I noticed that the TCollection and TStrings classes don't have the "Save" portion of persistence implemented.  I've added this for 2.05, so it should be available soon.

Tim Young
Elevate Software
www.elevatesoft.com
Thu, Jun 23 2016 3:07 PMPermanent Link

Mario Enríquez

Open Consult

Tim,

I just test your sample and it compiles and runs without error but the TCollectionItem's properties are empty.

Here's my class definition:

  TCOBahia = class(TCollectionItem)
  private
     FCompania: string;
     FTaller: string;
     FCodigo: string;
     FDescripcion: string;
  public           
     property Compania: string read FCompania write FCompania;
     property Taller: string read FTaller write FTaller;
     property Codigo: string read FCodigo write FCodigo;
     property Descripcion: string read FDescripcion write FDescripcion;
  end;

 TCOBahias = class(TCollection)
    public
       constructor Create; override;
       procedure LoadFromJSON(const Value: String);
    end;


And the code I used to load the JSON from a ServerRequest OnComplete event:

procedure TfmEPPlanificacion.srGetBahiasComplete(Request: TServerRequest);
var
  resultado: string;
begin
  if (Request.StatusCode = HTTP_OK) then
  begin
     resultado := Request.ResponseContent.Text;

     ShowMessage(resultado);

     FBahias := TCOBahias.Create;

     FBahias.LoadFromJSON(resultado);
     
     ShowMessage('Total Bahias:' + IntToStr(FBahias.Count));

     ShowMessage('Bahia No.1: ' + TCOBahia(FBahias.Items[0]).Codigo);
  end
  else
  begin
     resultado := Request.StatusText;
     ShowMessage(resultado);
  end;
end;

The actual result from the ServerRequest as show on the ShowMessage right after reading the ResponseContent.Text is:

[{"Compania":"CS","Taller":"011","Codigo":"301","Descripcion":"ENDEREZADO 1"},{"Compania":"CS","Taller":"011","Codigo":"302","Descripcion":"ENDEREZADO 2"},{"Compania":"CS","Taller":"011","Codigo":"303","Descripcion":"PREPARADO 1"},{"Compania":"CS","Taller":"011","Codigo":"304","Descripcion":"PREPARADO 2"},{"Compania":"CS","Taller":"011","Codigo":"305","Descripcion":"PREPARADO 3"},{"Compania":"CS","Taller":"011","Codigo":"306","Descripcion":"PINTURA"},{"Compania":"CS","Taller":"011","Codigo":"307","Descripcion":"PULIDO"},{"Compania":"CS","Taller":"011","Codigo":"308","Descripcion":"ARMADO"}]

The Collection is correctly populated with 8 elements as tested by ShowMessage('Total Bahias:' + IntToStr(FBahias.Count)).

But the all the the TCOBahia instances (TCollectionItem) properties are empty. Any idea of what I'm doing wrong?

Regards,
Mario
Thu, Jun 23 2016 3:19 PMPermanent Link

Matthew Jones

Note the "published" not public of the properties.

--
Matthew Jones
Page 1 of 2Next Page »
Jump to Page:  1 2
Image