#1 2016-07-18 16:14:09

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

JSON Deserialization question

Hello,

I've a issue with TConfiguration class that failed to deserialize.

{$M+}
type
 TConnectionInfos = class(TPersistent)
  private
    FHost: String;
    FPassword: String;
    FPort: string;
    FUsername:String;
 public
   function IsAlive: Boolean;
  published
    property Username:String read FUsername write FUsername;
    property Password:String read FPassword write FPassword;
    property Host:String read FHost write FHost;
    property Port:string read FPort write FPort;
end;
{$M-}

{$M+}
type
 TDBConnectionInfos = class(TConnectionInfos)
 private
   FDatabase: String;
   FProvider: String;
   FQuotedChar: String;
 published
   property Database: String read FDatabase write FDatabase;
   property Provider:String read FProvider write FProvider;
   property QuotedChar: String read FQuotedChar write FQuotedChar;
 end;
{$M-}

{$M+}
type
  { TConfiguration }
  TConfiguration = class(TPersistent)
  private
    class var FSingleton: TConfiguration;
  private
    FLocal: TDBConnectionInfos;
    FRemote: TDBConnectionInfos;
    constructor Init;
    procedure SetLocal(const Value: TDBConnectionInfos);
    procedure SetRemote(const Value: TDBConnectionInfos);
  public
    class function Instance: TConfiguration;
    class destructor DestroyClass;
  published
    property Local: TDBConnectionInfos read FLocal write SetLocal;
    property Remote: TDBConnectionInfos read FRemote write SetRemote;
  end;
{$M-}

TJSONSerializer.RegisterClassForJSON([TConnectionInfos,TDBConnectionInfos,TConfiguration]);

Bellow is the result of a serialization :

{"Local":{"Database":"D:\\Test.DB","Provider":"SQLite","QuotedChar":"","Username":"","Password":"","Host":"","Port":""},"Remote":{"Database":"dd","Provider":"PG","QuotedChar":"\"","Username":"test","Password":"@@","Host":"localhost","Port":"5432"}}

But JSONToNewObject fails to create a new object.

My class inherit from TPersistent, I have added all my classes to RegisterClassForJSON !
What am I missing ?

Ps : my code must works under wince (fpc 3) and android (delphi xe berlin)

Last edited by swierzbicki (2016-07-18 16:14:41)

Offline

#2 2016-07-19 09:16:03

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

Re: JSON Deserialization question

Android under Delphi is using a new NextGen compiler, and a new ARC memory model, which is not supported by the main framework units.
So you can't use mORMot.pas with Delphi for Android.
Only the cross-platform units are available, with a reduced set.

AFAIR JSONToNewObject() expects the class name to be part of the JSON.
As written in the doc:

// - woStoreClassName option shall have been used at ObjectToJSON() call

And BTW {$M+} / {$M-} are IMHO superfluous, since they are already part of TPersistent.

Offline

Board footer

Powered by FluxBB