#1 2015-09-16 07:06:18

hnb
Member
Registered: 2015-06-15
Posts: 290

ObjectListToJson for "true JSON"

Can we add new util function in SynCommons for "true JSON" (used for AJAX request) for converting TObjectList?

It will be very useful. ObjectToJSON is not the option (it return Table name in result)

Patch ofc included:

function ObjectListToJson(Value: TObjectList; ListOptions: TTextWriterWriteObjectOptions=[woDontStoreDefault]): RawUTF8;
var
  i: integer;
begin
  if (Value=nil) or (Value.Count = 0) then
    result := '[]' else
  with DefaultTextWriterJSONClass.CreateOwnedStream do
  try
    Add('[');
    for i := 0 to Value.Count - 1 do
    begin
      WriteObject(Value[i], ListOptions);
      Add(',');
    end;
    CancelLastComma;
    Add(']');

    SetText(result);
  finally
    Free;
  end;
end;

best regards,
Maciej Izak

Last edited by hnb (2015-09-16 07:13:52)


best regards,
Maciej Izak

Offline

#2 2015-09-16 18:26:25

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

Re: ObjectListToJson for "true JSON"

AFAIR ObjectToJSON() does not write the classname if the corresponding options are defined.

Offline

#3 2015-09-16 21:33:31

hnb
Member
Registered: 2015-06-15
Posts: 290

Re: ObjectListToJson for "true JSON"

It is true for TObjectList but It doesn't work for TObjectList items.


best regards,
Maciej Izak

Offline

#4 2015-09-17 06:12:45

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

Re: ObjectListToJson for "true JSON"

By default, TObjectList will set the woStoreClassName for its nested objects, unless the new woObjectListWontStoreClassName option is defined.

See http://synopse.info/fossil/info/fa71ce10dc

Offline

#5 2015-09-17 06:45:31

hnb
Member
Registered: 2015-06-15
Posts: 290

Re: ObjectListToJson for "true JSON"

Thanks! Your patch is much better smile


best regards,
Maciej Izak

Offline

Board footer

Powered by FluxBB