You are not logged in.
Pages: 1
Consider having classes defined like this:
TMyContainedClass = class
(...)
published
property Name: RawUTF8 read write;
end;
TMyContainedClassList = TObjectList;
TMyClass = class
(...)
published
property Name: RawUTF8 read write;
property MyContainedClassList: TMyContainedClassList read write;
end;
TMyClassList = TObjectList;
Having one objects in the lists I do serialization to Json string using ObjectToJSON().
As far as I see, instance of TMyClassList is correctly serialized.
Unfortunately, trying to de-serialize using TObjectList(JSONToNewObject(jsonPtr, valid)) set valid to False and returns nil
Is it correct behavior?
Offline
Ok, it seems not well formatted JSON for JSONToNewObject, as it does not start with '{'.
Tried with JSONToObject, same valid False result.
TMyClass instance created well and added to MyClassList. But new instance created contains nil for MyContainedClassList field and processing json fails.
Workaround is to manually create MyContainedClassList field on AfterConstruction, so nasted call to JSONToObject will have valid ObjectInstance as parameter.
Offline
Proposal for ClassInstanceCreate(aClass: TClass)
I see it checks whenever aClass is TSQLRecord, TInterfacedCollection, TCollection, ... in order proper class constructor to be called upon object creation.
It will be good TObjectList to be named there, and TObjectList constructor to be called, as it set FOwnsObjects to True. This is default behavior for TObjectList.
This will prevent memory leaks when TObjectList transmitted as a interface service parameter. Parameters are auto-freed, but one have to cycle all contained TObjectList in parameter and manually set OwnsItems to True.
Other case - unexpected memory leaks.
Offline
There is already the oObjectList internal kind of class within mORMot.pas.
Do you use the latest 1.18 version from http://synopse.info ?
Offline
Yes, latest sources I am using.
Offline
I've got the same problem (using 1.18). I want to read a complex JSON-String, which looks like:
{
"ClassName":"TMyClass",
"Name":"MainContainer",
"MyContainedClassList":
[
{
"ClassName":"TMyClass",
"Name":"Container",
"MyContainedClassList":
[
{
"ClassName":"TMyContainedClass",
"Name": "Name0",
},
{
"ClassName":"TMyContainedClass",
"Name": "Name1",
},
]
}
]
}
The first object of TMyClass ("MainContainer") is created by myself before executing the JSONtoObject-method. The second one ("Container") should be created by the JSONtoObject-method.
In my opinion the problem is in the mORMot.pas:26129
result := aClass.Create
There the constructor of TMyClass should be called to create the MyClass object with its TMyContainedClassList. But it never happens. A base class with a virtual constructor is necessary to do that. aClass itself is a TClass object (class of TObject) which hasn't got a virtual constructor.
Last edited by anvo (2013-03-12 13:34:10)
Offline
Did you register your class via TJSONSerializer.RegisterClassForJSON() ?
Other possibilitites:
- You can have a virtual constructor if you inherit from TInterfacedCollection - which is handled as expected by function ClassInstanceCreate().
- Or you can register your collection class/item pair via TJSONSerializer.RegisterCollectionForJSON().
Offline
TObjectList problem fixed by http://synopse.info/fossil/info/6e1fd07333
Thanks for the input!
Offline
Pages: 1