You are not logged in.
Pages: 1
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
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
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
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
Pages: 1