onryldz / x-superobject

Delphi Cross Platform Rapid JSON
316 stars 118 forks source link

Support for loading TObjectList<T> class properties #33

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. see attached demo source
2. saving my class went fine, but if i try to load from json i get an 
EPropReadOnly exception

What version of the product are you using? On what operating system?
Delphi XE3 with r64 @ Windows 8.1 x64

Please provide any additional information below.
see attached dpr file

Original issue reported on code.google.com by ros...@gmail.com on 12 May 2014 at 11:23

Attachments:

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r67.

Original comment by onryld...@gmail.com on 23 May 2014 at 3:25

GoogleCodeExporter commented 9 years ago
Hello,
I tried exact that with the current version of the xsuperobject (I tried also 
the sample-code of the demo1.dpr in the attachment of the issue), but I get the 
same error-message. Can you help me? Maybe, a short code-snippet of the usage 
with TList or TObjectList?
Thanks!

Original comment by svenbemm...@gmail.com on 20 Aug 2014 at 10:14

GoogleCodeExporter commented 9 years ago
Hello, a simple sample

type

  TDetail = class
    Name: String;
    Size: Double;
  end;

  TMaster = class
    List: TObjectList<TDetail>;
  end;

  ...

var
  Test: TMaster;
begin
  Test := TMaster.FromJSON('{List: [{Name:"Test Test", Size: 1.82}, {Name:"Foo Foo", Size: 1.72} ]}');
  ShowMessage(Test.List[0].Name);
end;

Original comment by onryld...@gmail.com on 20 Aug 2014 at 10:28

GoogleCodeExporter commented 9 years ago
Hi, thanks for the code, in that case it runs, but I tried to generate the JSON 
from the object and then regenerate the object from the generated JSON and that 
won't run :-(. Here is the code and the generated JSON:

type
  TDetail = class
   public
    Name: String;
    Size: Double;
  end;
  TMaster = class
    List: TObjectList<TDetail>;
  end;
var
 Master: TMaster;
 json: string;
 Detail:TDetail;
{ TObj }

begin
 try
  Master:=TMaster.Create;
  Detail:=TDetail.Create;
  Detail.Name:='Hallo';
  Detail.Size:=100;
  Master.List:=  TObjectList<TDetail>.Create;
  Master.List.Add(Detail);
  json:=Master.AsJSON;
  FreeAndNil(Master);
  Master:=TMaster.FromJSON(json);  //get an EInvalidCast exception

 except
  on E: Exception do
   Writeln(E.ClassName, ': ', E.Message);
 end;

 The Value in the json-string-variable is

  {"List":{"OwnsObjects":true,"Capacity":1,"Count":1,"List" {"Name":"Hallo","Size":100}],"FOwnsObjects":true,"FItems":[{"Name":"Hallo","Size":100}],"FCount":1,"FArrayManager":{}}}

Thanks for your help!

Original comment by svenbemm...@gmail.com on 21 Aug 2014 at 7:30

GoogleCodeExporter commented 9 years ago
There was an error in the JSON-string, this is the generated string:

{"List":{"OwnsObjects":true,"Capacity":1,"Count":1,"List":[{"Name":"Hallo","Size
":100}],"FOwnsObjects":true,"FItems":[{"Name":"Hallo","Size":100}],"FCount":1,"F
ArrayManager":{}}}

Original comment by svenbemm...@gmail.com on 21 Aug 2014 at 7:37