#1 2015-10-22 05:39:41

mitring
Member
Registered: 2015-10-22
Posts: 2

JSON serialize TObject

Hi I have problem with ObjectToJSON using SynCrossPlatformJSON.pas

  TTestJson = class(TObject)
  private
    FAge: Integer;
    FFirstName: string;
    FLastName: string;
    procedure SetAge(const Value: Integer);
    procedure SetFirstName(const Value: string);
    procedure SetLastName(const Value: string);
  public
    property Age: Integer read FAge write SetAge;
    property FirstName: string read FFirstName write SetFirstName;
    property LastName: string read FLastName write SetLastName;
  end;

{...}

var
  S1: TTestJson;
begin
  S1 := TTestJson.Create;
  try
    S1.Age       := 30;
    S1.FirstName := 'Peter';
    S1.LastName  := 'My Last Name';

    Memo1.Text := ObjectToJSON(S1); // <-- return 'null'
  finally
    S1.Free;
  end;
end;

How to correctly use ObjectToJSON using SynCrossPlatformJSON.pas

Thanks for help.

Last edited by mitring (2015-10-22 05:40:11)

Offline

#2 2015-10-22 06:10:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: JSON serialize TObject

Your class does not have RTTI, since it inherits from class(TObject).

Either define {$M+} ... {$M-} around the class definition, or inherit from class(TPersistent).

Offline

#3 2015-10-22 06:54:00

mitring
Member
Registered: 2015-10-22
Posts: 2

Re: JSON serialize TObject

Thanks for reply smile

Inherited by TPersistent is one and two propery not public but published

  TTestJson = class(TPersistent)
  private
    FAge: Integer;
    FFirstName: string;
    FLastName: string;
    procedure SetAge(const Value: Integer);
    procedure SetFirstName(const Value: string);
    procedure SetLastName(const Value: string);
  published
    property Age: Integer read FAge write SetAge;
    property FirstName: string read FFirstName write SetFirstName;
    property LastName: string read FLastName write SetLastName;
  end;

Once again thanks for help and for mORMot smile

Offline

Board footer

Powered by FluxBB