Delphi Superobject, generic list to json

廉价感情. 提交于 2019-12-12 09:40:55

问题


I have a object with some TObjectList<>-fields that I try to encode as JSON with help form SuperObject.

TLogs = TObjectList<TLog>;
TMyObject = class(TObject)
private
  FLogs: TLogs;
end;

Deep inside SuperObjects code, there is a ToClass procedure, iterating the fields and add them to the json result.

In this loop, there is a check on the TRttiFields FieldType. If it's nil, it skips the object.

for f in Context.GetType(Value.AsObject.ClassType).GetFields do
  if f.FieldType <> nil then
  begin
    v := f.GetValue(value.AsObject);
    result.AsObject[GetFieldName(f)] := ToJson(v, index);
  end

My generic list fields have a FieldType of nil. Why?

How can I make SuperObject serialize my list of objects?


回答1:


This is a known issue in Delphi's RTTI creation. If you declare your generic class like that, it won't work. You need to use the class keyword.

TLogs = class(TObjectList<TLog>);

Hopefully this will be fixed in the next release.



来源:https://stackoverflow.com/questions/2882894/delphi-superobject-generic-list-to-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!