#1 2018-09-18 13:09:26

Goulu
Member
From: Switzerland
Registered: 2018-09-18
Posts: 6
Website

(Noob) how to stream a TDynArray of TObject ?

I came across your (great) TDynArray while trying to add support for dynamic arrays to our ClassStreamer which streams published properties in XML.
I have something like this:

TheObjValue := GetObjectProp(AClass, APPropInfo);
      TheTypeInfo := APPropInfo.PropType^;
      case TheTypeInfo^.Kind of
        tkClass : begin
          if TheObjValue is TStrings then 
            TheStrConfig.AddValue(string(APPropInfo^.Name), TStrings(TheObjValue).Text)
          else begin
            AddObj(TheObjValue, string(APPropInfo^.Name), false);
          end; //else
        end; // tkClass
        tkDynArray : begin
          TheElTypeInfo := TheTypeInfo.TypeData^.elType2^; // .eltype returns nil on XE5
          TheObjValue := GetObjectProp(AClass, APPropInfo);
          TheArray.InitSpecific(TheTypeInfo,TheObjValue, djObject); // Why not TheElTypeInfo.Kind ?
          LBuffer := JSONtoXML(TheArray.SaveToJSON());
          TheStrConfig.AddValue(string(APPropInfo^.Name), LBuffer);
        end // tkClass
        else if TheTypeInfo^.Kind in KNOW_TYPE_KIND then begin
          LVal := GetPropValue(AClass, string(APPropInfo^.Name));
          TheStrConfig.AddValue(string(APPropInfo^.Name), LVal);
        end //else if
      end; // case

A few stupid questions and remarks:
1. in TDynArray.Init I had to add

fElemType := PTypeInfo(aTypeInfo)^.elType;
  if fElemType = nil then
    fElemType := PTypeInfo(aTypeInfo)^.elType2;
  if fElemType<>nil then begin

to have something in my array. It probably needs to be guarded by $ifdef for other compilers (I use XE5)
2. why does TDynArray.InitSpecific require a TDynArrayKind and not a TTypeKind ? They look similar and it would be very handy... or am I missing something ?
3. TheArray.SaveToJSON() produces an array with the adresses of the TObject s in the array, which is a good start ... but how should I stream the objects themselves ?

Sorry for the Noob questions... A short link to a tutorial or example code will do as well as a long answer...

Thank you very much !

Last edited by Goulu (2018-09-18 13:11:16)

Offline

#2 2018-09-19 11:54:04

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 210

Re: (Noob) how to stream a TDynArray of TObject ?

but how should I stream the objects themselves?

Did you check the documentation? 10.1.6. TObject serialization

You also may find TDocVariant interesting.

Offline

Board footer

Powered by FluxBB