onryldz / x-superobject

Delphi Cross Platform Rapid JSON
316 stars 118 forks source link

Fake TArray<TMyObject> instead of TObjectList<TMyObject> #83

Closed vkrapotkin closed 8 years ago

vkrapotkin commented 8 years ago

To save a TObjectList property w/o the "garbage" as Count, Capacity, etc... I tryed a trick - to substitute List property by fake _List array

TMyObject=class
  S:string;
  B:boolean;
  N:single;
end;
TMyList=class(TObjectList<TMyObject>)
end;
TMyArray=TArray<TMyObject>;

TModel=class
public
  [DISABLE]
  List:TMyList;
  [ALIAS('List')]
  property _List:TMyArray read Get_List write Set_List;
end;

function TModel.Get_List:TMyArray;
begin
  setLength(Result, List.Count);
  for i:=0 to List.Count-1 do
    result[i]:=List[i];
end;

procedure TModel.Set_list(const Value:TMyArray);
begin
  List.Clear;
  for i:=0 to length(Value) do
    list.Add(Value[i]);
end;

As a result. I successfully saved my Model object in a file. But reverse operation failed.

in Set_List proc, value of Value is NIL therefore my trick fails (((

can I do something to avoid this failure ??

vkrapotkin commented 8 years ago

p.s. Delphi Seattle Upd1

vkrapotkin commented 8 years ago

Unfortunately, I still have Value=NIL in Set_List(Value) Seems nothing has been changed

onryldz commented 8 years ago

Hi, I can't see any issue. A sample here

vkrapotkin commented 8 years ago

Hi. Thanks a lot for your great work and for the answer.

I found a source of a problem If I create new constructor for TMyObject like this constructor Create(st:string; bl:Boolean; nm:single); reintroduce; system breaks :( BUT! I found a workaround too!

TMyObject=class
  ...
    constructor Create; reintroduce; overload;
    constructor Create(st:string; bl:Boolean; nm:single);  overload;
end;

constructor TMyObject.Create;
begin
  inherited;
end;

constructor TMyObject.Create(st: string; bl: Boolean; nm: single);
begin
  inherited Create;
  s:=st;
  b:=bl;
  n:=nm;
end;

I've attached the modified sample. But I have three another issues (feature requests)

  1. Unescaped international strings
  2. constructor CreateFromFile and procedure SaveToFile
  3. LoadFromJson into existing object All issues I solved in the additionally attached file. Maybe better to add this functionality in your helper class?

https://yadi.sk/d/bY5L72yUo2XTr