Rohit_Nz
(Gupta Rohit)
March 14, 2025, 2:34pm
1
I am getting this in swagger
{
"session": "",
"ref": 1,
"status": "A",
"irst_Name": "aa",
"last_Name": "bb",
"known_As": "",
"id": "ff"
}
But the class is
TLogin_Data = class
Session : string;
Ref : int64;
Status : char;
First_Name: string;
Last_Name : string;
Known_As : string;
Id : string;
end;
And the code in the endpoint to convert to json is
Result := TJson.ObjectToJsonObject (Login_Data);
The end point definition is
[HttpGet] function Login_Token (const User, Pwd : string) : TJSOnObject;
I was not expecting the first character to be lower case. Can I rely on this happening?
First_Name is missing the "f ". This makes it unreliable.
wlandgraf
(Wagner Landgraf)
March 14, 2025, 6:45pm
2
It's the above line that is causing this, and that is Delphi, not XData. That's the behavior of the function you are using.
You can try simply returning TLogin_Data
instead of TJSONObject
and, if needed, fine tune the output using our JSON attributes .
Rohit_Nz
(Gupta Rohit)
March 15, 2025, 1:59am
3
Hi Wagner,
Strangely enough, if I return the class itself, without converting it. I get the exact same result except that id is converted to $id (which I understand). But the First_Name is still missing the leading F
This is the response I get in Swagger
{
"$id": 1,
"Session": "",
"Ref": 1,
"Status": "A",
"irst_Name": "aa",
"Last_Name": "bb",
"Known_As": "",
"Id": "ff"
}
The class (with no embellishments (for testing).
TLogin_Data = class
Session : string;
Ref : int64;
Status : char;
First_Name: string;
Last_Name : string;
Known_As : string;
Id : string;
end;
The endpoint
[HttpGet] function Login_Token2 (const User, Pwd : string) : TLogin_Data;
I id the following
Changed the Id to Staff_Id (which fixed the $ issues).
Changed char to string (no changes)
Changed FirstName to fFirst_Name (I get fFirst_Name).
So both Delphi and TM functions are removing the F in the fieldname for some reason, in some cases.
wlandgraf
(Wagner Landgraf)
March 15, 2025, 2:53pm
4
That is expected, your class has fields, not properties, so for fields the leading "F" is removed. You can change it this way:
TLogin_Data = class
Session : string;
Ref : int64;
Status : char;
[JsonProperty('First_Name')]
First_Name: string;
Last_Name : string;
Known_As : string;
Id : string;
end;
wlandgraf
(Wagner Landgraf)
March 15, 2025, 2:57pm
5
Also, no need to rename the ID. XData adds the $id
property which is a different one. You can remove its output by setting InstanceLoopHandling
property to TInstanceLoopHandling.Error
in TXDataServer (or TXDataServerModule):
XDataServer1.InstanceLoopHandling := TInstanceLoopHandling.Error;
system
(system)
Closed
March 17, 2025, 7:26am
8
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.