#1 2014-09-21 03:43:54

jsmart
Member
Registered: 2014-09-19
Posts: 8

Can't deserialize with nested object

With this sample code, why is it not automatically able to deserialize json string which has been successfully serialized with opposite function?

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, Contnrs, Classes,

  SynCommons, mORMot;

type
  {$M+}
  TMyObj2 = class(TPersistent)
  private
    FTesting: string;
  public
    constructor Create; virtual;
  published
    property Testing: string read FTesting write FTesting;
  end;
  {$M-}

  {$M+}
  TMyObject = class(TPersistent)
  private
    FAValue1: string;
    FAValue2: string;
    FAValue3: Integer;
    FMyObj2: TMyObj2;
  public
    constructor Create; virtual;
    function SetCreate(AValue1, AValue2: string; AValue3: Integer): TMyObject;
  published
    destructor Destroy; override;
    property MyObj2: TMyObj2 read FMyObj2;
    property AValue1: string read FAValue1 write FAValue1;
    property AValue2: string read FAValue2 write FAValue2;
    property AValue3: Integer read FAValue3 write FAValue3;
  end;
  {$M-}

{ TMyObj2 }

constructor TMyObj2.Create;
begin
  inherited;

end;

{ TMyObject }

constructor TMyObject.Create;
begin
  FMyObj2 := TMyObj2.Create;
end;

function TMyObject.SetCreate(AValue1, AValue2: string; AValue3: Integer): TMyObject;
begin

  FAValue1 := AValue1;
  FAValue2 := AValue2;
  FAValue3 := AValue3;
  Result := Self;
end;


destructor TMyObject.Destroy;
begin
  FMyObj2.Free;

  inherited;
end;

var
  List: TObjectList;
  Json: RawUTF8;
  List2: TObjectList;
  Valid: Boolean;
  From: PUTF8Char;
  Obj: TMyObject;
  Obj2: TObject;
begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    Obj := tMyObject.Create.SetCreate('a','b',1);
    try    with
      Json := ObjectToJson(Obj, [woStoreClassName]);
      Writeln(Utf8ToString(Json));
      Writeln('===================================================');
      From := @Json[1];
      Obj2 := JsonToNewObject(From, Valid, []);
      if not Valid then
        Writeln('not valid');
      if assigned(Obj2) then
        writeln(obj2.ClassName)
      else
        writeln('unassigned');
    finally
      Obj.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

It correctly creates the Json string with the nested object, but when I attempt to pass that exact same json string back into the deserializer, it claims it's not valid. I can't find anywhere in the documentation talking about this.

Thanks

Offline

#2 2014-09-21 07:01:01

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

Re: Can't deserialize with nested object

Which version of the source code are you using?

BTW, as stated by the documentation, you should use TPersistentWithCustomCreate instead of TPersistent for TMyObj2.

Offline

#3 2014-09-21 07:30:43

jsmart
Member
Registered: 2014-09-19
Posts: 8

Re: Can't deserialize with nested object

Yeah I did descend from TPersistentWithCustomCreate  - I probably posted code after I'd played around a bit...

I'm using version 1.18

Thanks

Offline

#4 2014-09-21 08:11:51

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

Re: Can't deserialize with nested object

jsmart wrote:

I'm using version 1.18

Which revision?
Current is 1.18.272

Offline

#5 2014-09-21 11:04:47

jsmart
Member
Registered: 2014-09-19
Posts: 8

Re: Can't deserialize with nested object

Thanks...

I'm on 1.18.273 according to SynopseCommit.inc.  I just did a pull now (I use git repository).

Just tried again but same result.

Offline

#6 2014-09-25 09:50:07

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

Re: Can't deserialize with nested object

How are you using TPersistentWithCustomCreate ?

Offline

#7 2014-09-25 10:04:41

jsmart
Member
Registered: 2014-09-19
Posts: 8

Re: Can't deserialize with nested object

Sorry, you're right.
I did test with that but failed, but worked ok now.

My apologies.  Thanks - I think I needed to have ALL objects descend from TPersistentWithCustomCreate not just the nested object. (in this case TMyObject).

Cheers

Offline

#8 2014-09-25 16:29:38

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

Re: Can't deserialize with nested object

The parent object needs to inherit from this class to have its constructor called and create the nested objects, indeed.

Offline

Board footer

Powered by FluxBB