Recent

Author Topic: Lamw using self other object  (Read 727 times)

juank1971

  • Full Member
  • ***
  • Posts: 112
Lamw using self other object
« on: October 02, 2020, 03:17:16 pm »
I have this unit  worked fine in windows is for zerialized json
Bat lamw have error becouse
In this line
 json_str.JSONToObject(json_dat, self); of the

procedure TJsonComponent.LoadFromStream(aStream: TStream);

Self refered to TJsonComponent class bat self the compiller try
To lamw module self

I need real  object for this self  not lamw self

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.    typinfo,  jsonscanner, fpjson, fpjsonrtti;
  10.  
  11. type
  12.  
  13.   TJsonComponent = class(TComponent)
  14. protected
  15.   procedure propertyError(Sender : TObject; AObject : TObject; Info : PPropInfo;
  16.     AValue : TJSONData; Error : Exception; Var doContinue : Boolean); virtual;
  17.   procedure restoreProperty(Sender : TObject; AObject : TObject; Info : PPropInfo;
  18.     AValue : TJSONData;Var Handled : Boolean);
  19. public
  20.   procedure LoadFromFile(const aFilename: string); virtual;
  21.   procedure SaveToFile(const aFilename: string); virtual;
  22.   procedure LoadFromStream(aStream: TStream);
  23.   procedure SaveToStream(aStream: TStream);
  24. end;
  25.  
  26. TJSonBindDemo = class(TJsonComponent)
  27. private
  28.   fFirstName: string;
  29.   fLastName: string;
  30.   fAge: integer;
  31. published
  32.   property FirstName: string read fFirstName write fFirstName;
  33.   property LastName: string read fLastName write fLastName;
  34.   property Age: integer read fAge write fAge;
  35. end;
  36.  
  37.  
  38.  
  39.   { TForm1 }
  40.  
  41.   TForm1 = class(TForm)
  42.     Button1: TButton;
  43.     procedure Button1Click(Sender: TObject);
  44.   private
  45.  
  46.   public
  47.  
  48.   end;
  49.  
  50. const
  51.   jsondata: string = '{"FirstName": "Sophia",'+
  52.                        '"LastName": "X",'+
  53.                       ' "Age": "25"}';
  54.  
  55. var
  56.   Form1: TForm1;
  57.  
  58.   obj: TJSonBindDemo;
  59.   str: TStream;
  60.  
  61. implementation
  62.  
  63. {$R *.lfm}
  64.  
  65. { TForm1 }
  66.  
  67. procedure TForm1.Button1Click(Sender: TObject);
  68. begin
  69.    obj := TJSonBindDemo.Create(nil);
  70.   str := TMemoryStream.Create;
  71.   str.Write(jsondata[1], length(jsondata));
  72.   obj.LoadFromStream(str);
  73.  
  74.   ShowMessage(obj.FirstName);
  75.   ShowMessage(obj.LastName);
  76.   ShowMessage(obj.Age.ToString);
  77.  
  78.   str.Free;
  79.   obj.Free;
  80. end;
  81.  
  82.  
  83. procedure TJsonComponent.restoreProperty(Sender : TObject; AObject : TObject; Info : PPropInfo;
  84.   AValue : TJSONData; Var Handled : Boolean);
  85. begin
  86. end;
  87.  
  88. procedure TJsonComponent.propertyError(Sender : TObject; AObject : TObject; Info : PPropInfo;
  89.   AValue : TJSONData; Error : Exception; Var doContinue : Boolean);
  90. begin
  91. end;
  92.  
  93. procedure TJsonComponent.LoadFromStream(aStream: TStream);
  94. var
  95.   json_str: TJSONDeStreamer;
  96.   json_dat: TJSONStringType;
  97.   t:TJSONObject;
  98. begin
  99.   json_str := TJSONDeStreamer.Create(nil);
  100.   try
  101.     aStream.Position:=0;
  102.     json_str.OnPropertyError:= @propertyError;
  103.     json_str.OnRestoreProperty := @restoreProperty;
  104.     setLength(json_dat, aStream.Size);
  105.     aStream.Read(json_dat[1], length(json_dat));
  106.     json_str.JSONToObject(json_dat, self);
  107.  
  108.   finally
  109.     json_str.Free;
  110.   end;
  111. end;
  112.  
  113. procedure TJsonComponent.SaveToStream(aStream: TStream);
  114. var
  115.   json_str: TJSONStreamer;
  116.   json_dat: TJSONStringType;
  117. begin
  118.   json_str := TJSONStreamer.Create(nil);
  119.   try
  120.     json_dat := json_str.ObjectToJSONString(self);
  121.     aStream.Write(json_dat[1], length(json_dat));
  122.   finally
  123.     json_str.Free;
  124.   end;
  125. end;
  126.  
  127. procedure TJsonComponent.LoadFromFile(const aFilename: string);
  128. var
  129.   str: TMemoryStream;
  130. begin
  131.   str:= TMemoryStream.Create;
  132.   try
  133.     str.LoadFromFile(aFilename);
  134.     LoadFromStream(str);
  135.   finally
  136.     str.Free;
  137.   end;
  138. end;
  139.  
  140. procedure TJsonComponent.SaveToFile(const aFilename: string);
  141. var
  142.   str: TMemoryStream;
  143. begin
  144.   str:= TMemoryStream.Create;
  145.   try
  146.     SaveToStream(str);
  147.     str.SaveToFile(aFilename);
  148.   finally
  149.     str.Free;
  150.   end;
  151. end;
  152.  
  153.  
  154.  
  155.  
  156.  
  157. end.
  158.  
  159.  
  160.  
  161.  

jmpessoa

  • Hero Member
  • *****
  • Posts: 2302
Re: Lamw using self other object
« Reply #1 on: October 02, 2020, 07:36:12 pm »

You can try use the  "json_str" reference  not "self".....
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018