Link to home
Start Free TrialLog in
Avatar of Andrea Matthiae
Andrea Matthiae

asked on

Delphi DataSnap REST problem with TJSONObject

Hi Experts!

I have been using DataSnap REST for some weeks but I have problems understanding some concepts related to TJSONObject and how these data are sent.
I have three related questions.
I consider a simple class:


TMyclass = class
  private
    [JSONName('name')] // invio
    Fname: string;
    [JSONName('surname')] // invio
    Fsurname: string;
    [JSONName('mytext')] // invio
    Fmytext: string;

    procedure Setname(const Value: string);
    procedure Setsurname(const Value: string);
    procedure Setmytext(const Value: string);
  public
    property name: string read Fname write Setname;
    property surname: string read Fsurname write Setsurname;
    property mytext: string read Fmytext write Setmytext;
  end;
  
.....

procedure TMyclass.Setname(const Value: string);
begin
  Fname := Value;
end;

procedure TMyclass.Setsurname(const Value: string);
begin
  Fsurname := Value;
end;

procedure TMyclass.Setmytext(const Value: string);
begin
  Fmytext := Value;
end;  

Open in new window


.....

In my code I have something similar to:

Procedure TMain.Send_JSON;
var
   VJSONObject: TJSONObject;
   VMyclass:TMyclass;
begin
   VMyclass:=TMyclass.Create;

   VMyclass.name:='John';
   VMyclass.surname:='Ace';
   VMyclass.mytext:='';

   VJSONObject:=TJson.ObjectToJsonObject(VMyclass);
   //...
   
   RESTClient1.ResetToDefaults;
   RESTRequest1.ResetToDefaults;
   RESTResponse1.ResetToDefaults;
   RESTClient1.BaseURL:='http://111.222.333.444:8080/datasnap/rest/mypath';
   RESTRequest1.Timeout:=RESTRequest_Timeout;
   
   RESTRequest1.Resource:='/MyParam'; 
   RESTRequest1.Method:=TRESTRequestMethod.rmPOST; 
   RESTRequest1.AddBody(VJSONObject.ToJSON, ContentTypeFromString('application/json'));
   //...
   RESTRequest1.Execute;
   //...
end;

Open in new window



[First question]
When I send this JSON:
         {"name":"John","surname":"Ace","mytext":""}
i don't want to have:
"mytext":""
in the JSON.

[Second question]
I don't know a way to insert a JSON array like this:
{"name":"John","file": [{"name_file":"myfile1.txt","dir_file":"C:\mydir"},{"name_file":"myfile2.txt","dir_file":"C:\mydir2"},...]}

[Third question]
I am looking for a method to manipulate the:

VJSONObject.ToString

before insert in the:

RESTRequest1.AddBody(VJSONObject.ToJSON,...

but I couldn't find it. I search something like:

  VJSONObject := Json.StringToJsonObject(VJSONObject.ToString);

I have a lot of problems because there is no documentation on this part.
Does anyone have a solution?


Working with RAD Studio Tokyo 10.2.3
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

I'm not really a Delphi guy, but just try to provide some inputs.

[First question]
When I send this JSON:
         {"name":"John","surname":"Ace","mytext":""}
i don't want to have:
"mytext":""
in the JSON.

you can try remove:
[JSONName('mytext')] // invio
    Fmytext: string;

Open in new window


[Second question]
I don't know a way to insert a JSON array like this:
{"name":"John","file": [{"name_file":"myfile1.txt","dir_file":"C:\mydir"},{"name_file":"myfile2.txt","dir_file":"C:\mydir2"},...]}
think you need to create an array for the files and you need to include it in your class definition. So, when you serialize your class to JSON, you will get the file names included.

[Third question]
I am looking for a method to manipulate the:

VJSONObject.ToString

before insert in the:

RESTRequest1.AddBody(VJSONObject.ToJSON,...

but I couldn't find it. I search something like:

  VJSONObject := Json.StringToJsonObject(VJSONObject.ToString);
Not too sure what exactly what you're asking, but VJSONObject.ToString may not be needed or valid. In your IDE, are you able to debug with break points? or have a Watch to view the objects in run time?
Avatar of Andrea Matthiae
Andrea Matthiae

ASKER

Hi Ryan,
thanks for the response.

[First question]
I can't remove the declaration at runtime in the delphi code.
I rewrite the question:
if I have
  {"name":"John","surname":"Ace","mytext":"abcde"}
or  
  {"name":"John","surname":"Ace","mytext":"zxcvdfeye"}
 
and in a particular case I have:  

  {"name":"John","surname":"Ace","mytext":""}

with
"mytext":""
how can I remove "mytext":"" to have
{"name":"John","surname":"Ace"}   ?

 
[Second question]
"... you need to create an array for the files and you need to include it in your class definition".
How can I do it? This is exactly my question.


[Third question]
When I stay in debug I see VJSONObject.ToString. I save also this string in my DB before performing the execute
[First question]
I can't remove the declaration at runtime in the delphi code.

with
"mytext":""
how can I remove "mytext":"" to have
{"name":"John","surname":"Ace"}   ?
In this case, you probably can save the value of VJSONObject.ToJSON into a variable, and then do the text comparing and replace it with empty string.

[Second question]
"... you need to create an array for the files and you need to include it in your class definition".
How can I do it? This is exactly my question.
sorry that can't really answer you this in Delphi, I'm more like a .NET guy these days.

[Third question]
When I stay in debug I see VJSONObject.ToString. I save also this string in my DB before performing the execute
Ok... so you can save the object as text in DB before performing the execution. So, your question is... ?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.