- This topic has 2 replies, 2 voices, and was last updated 4 years, 10 months ago by .
Viewing 2 reply threads
Viewing 2 reply threads
- You must be logged in to reply to this topic.
Tagged: smartservices JSON serialize
Hi,
Let’s say I have this classes :
TGeneralData = class
public
Name: string;
Address: string;
end;
THobby = class
public
Name: string;
constructor Create( hobbyName: string );
end;
TMyResult = class
public
GeneralData: TGeneralData;
Hobbies: TObjectList<THobby>;
constructor Create;
destructor destroy;override;
end;
initialization
kbmMWRegisterKnownClasses([THobby, TGeneralData, TMyResult]);
And a smartservice
function TkbmMWCustomSmartServiceDemo.Test:TMyResult;
begin
result := TMyResult.Create;
result.GeneralData.Name := 'Sundar Pichai';
result.GeneralData.Address := '600 Amphitheatre Parkway';
result.Hobbies.Add( THobby.Create('Surfing') );
result.Hobbies.Add( THobby.Create('Hiking') );
end;
Please forgive this made up example (the original code is more complex instead of public fields there are properties with getters and setters ), here when I have the public field hobbies ( TObjectList ) I get access violation when I do a GET request from the browser, if I remove this field, the browser gets the serialized data as JSON correctly
The original code doesn’t throw an error but nothing is returned to the browser when there are fields/properties which are of the type TObjectList
Perhaps I’m missing something needed for kbmMWRegisterKnownClasses.
What else should be done to let kbmMW serialize TObjectList correctly ?
Seem like declaring an alias class for hobbies solves the problem, something like:
THobbies = TObjectList<THobby>;
Then I can have in TMyResult :
Hobbies: THobbies;
Error gone :), I quickly applied the same strategy on the real code, but I’m getting this in the result when the objectlist is empty:
"Hobbies":[true,0,0],
and when I add data, seems to be duplicated, the element is part of the array but also appears as a nested array, so I’m changing all the TObjectlist fields to plain old arrays for now.
Thank you all.
Hi,
You also need to register list versions of your objects as known objects. Eg. register TObjectList<THobby>
Try to send me a simple sample, including the json that show the problem with the additional bogus hobbies element.