grijjy / GrijjyFoundation

Foundation classes used by other Grijjy repositories
Other
244 stars 86 forks source link

How to serialize TObjectList using TgoBsonSerializer? #9

Closed sallypmt closed 4 years ago

sallypmt commented 5 years ago

q. How to serialize TObjectList using TgoBsonSerializer?

landrix commented 5 years ago

Push

erikvanbilsen commented 4 years ago

The library (currently) does not support serialization of (generic) collections. It does support serialization of dynamic arrays though.

So you could add a serializable property of type TArray. In the getter, it could return TObjectList.ToArray. In the setter, it could add the array to the list.

PolywickStudio commented 4 years ago

It's fine. I understand. It's okay. Your code works great.

ricardodarocha commented 4 years ago

How to serialize TObjectList using TgoBsonSerializer? I got it in my way

Type TVehicle = class;
MyVehicles: TObjectList<TVehicle>;

procedure SerializeVehicles(out aResult: String);
var 
V: TVehicle;
CurrentItem: String; 

const MACRO = '$NEXTITEM' 

begin
aResult := '{"Result": [' + MACRO;

try
  if MyVehicles.Count = 0 then
     aResult := '{"Result": []}';
  for V in MyVehicles do
  begin
     TGoBsonSerializer.Serialize<TRetorno>(V, CurrentItem);
     aResult := StringReplace(aResult, MACRO, CurrentItem + ',' + MACRO, []);
  end;
finally
   aResult := StringReplace(aResult, ',' + MACRO, ']}');
end;
end;