cxflag203/superobject

TSupperRttiContext deals with fields, but not with properties

Opened this issue · 0 comments

What steps will reproduce the problem?

Let's have the following class:

Type
  TPerson = class
  private
    FName: string;
    FAge: integer;
    procedure SetAge(const Value: integer);
    procedure SetName(const Value: string);
  public
    property Name: string read FName write SetName;
    property Age: integer read FAge write SetAge;
  end;

Implementation

{ TPerson }

procedure TPerson.SetAge(const Value: integer);
begin
  FAge := Value;
  // Perform some other operations here
end;

procedure TPerson.SetName(const Value: string);
begin
  FName := Value;
  // Perform some other operations here
end;

Let's serialize it:

procedure TForm3.Button1Click(Sender: TObject);
var
  p: TPerson;
  s: string;
  LCtx: TSuperRttiContext;
begin
  p := TPerson.Create;
  p.Name := 'John Doe';
  p.Age := 25;
  LCtx := TSuperRttiContext.Create;
  try
    s := LCtx.AsJson<TPerson>(AObject).AsString;
  finally
    LCtx.Free;
  end;
end;

What is the expected output? 

 // The keys are the names of the properties
 s == '{"Name":"John Doe","Age":25}'

What do you see instead?

 // In the current SO version the keys are named as object field's names
 s == '{"FName":"John Doe","FAge":25}'

What version of the product are you using? On what operating system?

- Current

Please provide any additional information below.

TSupperRttiContext enumerates objects fields. This is ok since the data 
integrity is ok. 

But this way it bypasses the execution of eventual getters/setters at 
deserialization. These getters/setters may in turn do some other logic, like 
logging into a database, sending something over HTTP, changing some other 
fields etc. This is dangerous!

I think TSupperRttiContext should work with Properties instead of Fields, just 
like Rest.Json.TJson in Delphi XE5 and later. Or TSupperRttiContext may provide 
additional methods AsJsonNew/AsTypeNew to preserve backward compatibility.

Original issue reported on code.google.com by Peshe...@gmail.com on 5 Jan 2015 at 10:50