You are not logged in.
Pages: 1
Hello!
What is the reason for the JSON result as an object with the array "result" {result:[...]}?
Why can't be returned something like {obj1} for one object and [{obj1},{obj2}] for a list of objects? Is there an important reason for that?
TFullname = record
Firstname: String;
Lastname: String;
end;
TFullnames = array of TFullname;
IFullnameService = interface(IInvokable)
function GetFullnameArray: TFullnames;
end;
// result JSON
{
result:[
[
{
Firstname:"My",
Lastname:"SQL"
},
{
Firstname:"Maria",
Lastname:"DB"
}
]
]
}
// wish
[
{
Firstname:"My",
Lastname:"SQL"
},
{
Firstname:"Maria",
Lastname:"DB"
}
]
Thank you for your answer!
Offline
The reason to nest the returned values in a "result" array by default, is that it allows to return the corresponding session ID, in sicClientDriven mode.
Even if you are not in sicClientDriven mode, it shares the same layout.
BTW, this is close to the JSON-RPC format.
You can return plain JSON, using the TServiceFactoryServer.ResultAsJSONObject or TServiceFactoryServer.ResultAsJSONObjectWithoutResult properties.
See http://synopse.info/files/html/Synopse% … #TITLE_423
Offline
:-) Ah ok, thank you very much for the explanation!
Offline
Hi ab
I used the TFullname records posted by cypriotcalm and added them to the calculator in the sample project 27 (Project14ServerHttpWrapper), since this declaration is already done in there to test the result:
aServer.ServiceDefine(TServiceCalculator,[ICalculator],sicShared).ResultAsJSONObjectWithoutResult := true;
I added a test function like this:
function TServiceCalculator.GetSingleFullname: TFullname;
begin
Result.Firstname := 'First Name';
Result.Lastname := 'Last Name';
end;
and expected the result to not contain "Result":
However, it looks like it always encapsulates it in a Result ie
{"Result":{"Firstname":"First Name","Lastname":"Last Name"}}
Is that the expected behaviour of this property, or did I misunderstood the reason for it? Is the idea behind this property not to send the data without result, ie:
{"Firstname":"First Name","Lastname":"Last Name"}
?
Is it possible to send a record like that without Result being added? It seems that using RawJson also always include Result?
Offline
Offline
Thanks ab
It can get very complicated to add/update out parameters on many functions when a standard long/nested record result is to be returned, especially if the expected output is prepopulated as a record by a universal function.
Is there maybe an event on TSQLRestServerFullMemory that can be overridden before the data is sent to manually remove the leading {"Result": and trailing " from the outgoing json string, or another property that can be set to not add it at all?
Offline
Offline
Thanks Vitaly that looks like an option, but also looks scary, since I'm not too sure what impact that will have.
I suppose that would entail overriding both TSQLRestServerURIContext.ServiceResultStart and TSQLRestServerURIContext.ServiceResultEnd and replacing them with versions that look for a new boolean property which causes them to not make any changes to wr at all?
Last edited by squirrel (2021-01-15 10:12:38)
Offline
Sorry, I'm not sure about ServiceResultStart/End since I haven't used them. I took a quick look at these methods, I am afraid it might be not enough. Hope, Arnaud or other guys will advise you something better.
We're using custom ServicesRouting and TServiceCustomAnswer in services for a heavy project (FHIR-specified), where we had to override almost everything to make it automatic and compatible with third-party software: fhir+json/fhir+xml output/input data formats, different http-methods, custom meaningful headers, and much other stuff...
Last edited by Vitaly (2021-01-15 11:03:57)
Offline
Pages: 1