Как сделать «сериализовать» и «десериализовать» массив в Delphi? - PullRequest
0 голосов
/ 17 октября 2019

Мне нужно выполнить «сериализацию» и «десериализацию» на delphi, как на php, чтобы полученный массив затем сохранялся в базе данных сайта. Те. Мне нужно получить что-то вроде этого.

a: 10: {i: 0; a: 3: {i: 0; s: 4: "Obj1"; i: 1; i:383; я: 2; я: 339;} я: 1; а: 3: {я: 0; s: 4: "obj2"; я: 1; I: 339; я: 2; я: 18;} я: 2; а: 3: {я: 0; s: 4: "Obj3"; я: 1; I: 386; я: 2; я: 98;} я: 3; а: 3: {я: 0;s: 4: "Obj4"; я: 1; I: 428; я: 2; я: 286;} я: 4; а: 3: {я: 0; s: 4: "Obj5"; я: 1;я: 54; я: 2; я: 47;} я: 5; а: 3: {я: 0; s: 4: "Obj6"; я: 1; I: 328; я: 2; я: 27;} я: 6; а: 3: {я: 0; s: 4: "Obj7"; я: 1; I: 332; я: 2; я: 371;} я: 7; а: 3: {я:0; s: 4: "OBJ8"; я: 1; я: 42; я: 2; я: 187;} я: 8; а: 3: {я: 0; s: 4: "Obj9"; я:1; I: 314; я: 2; я: 68;} я: 9; а: 3: {я: 0; s: 5: "Obj10"; я: 1; I: 124; я: 2; я:120;}}

Вот мой код добавления объекта в delphi:


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Contnrs, StdCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 TRect = class 
  private
   p:TPoint;
   width, height: Integer;
   color : TColor;
   str:String;
  public
    constructor Create(APt:TPoint;w1, h1: Integer; _Color:TColor;str1:String);
    Procedure Draw(Cs:TCanvas;x:Integer;y:Integer); 
  end;
   TRectList=class(TObjectList)
  private
    function GetItems(index:integer):TRect;
    procedure SetItems(index:integer; Value:TRect);
  public
    property Items[index:integer]: TRect read GetItems write SetItems; default;
  end;

var
  Form1: TForm1;
  ImPic : TBitmap;
  RectList: TRectList;
  rect : TRect;
  start : TPoint;
  ObjId:Integer;


implementation
{$R *.dfm}
constructor TRect.Create(APt:TPoint; w1, h1: Integer; _Color: TColor;str1:String);
begin
  p:=APt;
  width := w1;
  height := h1;
  color := _Color;
  str:=str1;

end;

procedure TRect.Draw(Cs:TCanvas; x, y: Integer);
begin
  Cs.Brush.Color:=color;
  Cs.Rectangle(p.X,p.Y,p.X+width,p.Y+height);
end;


procedure TForm1.Button1Click(Sender: TObject);
var i,x,y:Integer;
begin
x := Random(500);
y := Random(400);
ObjId := ObjId+1;

 start := Point(x, y);
 rect := TRect.Create(start,20,20,clBlue,'Obj'+IntToStr(ObjId));
 RectList.Add(rect);
 Memo1.Lines.Clear;
 Image1.Canvas.Brush.Color:=clWhite;
 Image1.Canvas.FillRect(Image1.ClientRect);
 Memo1.Lines.Add('[');
 for i := 0 to RectList.Count-1 do
 begin
   RectList[i].Draw(Image1.Canvas,RectList[i].p.X,RectList[i].p.Y);
   Memo1.Lines.Add('["'+RectList[i].str+'",'+IntToStr(RectList[i].p.X)+','+IntToStr(RectList[i].p.Y)+'],');
 end;
  Memo1.Lines.Add(']');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
RectList := TRectList.Create(true);
randomize();
with Image1.Canvas do
begin
   brush.Color:=clSilver;
   Rectangle(0,0,width,height);
end;
end;
function TRectList.GetItems(index: integer):TRect;
begin
   Result := inherited Items[index] as TRect;
end;

procedure TRectList.SetItems(index: integer; Value: TRect);
begin
   inherited Items[index] := Value;
end;

end.

Массив создается в «memo». Пример на картинке: img Затем вы можете добавить этот массив в файл php, применить сериализацию, и вы получите массив, который показан вверху поста. Php код:

<code>$array = [
["Obj1",383,339],
["Obj2",339,18],
["Obj3",386,98],
["Obj4",428,286],
["Obj5",54,47],
["Obj6",328,27],
["Obj7",332,371],
["Obj8",42,187],
["Obj9",314,68],
["Obj10",124,120],
]
;
$string = serialize( $array );
echo $string."<br>";
$array = unserialize($string);
echo "<pre>";
var_dump($array);
echo "
";?>

Как сделать то же самое с delphi« сериализация »и« десериализация »для последующего хранения в базе данных mysql на сайте?

...