| <!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:21 PDT 2014 --> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title>TypeAdapter (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="TypeAdapter (Gson 2.3 API)"; |
| } |
| } |
| catch(err) { |
| } |
| //--> |
| var methods = {"i0":10,"i1":10,"i2":10,"i3":10,"i4":6,"i5":10,"i6":10,"i7":10,"i8":6}; |
| var tabs = {65535:["t0","All Methods"],2:["t2","Instance Methods"],4:["t3","Abstract Methods"],8:["t4","Concrete 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/TypeAdapter.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/LongSerializationPolicy.html" title="enum in com.google.gson"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../com/google/gson/TypeAdapterFactory.html" title="interface in com.google.gson"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../index.html?com/google/gson/TypeAdapter.html" target="_top">Frames</a></li> |
| <li><a href="TypeAdapter.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><a href="#constructor.summary">Constr</a> | </li> |
| <li><a href="#method.summary">Method</a></li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li><a href="#constructor.detail">Constr</a> | </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="Class TypeAdapter" class="title">Class TypeAdapter<T></h2> |
| </div> |
| <div class="contentContainer"> |
| <ul class="inheritance"> |
| <li><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">java.lang.Object</a></li> |
| <li> |
| <ul class="inheritance"> |
| <li>com.google.gson.TypeAdapter<T></li> |
| </ul> |
| </li> |
| </ul> |
| <div class="description"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <hr> |
| <br> |
| <pre>public abstract class <span class="typeNameLabel">TypeAdapter<T></span> |
| extends <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">Object</a></pre> |
| <div class="block">Converts Java objects to and from JSON. |
| |
| <h3>Defining a type's JSON form</h3> |
| By default Gson converts application classes to JSON using its built-in type |
| adapters. If Gson's default JSON conversion isn't appropriate for a type, |
| extend this class to customize the conversion. Here's an example of a type |
| adapter for an (X,Y) coordinate point: <pre> <code> |
| |
| public class PointAdapter extends TypeAdapter<Point> { |
| public Point read(JsonReader reader) throws IOException { |
| if (reader.peek() == JsonToken.NULL) { |
| reader.nextNull(); |
| return null; |
| } |
| String xy = reader.nextString(); |
| String[] parts = xy.split(","); |
| int x = Integer.parseInt(parts[0]); |
| int y = Integer.parseInt(parts[1]); |
| return new Point(x, y); |
| } |
| public void write(JsonWriter writer, Point value) throws IOException { |
| if (value == null) { |
| writer.nullValue(); |
| return; |
| } |
| String xy = value.getX() + "," + value.getY(); |
| writer.value(xy); |
| } |
| }</code></pre> |
| With this type adapter installed, Gson will convert <code>Points</code> to JSON as |
| strings like <code>"5,8"</code> rather than objects like <code>{"x":5,"y":8}</code>. In |
| this case the type adapter binds a rich Java class to a compact JSON value. |
| |
| <p>The <a href="../../../com/google/gson/TypeAdapter.html#read-com.google.gson.stream.JsonReader-"><code>read()</code></a> method must read exactly one value |
| and <a href="../../../com/google/gson/TypeAdapter.html#write-com.google.gson.stream.JsonWriter-T-"><code>write()</code></a> must write exactly one value. |
| For primitive types this is means readers should make exactly one call to |
| <code>nextBoolean()</code>, <code>nextDouble()</code>, <code>nextInt()</code>, <code>nextLong()</code>, <code>nextString()</code> or <code>nextNull()</code>. Writers should make |
| exactly one call to one of <code>value()</code> or <code>nullValue()</code>. |
| For arrays, type adapters should start with a call to <code>beginArray()</code>, |
| convert all elements, and finish with a call to <code>endArray()</code>. For |
| objects, they should start with <code>beginObject()</code>, convert the object, |
| and finish with <code>endObject()</code>. Failing to convert a value or converting |
| too many values may cause the application to crash. |
| |
| <p>Type adapters should be prepared to read null from the stream and write it |
| to the stream. Alternatively, they should use <a href="../../../com/google/gson/TypeAdapter.html#nullSafe--"><code>nullSafe()</code></a> method while |
| registering the type adapter with Gson. If your <code>Gson</code> instance |
| has been configured to <a href="../../../com/google/gson/GsonBuilder.html#serializeNulls--"><code>GsonBuilder.serializeNulls()</code></a>, these nulls will be |
| written to the final document. Otherwise the value (and the corresponding name |
| when writing to a JSON object) will be omitted automatically. In either case |
| your type adapter must handle null. |
| |
| <p>To use a custom type adapter with Gson, you must <i>register</i> it with a |
| <a href="../../../com/google/gson/GsonBuilder.html" title="class in com.google.gson"><code>GsonBuilder</code></a>: <pre> <code> |
| |
| GsonBuilder builder = new GsonBuilder(); |
| builder.registerTypeAdapter(Point.class, new PointAdapter()); |
| // if PointAdapter didn't check for nulls in its read/write methods, you should instead use |
| // builder.registerTypeAdapter(Point.class, new PointAdapter().nullSafe()); |
| ... |
| Gson gson = builder.create(); |
| </code></pre></div> |
| <dl> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>2.1</dd> |
| </dl> |
| </li> |
| </ul> |
| </div> |
| <div class="summary"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <!-- ======== CONSTRUCTOR SUMMARY ======== --> |
| <ul class="blockList"> |
| <li class="blockList"><a name="constructor.summary"> |
| <!-- --> |
| </a> |
| <h3>Constructor Summary</h3> |
| <table class="memberSummary" border="0" cellpadding="3" cellspacing="0" summary="Constructor Summary table, listing constructors, and an explanation"> |
| <caption><span>Constructors</span><span class="tabEnd"> </span></caption> |
| <tr> |
| <th class="colOne" scope="col">Constructor and Description</th> |
| </tr> |
| <tr class="altColor"> |
| <td class="colOne"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#TypeAdapter--">TypeAdapter</a></span>()</code> </td> |
| </tr> |
| </table> |
| </li> |
| </ul> |
| <!-- ========== 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><span id="t4" class="tableTab"><span><a href="javascript:show(8);">Concrete 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/TypeAdapter.html" title="type parameter in TypeAdapter">T</a></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#fromJson-java.io.Reader-">fromJson</a></span>(<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Reader.html?is-external=true" title="class or interface in java.io">Reader</a> in)</code> |
| <div class="block">Converts the JSON document in <code>in</code> to a Java object.</div> |
| </td> |
| </tr> |
| <tr id="i1" class="rowColor"> |
| <td class="colFirst"><code><a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#fromJson-java.lang.String-">fromJson</a></span>(<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a> json)</code> |
| <div class="block">Converts the JSON document in <code>json</code> to a Java object.</div> |
| </td> |
| </tr> |
| <tr id="i2" class="altColor"> |
| <td class="colFirst"><code><a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#fromJsonTree-com.google.gson.JsonElement-">fromJsonTree</a></span>(<a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson">JsonElement</a> jsonTree)</code> |
| <div class="block">Converts <code>jsonTree</code> to a Java object.</div> |
| </td> |
| </tr> |
| <tr id="i3" class="rowColor"> |
| <td class="colFirst"><code><a href="../../../com/google/gson/TypeAdapter.html" title="class in com.google.gson">TypeAdapter</a><<a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a>></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#nullSafe--">nullSafe</a></span>()</code> |
| <div class="block">This wrapper method is used to make a type adapter null tolerant.</div> |
| </td> |
| </tr> |
| <tr id="i4" class="altColor"> |
| <td class="colFirst"><code>abstract <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#read-com.google.gson.stream.JsonReader-">read</a></span>(<a href="../../../com/google/gson/stream/JsonReader.html" title="class in com.google.gson.stream">JsonReader</a> in)</code> |
| <div class="block">Reads one JSON value (an array, object, string, number, boolean or null) |
| and converts it to a Java object.</div> |
| </td> |
| </tr> |
| <tr id="i5" class="rowColor"> |
| <td class="colFirst"><code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#toJson-T-">toJson</a></span>(<a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value)</code> |
| <div class="block">Converts <code>value</code> to a JSON document.</div> |
| </td> |
| </tr> |
| <tr id="i6" class="altColor"> |
| <td class="colFirst"><code>void</code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#toJson-java.io.Writer-T-">toJson</a></span>(<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Writer.html?is-external=true" title="class or interface in java.io">Writer</a> out, |
| <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value)</code> |
| <div class="block">Converts <code>value</code> to a JSON document and writes it to <code>out</code>.</div> |
| </td> |
| </tr> |
| <tr id="i7" class="rowColor"> |
| <td class="colFirst"><code><a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson">JsonElement</a></code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#toJsonTree-T-">toJsonTree</a></span>(<a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value)</code> |
| <div class="block">Converts <code>value</code> to a JSON tree.</div> |
| </td> |
| </tr> |
| <tr id="i8" class="altColor"> |
| <td class="colFirst"><code>abstract void</code></td> |
| <td class="colLast"><code><span class="memberNameLink"><a href="../../../com/google/gson/TypeAdapter.html#write-com.google.gson.stream.JsonWriter-T-">write</a></span>(<a href="../../../com/google/gson/stream/JsonWriter.html" title="class in com.google.gson.stream">JsonWriter</a> out, |
| <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value)</code> |
| <div class="block">Writes one JSON value (an array, object, string, number, boolean or null) |
| for <code>value</code>.</div> |
| </td> |
| </tr> |
| </table> |
| <ul class="blockList"> |
| <li class="blockList"><a name="methods.inherited.from.class.java.lang.Object"> |
| <!-- --> |
| </a> |
| <h3>Methods inherited from class java.lang.<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">Object</a></h3> |
| <code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#clone--" title="class or interface in java.lang">clone</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#equals-java.lang.Object-" title="class or interface in java.lang">equals</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#finalize--" title="class or interface in java.lang">finalize</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#getClass--" title="class or interface in java.lang">getClass</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#hashCode--" title="class or interface in java.lang">hashCode</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#notify--" title="class or interface in java.lang">notify</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#notifyAll--" title="class or interface in java.lang">notifyAll</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#toString--" title="class or interface in java.lang">toString</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#wait--" title="class or interface in java.lang">wait</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#wait-long-" title="class or interface in java.lang">wait</a>, <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html?is-external=true#wait-long-int-" title="class or interface in java.lang">wait</a></code></li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| <div class="details"> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <!-- ========= CONSTRUCTOR DETAIL ======== --> |
| <ul class="blockList"> |
| <li class="blockList"><a name="constructor.detail"> |
| <!-- --> |
| </a> |
| <h3>Constructor Detail</h3> |
| <a name="TypeAdapter--"> |
| <!-- --> |
| </a> |
| <ul class="blockListLast"> |
| <li class="blockList"> |
| <h4>TypeAdapter</h4> |
| <pre>public TypeAdapter()</pre> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| <!-- ============ METHOD DETAIL ========== --> |
| <ul class="blockList"> |
| <li class="blockList"><a name="method.detail"> |
| <!-- --> |
| </a> |
| <h3>Method Detail</h3> |
| <a name="write-com.google.gson.stream.JsonWriter-java.lang.Object-"> |
| <!-- --> |
| </a><a name="write-com.google.gson.stream.JsonWriter-T-"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>write</h4> |
| <pre>public abstract void write(<a href="../../../com/google/gson/stream/JsonWriter.html" title="class in com.google.gson.stream">JsonWriter</a> out, |
| <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value) |
| throws <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></pre> |
| <div class="block">Writes one JSON value (an array, object, string, number, boolean or null) |
| for <code>value</code>.</div> |
| <dl> |
| <dt><span class="paramLabel">Parameters:</span></dt> |
| <dd><code>value</code> - the Java object to write. May be null.</dd> |
| <dt><span class="throwsLabel">Throws:</span></dt> |
| <dd><code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></code></dd> |
| </dl> |
| </li> |
| </ul> |
| <a name="toJson-java.io.Writer-java.lang.Object-"> |
| <!-- --> |
| </a><a name="toJson-java.io.Writer-T-"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>toJson</h4> |
| <pre>public final void toJson(<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Writer.html?is-external=true" title="class or interface in java.io">Writer</a> out, |
| <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value) |
| throws <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></pre> |
| <div class="block">Converts <code>value</code> to a JSON document and writes it to <code>out</code>. |
| Unlike Gson's similar <a href="../../../com/google/gson/Gson.html#toJson-com.google.gson.JsonElement-java.lang.Appendable-"><code>toJson</code></a> |
| method, this write is strict. Create a <a href="../../../com/google/gson/stream/JsonWriter.html#setLenient-boolean-"><code>lenient</code></a> <code>JsonWriter</code> and call |
| <a href="../../../com/google/gson/TypeAdapter.html#write-com.google.gson.stream.JsonWriter-T-"><code>write(com.google.gson.stream.JsonWriter, Object)</code></a> for lenient |
| writing.</div> |
| <dl> |
| <dt><span class="paramLabel">Parameters:</span></dt> |
| <dd><code>value</code> - the Java object to convert. May be null.</dd> |
| <dt><span class="throwsLabel">Throws:</span></dt> |
| <dd><code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></code></dd> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>2.2</dd> |
| </dl> |
| </li> |
| </ul> |
| <a name="nullSafe--"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>nullSafe</h4> |
| <pre>public final <a href="../../../com/google/gson/TypeAdapter.html" title="class in com.google.gson">TypeAdapter</a><<a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a>> nullSafe()</pre> |
| <div class="block">This wrapper method is used to make a type adapter null tolerant. In general, a |
| type adapter is required to handle nulls in write and read methods. Here is how this |
| is typically done:<br> |
| <pre> <code> |
| |
| Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class, |
| new TypeAdapter<Foo>() { |
| public Foo read(JsonReader in) throws IOException { |
| if (in.peek() == JsonToken.NULL) { |
| in.nextNull(); |
| return null; |
| } |
| // read a Foo from in and return it |
| } |
| public void write(JsonWriter out, Foo src) throws IOException { |
| if (src == null) { |
| out.nullValue(); |
| return; |
| } |
| // write src as JSON to out |
| } |
| }).create(); |
| </code></pre> |
| You can avoid this boilerplate handling of nulls by wrapping your type adapter with |
| this method. Here is how we will rewrite the above example: |
| <pre> <code> |
| |
| Gson gson = new GsonBuilder().registerTypeAdapter(Foo.class, |
| new TypeAdapter<Foo>() { |
| public Foo read(JsonReader in) throws IOException { |
| // read a Foo from in and return it |
| } |
| public void write(JsonWriter out, Foo src) throws IOException { |
| // write src as JSON to out |
| } |
| }.nullSafe()).create(); |
| </code></pre> |
| Note that we didn't need to check for nulls in our type adapter after we used nullSafe.</div> |
| </li> |
| </ul> |
| <a name="toJson-java.lang.Object-"> |
| <!-- --> |
| </a><a name="toJson-T-"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>toJson</h4> |
| <pre>public final <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a> toJson(<a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value) |
| throws <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></pre> |
| <div class="block">Converts <code>value</code> to a JSON document. Unlike Gson's similar <a href="../../../com/google/gson/Gson.html#toJson-java.lang.Object-"><code>toJson</code></a> method, this write is strict. Create a <a href="../../../com/google/gson/stream/JsonWriter.html#setLenient-boolean-"><code>lenient</code></a> <code>JsonWriter</code> and call |
| <a href="../../../com/google/gson/TypeAdapter.html#write-com.google.gson.stream.JsonWriter-T-"><code>write(com.google.gson.stream.JsonWriter, Object)</code></a> for lenient |
| writing.</div> |
| <dl> |
| <dt><span class="paramLabel">Parameters:</span></dt> |
| <dd><code>value</code> - the Java object to convert. May be null.</dd> |
| <dt><span class="throwsLabel">Throws:</span></dt> |
| <dd><code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></code></dd> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>2.2</dd> |
| </dl> |
| </li> |
| </ul> |
| <a name="toJsonTree-java.lang.Object-"> |
| <!-- --> |
| </a><a name="toJsonTree-T-"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>toJsonTree</h4> |
| <pre>public final <a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson">JsonElement</a> toJsonTree(<a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> value)</pre> |
| <div class="block">Converts <code>value</code> to a JSON tree.</div> |
| <dl> |
| <dt><span class="paramLabel">Parameters:</span></dt> |
| <dd><code>value</code> - the Java object to convert. May be null.</dd> |
| <dt><span class="returnLabel">Returns:</span></dt> |
| <dd>the converted JSON tree. May be <a href="../../../com/google/gson/JsonNull.html" title="class in com.google.gson"><code>JsonNull</code></a>.</dd> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>2.2</dd> |
| </dl> |
| </li> |
| </ul> |
| <a name="read-com.google.gson.stream.JsonReader-"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>read</h4> |
| <pre>public abstract <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> read(<a href="../../../com/google/gson/stream/JsonReader.html" title="class in com.google.gson.stream">JsonReader</a> in) |
| throws <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></pre> |
| <div class="block">Reads one JSON value (an array, object, string, number, boolean or null) |
| and converts it to a Java object. Returns the converted object.</div> |
| <dl> |
| <dt><span class="returnLabel">Returns:</span></dt> |
| <dd>the converted Java object. May be null.</dd> |
| <dt><span class="throwsLabel">Throws:</span></dt> |
| <dd><code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></code></dd> |
| </dl> |
| </li> |
| </ul> |
| <a name="fromJson-java.io.Reader-"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>fromJson</h4> |
| <pre>public final <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> fromJson(<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Reader.html?is-external=true" title="class or interface in java.io">Reader</a> in) |
| throws <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></pre> |
| <div class="block">Converts the JSON document in <code>in</code> to a Java object. Unlike Gson's |
| similar <a href="../../../com/google/gson/Gson.html#fromJson-java.io.Reader-java.lang.Class-"><code>fromJson</code></a> method, this |
| read is strict. Create a <a href="../../../com/google/gson/stream/JsonReader.html#setLenient-boolean-"><code>lenient</code></a> |
| <code>JsonReader</code> and call <a href="../../../com/google/gson/TypeAdapter.html#read-com.google.gson.stream.JsonReader-"><code>read(JsonReader)</code></a> for lenient reading.</div> |
| <dl> |
| <dt><span class="returnLabel">Returns:</span></dt> |
| <dd>the converted Java object. May be null.</dd> |
| <dt><span class="throwsLabel">Throws:</span></dt> |
| <dd><code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></code></dd> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>2.2</dd> |
| </dl> |
| </li> |
| </ul> |
| <a name="fromJson-java.lang.String-"> |
| <!-- --> |
| </a> |
| <ul class="blockList"> |
| <li class="blockList"> |
| <h4>fromJson</h4> |
| <pre>public final <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> fromJson(<a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html?is-external=true" title="class or interface in java.lang">String</a> json) |
| throws <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></pre> |
| <div class="block">Converts the JSON document in <code>json</code> to a Java object. Unlike Gson's |
| similar <a href="../../../com/google/gson/Gson.html#fromJson-java.lang.String-java.lang.Class-"><code>fromJson</code></a> method, this read is |
| strict. Create a <a href="../../../com/google/gson/stream/JsonReader.html#setLenient-boolean-"><code>lenient</code></a> <code>JsonReader</code> and call <a href="../../../com/google/gson/TypeAdapter.html#read-com.google.gson.stream.JsonReader-"><code>read(JsonReader)</code></a> for lenient reading.</div> |
| <dl> |
| <dt><span class="returnLabel">Returns:</span></dt> |
| <dd>the converted Java object. May be null.</dd> |
| <dt><span class="throwsLabel">Throws:</span></dt> |
| <dd><code><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/IOException.html?is-external=true" title="class or interface in java.io">IOException</a></code></dd> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>2.2</dd> |
| </dl> |
| </li> |
| </ul> |
| <a name="fromJsonTree-com.google.gson.JsonElement-"> |
| <!-- --> |
| </a> |
| <ul class="blockListLast"> |
| <li class="blockList"> |
| <h4>fromJsonTree</h4> |
| <pre>public final <a href="../../../com/google/gson/TypeAdapter.html" title="type parameter in TypeAdapter">T</a> fromJsonTree(<a href="../../../com/google/gson/JsonElement.html" title="class in com.google.gson">JsonElement</a> jsonTree)</pre> |
| <div class="block">Converts <code>jsonTree</code> to a Java object.</div> |
| <dl> |
| <dt><span class="paramLabel">Parameters:</span></dt> |
| <dd><code>jsonTree</code> - the Java object to convert. May be <a href="../../../com/google/gson/JsonNull.html" title="class in com.google.gson"><code>JsonNull</code></a>.</dd> |
| <dt><span class="simpleTagLabel">Since:</span></dt> |
| <dd>2.2</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/TypeAdapter.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/LongSerializationPolicy.html" title="enum in com.google.gson"><span class="typeNameLink">Prev Class</span></a></li> |
| <li><a href="../../../com/google/gson/TypeAdapterFactory.html" title="interface in com.google.gson"><span class="typeNameLink">Next Class</span></a></li> |
| </ul> |
| <ul class="navList"> |
| <li><a href="../../../index.html?com/google/gson/TypeAdapter.html" target="_top">Frames</a></li> |
| <li><a href="TypeAdapter.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><a href="#constructor.summary">Constr</a> | </li> |
| <li><a href="#method.summary">Method</a></li> |
| </ul> |
| <ul class="subNavList"> |
| <li>Detail: </li> |
| <li>Field | </li> |
| <li><a href="#constructor.detail">Constr</a> | </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> |