You are not logged in.
Pages: 1
Personally, I would like to create a textual JSON representation of the Set with enumerations,
using mORMot functions: GetEnumname and GetCaptionFromEnum, and store it in database as a text field.
Something like in PHP, whereas you just create a PHP array and call json_encode on it.
Example:
$a = array(
array('foo' => 'bar'),
array('foo' => 'baz'));
$json = json_encode($a);
Gives you: [{"foo":"bar"},{"foo":"baz"}]
______
function json_encodeSet(DSet: Ds): String;
const
Delims: array[Boolean] of string = ('', ',');
var
SetItem: DD;
Begin
Result := '';
for SetItem := Low(SetItem) to High(SetItem) do
if SetItem in DSet then
Result := Result + Delims[Length(Result) > 0] + '"'+ (GetEnumname( TypeInfo(DD), Ord(SetItem)) )+'"';
Result := '['+(Result)+']';
end;
procedure TForm1.Button9Click(Sender: TObject);
var
porra : Ds;
begin
porra := [d2,d3];
ShowMessage(json_encodeSet(porra)); // D7 gives you ["d2","d3"]
end;
In Delphi XE2, shows an error Incompatible types: string and PShortString. D7 works fine.
Any idea to serialize Set of enumerations to return: [{"id":"2", "value":"d2"},{"id":"3", "value":"d3"}]
Last edited by warleyalex (2013-07-21 00:16:52)
Offline
Thanks.
Now I'd like to pass parameters.
___________________________
procedure TMyService.SetJ(var Ctxt: TSQLRestServerCallBackParams);
var value : RawUTF8;
var value1 : TWeeki;
begin
UrlDecodeValue(Ctxt.Parameters,'A=',value,@Ctxt.Parameters);
Ctxt.Returns('[{'+Set2JSON([d1,d7])+'}]');
End;
When I type into the address bar: http://localhost:8080/service/SetJ/?a=
I'll see this json containing an array of objects:
[{"id":1,"value":"Dom"},{"id":7,"value":"Sab"}]
I would like to pass parameters like this:
http://localhost:8080/service/SetJ/?a=[d1,d7]
Any idea?
Last edited by warleyalex (2013-07-21 00:13:39)
Offline
凸(^_^)凸
Last edited by warleyalex (2013-07-21 00:14:26)
Offline
Pages: 1