Icon View Thread

The following is the text of the current message along with any replies.
Messages 11 to 15 of 15 total
Thread Help with TServerRequests and JSON...
Thu, Jun 23 2016 4:25 PMPermanent Link

Mario Enríquez

Open Consult

Yep, didn't notice that.... Thank you.

Regards,
Mario
Sun, Jul 17 2016 3:55 PMPermanent Link

thomh

>>Tim Young [Elevate Software] wrote:
>>
>>If you need to save to JSON, then that's a bit of an issue still because, when working up this example, I noticed that >>the TCollection and TStrings classes don't have the "Save" portion of persistence implemented.  I've added this for >>2.05, so it should be available soon.

Hi Tim,

Could you expand on your example to Mario and show how you would code the "Save" portion in 2.05?

// Thom
Mon, Jul 18 2016 1:48 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Thom,

<< Could you expand on your example to Mario and show how you would code the "Save" portion in 2.05? >>

This is what the TStrings persistence overrides look like in 2.05:

interface

  TStrings = class(TAbstractList)
....
     protected
        procedure BeforeLoad; override;
        function LoadProperty(AReader: TReader): Boolean; override;
        function LoadArrayElement(AReader: TReader): Boolean; override;
        procedure Save(AWriter: TWriter); override;

..............

implementation

{ TStrings }

procedure TStrings.BeforeLoad;
begin
  inherited BeforeLoad;
  Clear;
end;

function TStrings.LoadProperty(AReader: TReader): Boolean;
var
  TempPropertyName: String;
begin
  Result:=False;
  TempPropertyName:=AReader.GetPropertyName;
  if (TempPropertyName <> '') then
     begin
     if SameText(TempPropertyName,PERSISTENT_LOAD_STRINGS) then
        begin
        Result:=True;
        AReader.SkipPropertyName;
        AReader.SkipPropertySeparator;
        LoadArray(AReader);
        end
     else
        Result:=inherited LoadProperty(AReader);
     end;
end;

function TStrings.LoadArrayElement(AReader: TReader): Boolean;
begin
  Add(AReader.ReadString);
  Result:=True;
end;

procedure TStrings.Save(AWriter: TWriter);
var
  TempCount: Integer;
  I: Integer;
begin
  BeforeSave;
  with AWriter do
     begin
     BeginObject;
     TempCount:=Count;
     BeginNewLine;
     IntegerProperty(PERSISTENT_LOAD_COUNT,TempCount);
     BeginNewLine;
     PropertyName(PERSISTENT_LOAD_STRINGS);
     BeginArray(TempCount > 0);
     IncIndent;
     for I:=0 to TempCount-1 do
        begin
        if (I > 0) then
           Separator;
        NewLine;
        StringValue(GetString(I));
        end;
     EndArray(TempCount > 0);
     DecIndent;
     SaveProperties(AWriter);
     EndObject;
     end;
  AfterSave;
end;

........

And here's the TCollection:

interface

  TCollection = class(TPersistent)
....
     protected
....
        procedure BeforeLoad; override;
        function LoadProperty(AReader: TReader): Boolean; override;
        function LoadArrayElement(AReader: TReader): Boolean; override;
        procedure Save(AWriter: TWriter); override;
....

implementation

procedure TCollection.BeforeLoad;
begin
  inherited BeforeLoad;
  Clear;
end;

function TCollection.LoadProperty(AReader: TReader): Boolean;
var
  TempPropertyName: String;
begin
  Result:=False;
  TempPropertyName:=AReader.GetPropertyName;
  if (TempPropertyName <> '') then
     begin
     if SameText(TempPropertyName,PERSISTENT_LOAD_ITEMS) then
        begin
        Result:=True;
        AReader.SkipPropertyName;
        AReader.SkipPropertySeparator;
        LoadArray(AReader);
        end
     else
        Result:=inherited LoadProperty(AReader);
     end;
end;

function TCollection.LoadArrayElement(AReader: TReader): Boolean;
begin
  Result:=Add.LoadArrayElement(AReader);
end;

procedure TCollection.Save(AWriter: TWriter);
var
  TempCount: Integer;
  I: Integer;
begin
  BeforeSave;
  with AWriter do
     begin
     BeginObject;
     TempCount:=FItems.Count;
     BeginNewLine;
     IntegerProperty(PERSISTENT_LOAD_COUNT,TempCount);
     BeginNewLine;
     PropertyName(PERSISTENT_LOAD_ITEMS);
     BeginArray(TempCount > 0);
     IncIndent;
     for I:=0 to TempCount-1 do
        begin
        if (I > 0) then
           Separator;
        NewLine;
        GetItem(I).Save(AWriter);
        end;
     EndArray(TempCount > 0);
     DecIndent;
     SaveProperties(AWriter);
     EndObject;
     end;
  AfterSave;
end;

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Jul 18 2016 2:24 PMPermanent Link

thomh

Thanks, Tim.

Since I do not have 2.05 yet, can I use this code 2.04b1 ?

// Thom
Mon, Jul 18 2016 3:34 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Thom,

<< Since I do not have 2.05 yet, can I use this code 2.04b1 ? >>

Yes, absolutely.

Tim Young
Elevate Software
www.elevatesoft.com
« Previous PagePage 2 of 2
Jump to Page:  1 2
Image