I try to enumerate an object published properties recursively with RTTI to get a string of the structure like this property = value. How can i go threw sub-objects ?
class function TJSONUtils.ToString(aSender : TObject ; aLevel : integer = 0) : string;
const
SKIP_PROP_TYPES = [tkUnknown, tkInterface];
var
vC : TRttiContext;
vType : TRttiType;
vProperty : TRttiProperty;
s : string;
vValue : TValue;
vVal: string;
begin
vC := TRttiContext.Create;
vType := vC.GetType(aSender.ClassInfo);
for vProperty in vType.GetProperties do
begin
if (vProperty.IsReadable) and not (vProperty.PropertyType.TypeKind in SKIP_PROP_TYPES) and (vProperty.Visibility = mvPublished ) then
begin
AValue := vProperty.GetValue(aSender);
if AValue.IsEmpty then
begin
vVal := 'nil';
end
else
begin
if AValue.Kind in [tkUString, tkString, tkWString, tkChar, tkWChar] then
vVal := QuotedStr(AValue.ToString)
else
vVal := AValue.ToString;
end;
if pos(' @', sval) > 0 then
begin
s := s + vProperty.Name + ' => ' + TJSONUtils.ToString(TObject(AValue)); // here is the problem
end
else
s := s + inttostr(aLevel) + ' - ' + vProperty.Name + '=' + vVal + #$D#$A;
end;
end;
result := s;
end;
The object can be for exemple : TFill that contain a TGradient sub object, that containt TGradientPoints (3 sublevels)
var
fFill : TBrush;
begin
fFill := TBrush.create;
try
showmessage(TJSONUtils.ToString(fFill, 0));
finally
fFill.free;
end;
end;
how can i enumerate all elements of object and sub objects until going to base types : string, integer, float, etc... ?