| <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <!-- NewPage --> |
| <html lang="en"> |
| <head> |
| <!-- Generated by javadoc (1.8.0_11) on Mon Aug 11 16:48:20 PDT 2014 --> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title>JsonDeserializer (Gson 2.3 API)</title> |
| <meta name="date" content="2014-08-11"> |
| <link rel="stylesheet" type="text/css" href="../../../stylesheet.css" title="Style"> |
| <script type="text/javascript" src="../../../script.js"></script> |
| </head> |
| <body> |
| <script type="text/javascript"><!-- |
| try { |
| if (location.href.indexOf('is-external=true') == -1) { |
| parent.document.title="JsonDeserializer (Gson 2.3 API)"; |
| } |
| } |
| catch(err) { |
| } |
| //--> |
| var methods = {"i0":6}; |
| var tabs = {65535:["t0","All Methods"],2:["t2","Instance Methods"],4:["t3","Abstract Methods"]}; |
| var altColor = "altColor"; |
| var rowColor = "rowColor"; |
| var tableTab = "tableTab"; |
| var activeTableTab = "activeTableTab"; |
| </script> |
| <noscript> |
| <div>JavaScript is disabled on your browser.</div> |
| </noscript> |
| <!-- ========= START OF TOP NAVBAR ======= --> |
| <div class="topNav"><a name="navbar.top"> |
| <!-- --> |
| </a> |
| <div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div> |
| <a name="navbar.top.firstrow"> |
| <!-- --> |
| </a> |
| <ul class="navList" title="Navigation"> |
| <li><a href="../../../overview-summary.html">Overview</a></li> |
| <li><a href="package-summary.html">Package</a></li> |
| <li class="navBarCell1Rev">Class</li> |
| <li><a href="class-use/JsonDeserializer.html">Use</a></li> |
| <li><a href="package-tree.html">Tree</a></li> |
| <li><a href="../../../deprecated-list.html">Deprecated</a></li> |
| <li><a href="../../../index-all.html">Index</a></li> |
| <li><a href="../../../help-doc.html">Help</a></li> |
| </ul> |
| </div> |
| <div class="subNav"> |
| <ul class="navList"> |
| <li><a href="../../../com/google/gson/JsonDeserializationContext.html" title="interface in com.google.gson"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../index.html?com/google/gson/JsonDeserializer.html" target="_top">Frames</a></li> |
| <li><a href="JsonDeserializer.html" target="_top">No Frames</a></li> |
| </ul> |
| <ul class="navList" id="allclasses_navbar_top"> |
| <li><a href="../../../allclasses-noframe.html">All Classes</a></li> |
| </ul> |
| <div> |
| <script type="text/javascript"><!-- |
| allClassesLink = document.getElementById("allclasses_navbar_top"); |
| if(window==top) { |
| allClassesLink.style.display = "block"; |
| } |
| else { |
| allClassesLink.style.display = "none"; |
| } |
| //--> |
| </script> |
| </div> |
| <div> |
| <ul class="subNavList"> |
| <li>Summary: </li> |
| <li>Nested | </li> |
| <li>Field | </li> |
| <li>Constr | </li> |
| <li><a href="#method.summary">Method</a></li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li>Constr | </li> |
| <li><a href="#method.detail">Method</a></li> |
| </ul> |
| </div> |
| <a name="skip.navbar.top"> |
| <!-- --> |
| </a></div> |
| <!-- ========= END OF TOP NAVBAR ========= --> |
| <!-- ======== START OF CLASS DATA ======== --> |
| <div class="header"> |
| <div class="subTitle">com.google.gson</div> |
| <h2 title="Interface JsonDeserializer" class="title">Interface JsonDeserializer<T></h2> |
| </div> |
| <div class="contentContainer"> |
| <div class="description"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <dl> |
| <dt><span class="paramLabel">Type Parameters:</span></dt> |
| <dd><code>T</code> - type for which the deserializer is being registered. It is possible that a |
| deserializer may be asked to deserialize a specific generic type of the T.</dd> |
| </dl> |
| <hr> |
| <br> |
| <pre>public interface <span class="typeNameLabel">JsonDeserializer<T></span></pre> |
| <div class="block"><p>Interface representing a custom deserializer for Json. You should write a custom |
| deserializer, if you are not happy with the default deserialization done by Gson. You will |
| also need to register this deserializer through |
| <a href="../../../com/google/gson/GsonBuilder.html#registerTypeAdapter-java.lang.reflect.Type-java.lang.Object-"><code>GsonBuilder.registerTypeAdapter(Type, Object)</code></a>.</p> |
| |
| <p>Let us look at example where defining a deserializer will be useful. The <code>Id</code> class |
| defined below has two fields: <code>clazz</code> and <code>value</code>.</p> |
| |
| <pre> |
| public class Id<T> { |
| private final Class<T> clazz; |
| private final long value; |
| public Id(Class<T> clazz, long value) { |
| this.clazz = clazz; |
| this.value = value; |
| } |
| public long getValue() { |
| return value; |
| } |
| } |
| </pre> |
| |
| <p>The default deserialization of <code>Id(com.foo.MyObject.class, 20L)</code> will require the |
| Json string to be <code>{"clazz":com.foo.MyObject,"value":20}</code>. Suppose, you already know |
| the type of the field that the <code>Id</code> will be deserialized into, and hence just want to |
| deserialize it from a Json string <code>20</code>. You can achieve that by writing a custom |
| deserializer:</p> |
| |
| <pre> |
| class IdDeserializer implements JsonDeserializer<Id>() { |
| public Id deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) |
| throws JsonParseException { |
| return new Id((Class)typeOfT, id.getValue()); |
| } |
| </pre> |
| |
| <p>You will also need to register <code>IdDeserializer</code> with Gson as follows:</p> |
| |
| <pre> |
| Gson gson = new GsonBuilder().registerTypeAdapter(Id.class, new IdDeserializer()).create(); |
| </pre> |
| |
| <p>New applications should prefer <a href="../../../com/google/gson/TypeAdapter.html" title="class in com.google.gson"><code>TypeAdapter</code></a>, whose streaming API |
| is more efficient than this interface's tree API.</div> |
| <dl> |
| <dt><span class="simpleTagLabel">Author:</span></dt> |
| <dd>Inderjeet Singh, Joel Leitch</dd> |
| </dl> |
| </li> |
| </ul> |
| </div> |
| <div class="summary"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <!-- ========== METHOD SUMMARY =========== --> |
| <ul class="blockList"> |
| <li class="blockList"><a name="method.summary"> |
| <!-- --> |
| </a> |
| <h3>Method Summary</h3> |
| <table class="memberSummary" border="0" cellpadding="3" cellspacing="0" summary="Method Summary table, listing methods, and an explanation"> |
| <caption><span id="t0" class="activeTableTab"><span>All Methods</span><span class="tabEnd"> </span></span><span id="t2" class="tableTab"><span><a href="javascript:show(2);">Instance Methods</a></span><span class="tabEnd"> </span></span><span id="t3" class="tableTab"><span><a href="javascript:show(4);">Abstract Methods</a></span><span class="tabEnd"> </span></span></caption> |
| <tr> |
| <th class="colFirst" scope="col">Modifier and Type</th> |
| <th class="colLast" scope="col">Method and Description</th> |
| </tr> |
| <tr id="i0" class="altColor"> |
| <td class="colFirst"><code><a href="../../../com/google/gson/JsonDeserializer.html" title="type parameter in JsonDeserializer">T</a></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/JsonDeserializer.html#deserialize-com.google.gson.JsonElement-java.lang.reflect.Type-com.google.gson.JsonDeserializationContext-">deserialize</a></span>(<a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson">JsonElement</a> json, |
| <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Type.html?is-external=true" title="class or interface in java.lang.reflect">Type</a> typeOfT, |
| <a href="../../../com/google/gson/JsonDeserializationContext.html" title="interface in com.google.gson">JsonDeserializationContext</a> context)</code> |
| <div class="block">Gson invokes this call-back method during deserialization when it encounters a field of the |
| specified type.</div> |
| </td> |
| </tr> |
| </table> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| <div class="details"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <!-- ============ METHOD DETAIL ========== --> |
| <ul class="blockList"> |
| <li class="blockList"><a name="method.detail"> |
| <!-- --> |
| </a> |
| <h3>Method Detail</h3> |
| <a name="deserialize-com.google.gson.JsonElement-java.lang.reflect.Type-com.google.gson.JsonDeserializationContext-"> |
| <!-- --> |
| </a> |
| <ul class="blockListLast"> |
| <li class="blockList"> |
| <h4>deserialize</h4> |
| <pre><a href="../../../com/google/gson/JsonDeserializer.html" title="type parameter in JsonDeserializer">T</a> deserialize(<a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson">JsonElement</a> json, |
| <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Type.html?is-external=true" title="class or interface in java.lang.reflect">Type</a> typeOfT, |
| <a href="../../../com/google/gson/JsonDeserializationContext.html" title="interface in com.google.gson">JsonDeserializationContext</a> context) |
| throws <a href="../../../com/google/gson/JsonParseException.html" title="class in com.google.gson">JsonParseException</a></pre> |
| <div class="block">Gson invokes this call-back method during deserialization when it encounters a field of the |
| specified type. |
| <p>In the implementation of this call-back method, you should consider invoking |
| <a href="../../../com/google/gson/JsonDeserializationContext.html#deserialize-com.google.gson.JsonElement-java.lang.reflect.Type-"><code>JsonDeserializationContext.deserialize(JsonElement, Type)</code></a> method to create objects |
| for any non-trivial field of the returned object. However, you should never invoke it on the |
| the same type passing <code>json</code> since that will cause an infinite loop (Gson will call your |
| call-back method again).</div> |
| <dl> |
| <dt><span class="paramLabel">Parameters:</span></dt> |
| <dd><code>json</code> - The Json data being deserialized</dd> |
| <dd><code>typeOfT</code> - The type of the Object to deserialize to</dd> |
| <dt><span class="returnLabel">Returns:</span></dt> |
| <dd>a deserialized object of the specified type typeOfT which is a subclass of <code>T</code></dd> |
| <dt><span class="throwsLabel">Throws:</span></dt> |
| <dd><code><a href="../../../com/google/gson/JsonParseException.html" title="class in com.google.gson">JsonParseException</a></code> - if json is not in the expected format of <code>typeofT</code></dd> |
| </dl> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| </div> |
| <!-- ========= END OF CLASS DATA ========= --> |
| <!-- ======= START OF BOTTOM NAVBAR ====== --> |
| <div class="bottomNav"><a name="navbar.bottom"> |
| <!-- --> |
| </a> |
| <div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div> |
| <a name="navbar.bottom.firstrow"> |
| <!-- --> |
| </a> |
| <ul class="navList" title="Navigation"> |
| <li><a href="../../../overview-summary.html">Overview</a></li> |
| <li><a href="package-summary.html">Package</a></li> |
| <li class="navBarCell1Rev">Class</li> |
| <li><a href="class-use/JsonDeserializer.html">Use</a></li> |
| <li><a href="package-tree.html">Tree</a></li> |
| <li><a href="../../../deprecated-list.html">Deprecated</a></li> |
| <li><a href="../../../index-all.html">Index</a></li> |
| <li><a href="../../../help-doc.html">Help</a></li> |
| </ul> |
| </div> |
| <div class="subNav"> |
| <ul class="navList"> |
| <li><a href="../../../com/google/gson/JsonDeserializationContext.html" title="interface in com.google.gson"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../index.html?com/google/gson/JsonDeserializer.html" target="_top">Frames</a></li> |
| <li><a href="JsonDeserializer.html" target="_top">No Frames</a></li> |
| </ul> |
| <ul class="navList" id="allclasses_navbar_bottom"> |
| <li><a href="../../../allclasses-noframe.html">All Classes</a></li> |
| </ul> |
| <div> |
| <script type="text/javascript"><!-- |
| allClassesLink = document.getElementById("allclasses_navbar_bottom"); |
| if(window==top) { |
| allClassesLink.style.display = "block"; |
| } |
| else { |
| allClassesLink.style.display = "none"; |
| } |
| //--> |
| </script> |
| </div> |
| <div> |
| <ul class="subNavList"> |
| <li>Summary: </li> |
| <li>Nested | </li> |
| <li>Field | </li> |
| <li>Constr | </li> |
| <li><a href="#method.summary">Method</a></li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li>Constr | </li> |
| <li><a href="#method.detail">Method</a></li> |
| </ul> |
| </div> |
| <a name="skip.navbar.bottom"> |
| <!-- --> |
| </a></div> |
| <!-- ======== END OF BOTTOM NAVBAR ======= --> |
| <p class="legalCopy"><small>Copyright © 2008–2014 <a href="http://www.google.com">Google, Inc.</a>. All rights reserved.</small></p> |
| </body> |
| </html> |