Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerics and Marshal / UnMarshal. What am I missing here?
    primarykey
    data
    text
    <p>Better mention this : I'm using Delphi XE2 - but XE or 2010 should do the trick too :-)</p> <p><strong>This Question is now in Quality Central <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=99313" rel="nofollow">QC#99313</a> please vote it up :-)</strong></p> <p><strong>As of 10-20-2011 Embarcadero has marked the QC report as RESOLVED. Solution was provided by SilverKnight. But the lack of information from Embarcadero worries me. Since the solution suggests using other source code than the one explained in XE(2) Help system, other forums and CC. But have a look at the QC yourself.</strong></p> <p>Given these type declarations:</p> <pre><code>type TTestObject : Class aList : TStringList; function Marshal : TJSonObject; end; TTestObjectList&lt;T:TestObject&gt; : Class(TObjectList&lt;T&gt;) function Marshal : TJSonObject; // How to write this ? end; </code></pre> <p>I would like to implement a Marshal method for TTestObjectList. To my best knowledge - I should register a converter for TTestObject and for the beauty of it - call Marshal for each element.</p> <p>The Marshal for TTestObject registers this converter:</p> <pre><code>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); </code></pre> <p>The Generic TTestObjectList Marshal method:</p> <pre><code>function TTestObjectList&lt;T&gt;.Marshal: TJSONObject; var Mar : TJsonMarshal; // is actually a property on the list. begin Mar := TJsonMarshal.Create(TJSONConverter.Create); try RegisterConverter(TTestObject, function(Data: TObject): TObject begin Result := TTestObject(Data).Marshal; end); Result := Mar.Marshal(Self) as TJSONObject; finally Mar.Free; end; end; </code></pre> <p>Here is a simplified example of using the list.</p> <pre><code>var aTestobj : TTestObject; aList : TTestObjectList&lt;TTestObject&gt;; aJsonObject : TJsonObject; begin aTestObj := TTestObject.Create; // constructor creates and fills TStringlist with dummy data. aJsonObject := aTestObj.Marshal; // This works as intended. aList := TTestObjectList&lt;TTestObject&gt;.Create; aJsonObject := aList.Marshal; // Fails with tkpointer is unknown .... end; </code></pre> <p>Of course I have similar functionality for restoring (unmarshal). But the above code should work - at least to my best knowledge.</p> <p>So if anyone can point out to me :</p> <p>Why the List fails to marshal?</p> <p>I know I have the TJsonMarshal property on my list - but it also has a converter/reverter.</p> <p>Changing to a TTypeStringConverter (instead of TTypeObjectConverter) will return a valid string. But I like the idea of working on a TJsonObject all along. Otherwise I would have the same problem (or something similar) when doing the unmarshalling from a string to TTestObject.</p> <p>Any advice / ideas are most welcome.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload