From 57313b637aa5da22cc0b9322c5100c2ba8cfe811 Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 23 Aug 2020 09:48:53 +0000 Subject: [PATCH] * Merging revisions r44776 from trunk: ------------------------------------------------------------------------ r44776 | michael | 2020-04-18 12:48:08 +0200 (Sat, 18 Apr 2020) | 1 line * Refactored ObjectToJSON so descendents have more control (bug ID 36803) ------------------------------------------------------------------------ git-svn-id: branches/fixes_3_2@46627 - --- packages/fcl-json/src/fpjsonrtti.pp | 46 ++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/packages/fcl-json/src/fpjsonrtti.pp b/packages/fcl-json/src/fpjsonrtti.pp index a07e4f31e4..c92c1429d6 100644 --- a/packages/fcl-json/src/fpjsonrtti.pp +++ b/packages/fcl-json/src/fpjsonrtti.pp @@ -54,6 +54,8 @@ Type function IsChildStored: boolean; function StreamChildren(AComp: TComponent): TJSONArray; protected + Function GetPropertyList(aObject : TObject) : TPropInfoList; virtual; + Procedure StreamProperties(aObject : TObject;aList : TPropInfoList; aParent : TJSONObject); virtual; function StreamClassProperty(Const AObject: TObject): TJSONData; virtual; Function StreamProperty(Const AObject : TObject; Const PropertyName : String) : TJSONData; Function StreamProperty(Const AObject : TObject; PropertyInfo : PPropInfo) : TJSONData; @@ -757,12 +759,36 @@ begin Result:=(GetChildProperty<>'Children'); end; +Function TJSONStreamer.GetPropertyList(aObject : TObject) : TPropInfoList; + +begin + result:=TPropInfoList.Create(AObject,tkProperties); +end; + +Procedure TJSONStreamer.StreamProperties(aObject : TObject;aList : TPropInfoList; aParent : TJSONObject); + +Var + I : Integer; + PD : TJSONData; + +begin + For I:=0 to aList.Count-1 do + begin + PD:=StreamProperty(AObject,aList.Items[i]); + If (PD<>Nil) then + begin + if jsoLowerPropertyNames in Options then + aParent.Add(LowerCase(aList.Items[I]^.Name),PD) + else + aParent.Add(aList.Items[I]^.Name,PD); + end; + end; +end; + function TJSONStreamer.ObjectToJSON(Const AObject: TObject): TJSONObject; Var PIL : TPropInfoList; - PD : TJSONData; - I : Integer; begin Result:=Nil; @@ -782,20 +808,12 @@ begin Result.Add('Objects', StreamTList(TList(AObject))) else begin - PIL:=TPropInfoList.Create(AObject,tkProperties); + PIL:=GetPropertyList(aObject); +// TPropInfoList.Create(AObject,tkProperties); try - For I:=0 to PIL.Count-1 do - begin - PD:=StreamProperty(AObject,PIL.Items[i]); - If (PD<>Nil) then begin - if jsoLowerPropertyNames in Options then - Result.Add(LowerCase(PIL.Items[I]^.Name),PD) - else - Result.Add(PIL.Items[I]^.Name,PD); - end; - end; + StreamProperties(aObject,PIL,Result); finally - FReeAndNil(Pil); + FreeAndNil(Pil); end; If (jsoStreamChildren in Options) and (AObject is TComponent) then Result.Add(ChildProperty,StreamChildren(TComponent(AObject)));