Better mention this: I am using Delphi XE2 - but XE or 2010 should do the trick too :-)
This question is now in Quality Central QC # 99313 , please vote for it :-)
As of 10-20-2011, Embarcadero marked the quality control report as RESOLVED. The solution was provided by SilverKnight. But I am concerned about the lack of information from Embarcadero. Because the solution suggests using a different source code than the one explained in the XE (2) help system, other forums, and CC. But look at QC yourself.
Given these type declarations:
type TTestObject : Class aList : TStringList; function Marshal : TJSonObject; end; TTestObjectList<T:TestObject> : Class(TObjectList<T>) function Marshal : TJSonObject; // How to write this ? end;
I would like to implement a marshal method for TTestObjectList. To my best knowledge - I have to register the converter for TTestObject and for the beauty of it - call the marshal for each element.
The marshal for TTestObject registers this converter:
RegisterConverter(TStringList, function(Data: TObject): TListOfStrings var i, Count: Integer; begin Count := TStringList(Data).Count; SetLength(Result, Count); for i := 0 to Count - 1 do Result[i] := TStringList(Data)[i]; end);
General Marshal TTestObjectList Method:
function TTestObjectList<T>.Marshal: TJSONObject; var Mar : TJsonMarshal;
Here is a simplified example of using a list.
var aTestobj : TTestObject; aList : TTestObjectList<TTestObject>; aJsonObject : TJsonObject; begin aTestObj := TTestObject.Create; // constructor creates and fills TStringlist with dummy data. aJsonObject := aTestObj.Marshal; // This works as intended. aList := TTestObjectList<TTestObject>.Create; aJsonObject := aList.Marshal; // Fails with tkpointer is unknown .... end;
Of course, I have similar functionality for recovery (unmarshal). But the code above should work - at least to my best knowledge.
So, if anyone can tell me:
Why doesn't the list marshal?
I know that I have the TJsonMarshal property on my list, but it also has a converter / converter.
Going to TTypeStringConverter (instead of TTypeObjectConverter) will return a valid string. But I like the idea of ββworking on TJsonObject. Otherwise, I would have the same problem (or something similar) when doing unmarshalling from a string in TTestObject.
Any tips / ideas are welcome.