Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should look at <a href="http://books.google.com/books?id=ka2VUBqHiWkC&amp;pg=PA142&amp;lpg=PA142&amp;dq=effective+java+heterogeneous+type&amp;source=bl&amp;ots=yXLiNgo6T0&amp;sig=t035r7WbwAGmJqGaUOEMRnWGKkU&amp;hl=en&amp;ei=N5QZTffhDoSq8AbogJnKDQ&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=1&amp;ved=0CBcQ6AEwAA#v=onepage&amp;q&amp;f=false" rel="nofollow noreferrer">Effective Java 2, Item 29: <em>Consider typesafe heterogeneous type containers</em></a></p> <p>The basic idea is that you could imagine you have some <code>Deserializer</code> interface. Then you can have a <code>Map&lt;Class&lt;?&gt;, Deserializer&lt;String, Object&gt;&gt;</code>. The map is basically a registry from type to the proper <code>Deserializer</code> for that type (which deserializes from a String (perhaps you want a json type or something instead of String, but String works) to the type of interest.</p> <p>The real trick is that <em>you must enforce</em> that the class key type matches the deserializer type. i.e. - For some Class that is a key in the map, you have a Deserializer as a value. You can do this with a generic method. For example:</p> <pre><code>&lt;T&gt; void put(Class&lt;T&gt; clazz, Deserializer&lt;String, T&gt; deserializer) { map.put(clazz, deserializer); } </code></pre> <p>Then to use the heterogeneous map, you can use a generic method again:</p> <pre><code>&lt;T&gt; T deserialize(Class&lt;T&gt; typeToDeserializeFromJson, String toDeserialize) { return typeToDeserializeFromJson.cast( deserializers.get(tTDFJ).deserialize(toDeserialize)); } </code></pre> <p>You use the type that is specified by the method caller to</p> <ul> <li>lookup the right `Deserializer` <li>deserialize the json text to an `Object` <li>safely cast the `Object` to the right type which we know is safe because of how we constructed the map in the first place. </ul> <p>I hope what I said was at least somewhat clear. Reading the corresponding entry in EJ2 should help!</p> <p><strong><em>UPDATE:</em></strong></p> <p><a href="https://stackoverflow.com/questions/4544191/how-do-i-correctly-use-generics/4544641#4544641">As Abhinav points out</a>, the GSON library will help you accomplish your final goal of deserializing objects in a way that uses generics appropriately. However, I think your question is more about generics than the end goal so I feel my answer is more appropriate. GSON must be implemented using a heterogeneous type container :-).</p> <p>Thanks, <a href="https://stackoverflow.com/users/526635/abhinav">Abhinav</a> for pointing out GSON!</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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