Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread TObjectList.Save
Tue, Apr 5 2016 5:40 AMPermanent Link

Matthew Jones

It would be nice if TObjectList had a Save to TWriter...

--

Matthew Jones
Tue, Apr 5 2016 6:53 AMPermanent Link

Matthew Jones

Matthew Jones wrote:

> It would be nice if TObjectList had a Save to TWriter...

My generic version until it does...

procedure ObjectListToJSON(xObjectList : TObjectList; xWriter :
TWriter; szName : String);
var               
   nWriteLoop : Integer;
   xItem : TPersistent;
begin      
   xWriter.BeginObject;
   xWriter.PropertyName(szName);
   xWriter.BeginArray(xObjectList.Count > 0); // hasElements param
controls the output of spaces

   for nWriteLoop := 0 to xObjectList.Count - 1 do
   begin
       if not xObjectList.Objects[nWriteLoop] is TPersistent then
           raise Exception.Create('Object in list is not persistent');
       xItem := TPersistent(xObjectList.Objects[nWriteLoop]);
       xItem.Save(xWriter);
       if nWriteLoop < xObjectList.Count - 1 then
           xWriter.Separator;
   end;
   xWriter.EndArray(xObjectList.Count > 0); // hasElements param
controls the output of spaces
   xWriter.EndObject;
end;

--

Matthew Jones
Tue, Apr 5 2016 7:28 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< It would be nice if TObjectList had a Save to TWriter... >>

What you want is the TCollection class - you should use it when you want persistence of lists of objects.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Apr 5 2016 8:02 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> What you want is the TCollection class - you should use it when you
> want persistence of lists of objects

I have had a good ponder at that, but I don't think I can use it as I
have to be "generic" to fit in with the back-end server. The use of ID
may not work for me.

--

Matthew Jones
Tue, Apr 5 2016 6:48 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< I have had a good ponder at that, but I don't think I can use it as I have to be "generic" to fit in with the back-end server. The use of ID may not work for me. >>

And how exactly does the ID property conflict with your goals ?  It's public, not published, so it isn't persisted, by default, and it is automatically assigned by the TCollection class.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Apr 6 2016 3:38 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> And how exactly does the ID property conflict with your goals ?  It's
> public, not published, so it isn't persisted, by default, and it is
> automatically assigned by the TCollection class.

I shall have another look - I missed that aspect. It did sound ideal,
except, so if I was wrong in the exceptions, so much the better.

--

Matthew Jones
Wed, Apr 6 2016 5:17 AMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> What you want is the TCollection class - you should use it when you
> want persistence of lists of objects.

Okay, I tried to switch to it, but I cannot add an arbitrary object to
it. I will be doing various manipulations of the objects, and perhaps
copying from one collection to another. The TCollection.Add and .Insert
are the only ways to add anything, and they create a new one. I may
also need to add different types, so I suspect that this is not going
to work for me. However, the list I posted works fine, so I will go
with that for now.

--

Matthew Jones
Image