com.google.gson.annotations

Annotation Type Expose



  • @Documented
     @Retention(value=RUNTIME)
     @Target(value=FIELD)
    public @interface Expose
    An annotation that indicates this member should be exposed for JSON serialization or deserialization.

    This annotation has no effect unless you build Gson with a GsonBuilder and invoke GsonBuilder.excludeFieldsWithoutExposeAnnotation() method.

    Here is an example of how this annotation is meant to be used:

     public class User {
       &#64Expose private String firstName;
       &#64Expose(serialize = false) private String lastName;
       &#64Expose (serialize = false, deserialize = false) private String emailAddress;
       private String password;
     }
     </pre></p>
     If you created Gson with <code>new Gson()</code>, the <code>toJson()</code> and <code>fromJson()</code>
     methods will use the <code>password</code> field along-with <code>firstName</code>, <code>lastName</code>,
     and <code>emailAddress</code> for serialization and deserialization. However, if you created Gson
     with <code>Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()</code>
     then the <code>toJson()</code> and <code>fromJson()</code> methods of Gson will exclude the
     <code>password</code> field. This is because the <code>password</code> field is not marked with the
     <code>@Expose</code> annotation. Gson will also exclude <code>lastName</code> and <code>emailAddress</code>
     from serialization since <code>serialize</code> is set to <code>false</code>. Similarly, Gson will
     exclude <code>emailAddress</code> from deserialization since <code>deserialize</code> is set to false.
    
     <p>Note that another way to achieve the same effect would have been to just mark the
     <code>password</code> field as <code>transient</code>, and Gson would have excluded it even with default
     settings. The <code>@Expose</code> annotation is useful in a style of programming where you want to
     explicitly specify all fields that should get considered for serialization or deserialization.</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">
    <!-- =========== ANNOTATION TYPE OPTIONAL MEMBER SUMMARY =========== -->
    <ul class="blockList">
    <li class="blockList"><a name="annotation.type.optional.element.summary">
    <!--   -->
    </a>
    <h3>Optional Element Summary</h3>
    <table class="memberSummary" border="0" cellpadding="3" cellspacing="0" summary="Optional Element Summary table, listing optional elements, and an explanation">
    <caption><span>Optional Elements</span><span class="tabEnd">&nbsp;</span></caption>
    <tr>
    <th class="colFirst" scope="col">Modifier and Type</th>
    <th class="colLast" scope="col">Optional Element and Description</th>
    </tr>
    <tr class="altColor">
    <td class="colFirst"><code>boolean</code></td>
    <td class="colLast"><code><span class="memberNameLink"><a href="../../../../com/google/gson/annotations/Expose.html#deserialize--">deserialize</a></span></code>
    <div class="block">If <code>true</code>, the field marked with this annotation is deserialized from the JSON.</div>
    </td>
    </tr>
    <tr class="rowColor">
    <td class="colFirst"><code>boolean</code></td>
    <td class="colLast"><code><span class="memberNameLink"><a href="../../../../com/google/gson/annotations/Expose.html#serialize--">serialize</a></span></code>
    <div class="block">If <code>true</code>, the field marked with this annotation is written out in the JSON while
     serializing.</div>
    </td>
    </tr>
    </table>
    </li>
    </ul>
    </li>
    </ul>
    </div>
    <div class="details">
    <ul class="blockList">
    <li class="blockList">
    <!-- ============ ANNOTATION TYPE MEMBER DETAIL =========== -->
    <ul class="blockList">
    <li class="blockList"><a name="annotation.type.element.detail">
    <!--   -->
    </a>
    <h3>Element Detail</h3>
    <a name="serialize--">
    <!--   -->
    </a>
    <ul class="blockList">
    <li class="blockList">
    <h4>serialize</h4>
    <pre>public abstract&nbsp;boolean&nbsp;serialize</pre>
    <div class="block">If <code>true</code>, the field marked with this annotation is written out in the JSON while
     serializing. If <code>false</code>, the field marked with this annotation is skipped from the
     serialized output. Defaults to <code>true</code>.</div>
    <dl>
    <dt><span class="simpleTagLabel">Since:</span></dt>
    <dd>1.4</dd>
    </dl>
    <dl>
    <dt>Default:</dt>
    <dd>true</dd>
    </dl>
    </li>
    </ul>
    </li>
    </ul>
    <ul class="blockList">
    <li class="blockList"><a name="deserialize--">
    <!--   -->
    </a>
    <ul class="blockListLast">
    <li class="blockList">
    <h4>deserialize</h4>
    <pre>public abstract&nbsp;boolean&nbsp;deserialize</pre>
    <div class="block">If <code>true</code>, the field marked with this annotation is deserialized from the JSON.
     If <code>false</code>, the field marked with this annotation is skipped during deserialization. 
     Defaults to <code>true</code>.</div>
    <dl>
    <dt><span class="simpleTagLabel">Since:</span></dt>
    <dd>1.4</dd>
    </dl>
    <dl>
    <dt>Default:</dt>
    <dd>true</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/Expose.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>Prev&nbsp;Class</li>
    <li><a href="../../../../com/google/gson/annotations/JsonAdapter.html" title="annotation in com.google.gson.annotations"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
    </ul>
    <ul class="navList">
    <li><a href="../../../../index.html?com/google/gson/annotations/Expose.html" target="_top">Frames</a></li>
    <li><a href="Expose.html" target="_top">No&nbsp;Frames</a></li>
    </ul>
    <ul class="navList" id="allclasses_navbar_bottom">
    <li><a href="../../../../allclasses-noframe.html">All&nbsp;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:&nbsp;</li>
    <li>Field&nbsp;|&nbsp;</li>
    <li>Required&nbsp;|&nbsp;</li>
    <li><a href="#annotation.type.optional.element.summary">Optional</a></li>
    </ul>
    <ul class="subNavList">
    <li>Detail:&nbsp;</li>
    <li>Field&nbsp;|&nbsp;</li>
    <li><a href="#annotation.type.element.detail">Element</a></li>
    </ul>
    </div>
    <a name="skip.navbar.bottom">
    <!--   -->
    </a></div>
    <!-- ======== END OF BOTTOM NAVBAR ======= -->
    <p class="legalCopy"><small>Copyright &#169; 2018. All Rights Reserved.</small></p>
    </body>
    </html>