Recent

Author Topic: [SOLVED] Serialize/deserialize collections to/from streams  (Read 6740 times)

JD

  • Hero Member
  • *****
  • Posts: 1848
[SOLVED] Serialize/deserialize collections to/from streams
« on: August 04, 2015, 04:54:55 pm »
Hi there everyone,

Does anyone know of a better crossplatform method of serializing/deserializing a TCollection to/from streams. I am currently using the procedures found at http://users.atw.hu/delphicikk/listaz.php?id=620&oldal=36 but they sometimes fail giving me invalid values for property errors.

The collection I want to serialize is a simple one

Code: [Select]
  { TGeneric }
  TGeneric = class(TBaseItemClass)
  private
    { private declarations }
    FInteger: integer;
    FString: string;
    function GetPropertyCount: integer;
    function GetPropertyString(Aindex: integer): string;
  public
    { public declarations }
  published
    { published declarations }
    property IntValue: integer read FInteger write FInteger;
    property StrValue: {UTF8}string read FString write FString;
  end;

  { TGenerics }
  TGenerics = class(TCollection)
  private
    { private declarations }
    function GetItem(AIndex: integer): TGeneric;
    procedure SetItem(AIndex: integer; AValue: TGeneric);
  public
    { public declarations }
    constructor Create;
    function Add: TGeneric;
    property Item[AIndex: integer]: TGeneric read GetItem write SetItem; default;
  end;

Thanks a lot.

JD
« Last Edit: August 18, 2015, 10:07:55 am by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Serialize/deserialize collections to/from streams
« Reply #1 on: August 04, 2015, 10:10:14 pm »
Give unit fpjsonrtti a try. There is a wiki page to get you started.

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Serialize/deserialize collections to/from streams
« Reply #2 on: August 04, 2015, 10:49:58 pm »
Give unit fpjsonrtti a try. There is a wiki page to get you started.

Thanks for the link. I'll consider it on a future project. I'm not using JSON in this one. It is an old project & it would involve substantial rewriting of the code to move to JSON at this point.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Serialize/deserialize collections to/from streams
« Reply #3 on: August 04, 2015, 11:07:33 pm »
It may not be the serialization/deserialization after all.

After reading http://www.delphigroups.info/2/a1/512873.html, I'll move the private GetPropertyString & GetPropertyCount functions in TGeneric (a TCollectionItem) to public & see what happens.

Testing......

OR maybe even use a stringlist instead of a collection. A stringlist makes serialization/deserialization easier.
« Last Edit: August 04, 2015, 11:39:55 pm by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Basile B.

  • Guest
Re: Serialize/deserialize collections to/from streams
« Reply #4 on: August 05, 2015, 12:05:27 am »
You could encapsulate your TCollection in a TComponent and then serialize using TStream.WriteComponent().
The easy way...But i guess that you don't want to do that.

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Serialize/deserialize collections to/from streams
« Reply #5 on: August 18, 2015, 10:06:10 am »
Give unit fpjsonrtti a try. There is a wiki page to get you started.

Thanks engkin! I took a look at fpjsonrtti & I was pleasantly surprised. I've started to introduce it into the code. I now use TJSONStreamer & TJSONDeStreamer everywhere.

In the process, I also discovered fjsondataset and it has given me some interesting ideas. No documentation though but I guess it works like any other dataset. I use Zeos on the server side so I was thinking of copying data from a Zeos dataset to the fpjsondataset.

I wonder if anyone has an example of such a thing i.e filling fpjsondataset from another dataset. I guess I'll put it in another thread.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: [SOLVED] Serialize/deserialize collections to/from streams
« Reply #6 on: August 18, 2015, 04:54:24 pm »
I wonder if anyone has an example of such a thing i.e filling fpjsondataset from another dataset.

Since it is part of Free Pascal packages, there is at least a test file:
Code: [Select]
Var
  DS : TExtjsJSONObjectDataset;
  I,J : Integer;
  F : TFieldDef;

begin
  DS:=TExtjsJSONObjectDataset.Create(Nil);
  try
    With DS do
      begin
      FieldDefs.Add('ID',ftLargeint,0);
      FieldDefs.Add('Name',ftString,20);
      FieldDefs.Add('Email',ftString,30);
      Open;

      // Record 1
      Append;
      FieldByName('ID').AsInteger:=3;
      FieldByName('Name').AsString:='Michael';
      FieldByName('Email').AsString:='michael@freepascal.org';
      Post;
...

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: [SOLVED] Serialize/deserialize collections to/from streams
« Reply #7 on: August 20, 2015, 01:16:34 pm »
@engkin

Thanks a lot!
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

 

TinyPortal © 2005-2018