#1 2013-07-15 20:14:03

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Set with Enumerations

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

#2 2013-07-16 06:31:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Set with Enumerations

Uses GetEnumName()^ instead of GetEnumName().

Online

#3 2013-07-18 19:49:50

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: Set with Enumerations

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

#4 2013-07-18 20:38:09

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Set with Enumerations

Afaik url has to be encoded.

Online

#5 2013-07-18 21:11:52

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: Set with Enumerations

凸(^_^)凸

Last edited by warleyalex (2013-07-21 00:14:26)

Offline

Board footer

Powered by FluxBB