You are not logged in.
Hi,
We have an published property that is actually TCollectionItem and internally created. In delphi's object inspector it is normally visible
and streamable ...
1. problem with code in :
function TPropInfo.ClassFromJSON
...
if SetProc<>0 then begin
// it is a setter method -> create a temporary object
tmp := PropType^.ClassCreate; // it doesn't create TCollectionItem class
2. Is it possible to just set properties without creating published objects ?
Thank you and all the best,
Vojko Cenda
Offline
It is not clear to me how it may be possible "to just set properties without creating published objects", by the way...
I've added a new j2oSetterNoCreate option for JSONToObject() process. Is it what you expected?
See http://synopse.info/fossil/info/4bf51eadac
Offline
I've change the function so that the streamer checks whether the property has object. If it does it just write properties
and it doesn't free the object even if j2oSetterExpectsToFreeTempInstance is present.
function TPropInfo.ClassFromJSON(Instance: TObject; From: PUTF8Char;
var Valid: boolean; Options: TJSONToObjectOptions): PUTF8Char;
var Field: ^TObject;
tmp: TObject;
origobj: Boolean; // internal object
begin
valid := false;
origobj := False; // assume nil
result := nil;
if (@self=nil) or (PropType^.Kind<>tkClass) or (Instance=nil) then
exit;
if SetterIsField then
// setter to field -> direct in-memory access
Field := SetterAddr(Instance) else
{$ifndef FPC}
if (SetProc<>0) and not (j2oSetterNoCreate in Options) then begin
// it is a setter method -> create a temporary object
tmp := GetObjProp(Instance); // get property object
if tmp=nil then // nil
tmp := PropType^.ClassCreate
else
origobj := True; // flag it is already present
try
result := JSONToObject(tmp,From,Valid,nil,Options);
if not Valid then
if not origobj then FreeAndNil(tmp) else begin // free only if not internal
SetOrdProp(Instance,PtrInt(tmp)); // PtrInt(tmp) is OK for CPU64
if (not origobj) and (j2oSetterExpectsToFreeTempInstance in Options) then // free only if not internal
FreeAndNil(tmp);
end;
except
on e:Exception do
if (not origobj) then tmp.Free; // free only if not internal
end;
exit;
end else
{$endif}
if GetterIsField then
// no setter -> use direct in-memory access from getter (if available)
Field := GetterAddr(Instance) else
// no setter, nor direct field offset -> impossible to set the instance
exit;
result := JSONToObject(Field^,From,Valid,nil,Options);
end;
thank you
Offline