Jump to content
Sign in to follow this  
Stéphane Wierzbicki

JSON - Why self ins't updated with new values ?

Recommended Posts

 

Hi,

 

I cannot get this procedure TPerson.LoadSettings(const aJsonString: String). Is there any reason why self instance is not updated with new value ?

 

Is their any workaround or should I create a LoadSettings method outside TPerson ?

 

program Project1;
{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  REST.JSON,
  Unit1 in 'Unit1.pas';

var Person:TPerson;
    jsonstr:string;
begin
  try
    Person:=TPerson.Create;
    try
       person.Name := 'Hello';
       jsonstr:=  Person.SaveSettings;
       writeln(jsonstr); //write {"name":"hello"}
       person.Name := 'hi';
       Person.LoadSettings(jsonstr);
       writeln(person.Name);    //write hi instead of Hello
       readln;
    finally
      Person.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

 

unit Unit1;

interface

type
  TPerson = class(TObject)

  private
    FName: string;
  public
    procedure LoadSettings(const aJsonString: String);
    function SaveSettings: string;
    property Name: string read FName write FName;
  end;

implementation

uses REST.JSON;

procedure TPerson.LoadSettings(const aJsonString: String);
begin
  self := TJSon.JsonToObject<TPerson>(aJsonString);
end;

function TPerson.SaveSettings: string;
begin
  result := TJSon.ObjectToJsonString(self);
end;

end.

Ps: I'm using Delphi RIO Updt 2

Share this post


Link to post

Pretty weird. Never seen assigning something to "self", I'm wondering that this compiles.

But for your problem, think on "self" like if it was a variable. 

 

It should be rather Person := TJSon.JsonToObject<TPerson>(aJsonString); 

Share this post


Link to post
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils,
  REST.JSON,
  Unit1 in 'Unit1.pas';

var
  Person1, Person2: TPerson;
  jsonstr: string;
begin
  Person1 := TPerson.Create;
  try
     Person1.Name := 'Hello';
     jsonstr := Person1.SaveSettings;
     writeln(jsonstr); //write {"name":"Hello"}
     Person1.Name := 'hi';
     Person1.LoadSettings(jsonstr);
     writeln(Person1.Name); //write Hello
     readln;
  finally
    Person1.Free;
  end;
  
  jsonstr := '{"name":"Stephane"}';
  Person2 := TPerson.FromString(jsonstr);
  try
    writeln(Person2.Name); //write Stephane
    readln;
  finally
    Person2.Free;
  end;
end.
unit Unit1;

interface

type
  TPerson = class(TObject)
  private
    [JSONNameAttribute('NAME')]
    FName: string;
  public
    class function FromString(const aJsonString: String): TPerson;
    procedure LoadSettings(const aJsonString: String);
    function SaveSettings: string;
    property Name: string read FName write FName;
  end;

implementation

uses REST.JSON;

class function TPerson.FromString(const aJsonString: String): TPerson;
begin
  Result := TJSON.JsonToObject<TPerson>(aJsonString);
end;

procedure TPerson.LoadSettings(const aJsonString: String);
var
  oJSON: TJSONObject;
begin
  oJSON := TJSONObject.ParseJSONValue(aJsonString) as TJSONObject;
  try
    TJSON.JsonToObject(self, oJSON);
  finally
    oJSON.Free;
  end;
end;

function TPerson.SaveSettings: String;
begin
  Result := TJSON.ObjectToJsonString(self);
end;

end.

Reference

Edited by Dmitriy M
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×