2010-11-25 00:41:30 +01:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- NewPage -->
< HTML >
< HEAD >
2011-04-13 22:41:54 +02:00
<!-- Generated by javadoc (build 1.6.0_24) on Wed Apr 13 13:26:44 PDT 2011 -->
2010-11-25 00:41:30 +01:00
< META http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< TITLE >
2011-04-13 22:41:54 +02:00
ExclusionStrategy (Gson 1.7.1 API)
2010-11-25 00:41:30 +01:00
< / TITLE >
2011-04-13 22:41:54 +02:00
< META NAME = "date" CONTENT = "2011-04-13" >
2010-11-25 00:41:30 +01:00
< LINK REL = "stylesheet" TYPE = "text/css" HREF = "../../../stylesheet.css" TITLE = "Style" >
< SCRIPT type = "text/javascript" >
function windowTitle()
{
2011-04-13 22:41:54 +02:00
if (location.href.indexOf('is-external=true') == -1) {
parent.document.title="ExclusionStrategy (Gson 1.7.1 API)";
}
2010-11-25 00:41:30 +01:00
}
< / SCRIPT >
< NOSCRIPT >
< / NOSCRIPT >
< / HEAD >
< BODY BGCOLOR = "white" onload = "windowTitle();" >
2011-04-13 22:41:54 +02:00
< HR >
2010-11-25 00:41:30 +01:00
<!-- ========= START OF TOP NAVBAR ======= -->
< A NAME = "navbar_top" > <!-- --> < / A >
< A HREF = "#skip-navbar_top" title = "Skip navigation links" > < / A >
< TABLE BORDER = "0" WIDTH = "100%" CELLPADDING = "1" CELLSPACING = "0" SUMMARY = "" >
< TR >
< TD COLSPAN = 2 BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" >
< A NAME = "navbar_top_firstrow" > <!-- --> < / A >
< TABLE BORDER = "0" CELLPADDING = "0" CELLSPACING = "3" SUMMARY = "" >
< TR ALIGN = "center" VALIGN = "top" >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../overview-summary.html" > < FONT CLASS = "NavBarFont1" > < B > Overview< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "package-summary.html" > < FONT CLASS = "NavBarFont1" > < B > Package< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#FFFFFF" CLASS = "NavBarCell1Rev" > < FONT CLASS = "NavBarFont1Rev" > < B > Class< / B > < / FONT > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "class-use/ExclusionStrategy.html" > < FONT CLASS = "NavBarFont1" > < B > Use< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "package-tree.html" > < FONT CLASS = "NavBarFont1" > < B > Tree< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../deprecated-list.html" > < FONT CLASS = "NavBarFont1" > < B > Deprecated< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../index-all.html" > < FONT CLASS = "NavBarFont1" > < B > Index< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../help-doc.html" > < FONT CLASS = "NavBarFont1" > < B > Help< / B > < / FONT > < / A > < / TD >
< / TR >
< / TABLE >
< / TD >
< TD ALIGN = "right" VALIGN = "top" ROWSPAN = 3 > < EM >
< / EM >
< / TD >
< / TR >
< TR >
< TD BGCOLOR = "white" CLASS = "NavBarCell2" > < FONT SIZE = "-2" >
PREV CLASS
< A HREF = "../../../com/google/gson/FieldAttributes.html" title = "class in com.google.gson" > < B > NEXT CLASS< / B > < / A > < / FONT > < / TD >
< TD BGCOLOR = "white" CLASS = "NavBarCell2" > < FONT SIZE = "-2" >
< A HREF = "../../../index.html?com/google/gson/ExclusionStrategy.html" target = "_top" > < B > FRAMES< / B > < / A >
< A HREF = "ExclusionStrategy.html" target = "_top" > < B > NO FRAMES< / B > < / A >
< SCRIPT type = "text/javascript" >
<!--
if(window==top) {
document.writeln('< A HREF = "../../../allclasses-noframe.html" > < B > All Classes< / B > < / A > ');
}
//-->
< / SCRIPT >
< NOSCRIPT >
< A HREF = "../../../allclasses-noframe.html" > < B > All Classes< / B > < / A >
< / NOSCRIPT >
< / FONT > < / TD >
< / TR >
< TR >
< TD VALIGN = "top" CLASS = "NavBarCell3" > < FONT SIZE = "-2" >
SUMMARY: NESTED | FIELD | CONSTR | < A HREF = "#method_summary" > METHOD< / A > < / FONT > < / TD >
< TD VALIGN = "top" CLASS = "NavBarCell3" > < FONT SIZE = "-2" >
DETAIL: FIELD | CONSTR | < A HREF = "#method_detail" > METHOD< / A > < / FONT > < / TD >
< / TR >
< / TABLE >
< A NAME = "skip-navbar_top" > < / A >
<!-- ========= END OF TOP NAVBAR ========= -->
< HR >
<!-- ======== START OF CLASS DATA ======== -->
< H2 >
< FONT SIZE = "-1" >
com.google.gson< / FONT >
< BR >
Interface ExclusionStrategy< / H2 >
< HR >
< DL >
< DT > < PRE > public interface < B > ExclusionStrategy< / B > < / DL >
< / PRE >
< P >
A strategy (or policy) definition that is used to decide whether or not a field or top-level
class should be serialized or deserialized as part of the JSON output/input. For serialization,
if the < A HREF = "../../../com/google/gson/ExclusionStrategy.html#shouldSkipClass(java.lang.Class)" > < CODE > shouldSkipClass(Class)< / CODE > < / A > method returns false then that class or field type
will not be part of the JSON output. For deserialization, if < A HREF = "../../../com/google/gson/ExclusionStrategy.html#shouldSkipClass(java.lang.Class)" > < CODE > shouldSkipClass(Class)< / CODE > < / A >
returns false, then it will not be set as part of the Java object structure.
< p > The following are a few examples that shows how you can use this exclusion mechanism.
< p > < strong > Exclude fields and objects based on a particular class type:< / strong >
< pre class = "code" >
private static class SpecificClassExclusionStrategy implements ExclusionStrategy {
private final Class< ?> excludedThisClass;
public SpecificClassExclusionStrategy(Class< ?> excludedThisClass) {
this.excludedThisClass = excludedThisClass;
}
public boolean shouldSkipClass(Class< ?> clazz) {
return excludedThisClass.equals(clazz);
}
public boolean shouldSkipField(FieldAttributes f) {
return excludedThisClass.equals(f.getDeclaredClass());
}
}
< / pre >
< p > < strong > Excludes fields and objects based on a particular annotation:< / strong >
< pre class = "code" >
public & #64interface FooAnnotation {
// some implementation here
}
// Excludes any field (or class) that is tagged with an "& #64FooAnnotation"
private static class FooAnnotationExclusionStrategy implements ExclusionStrategy {
public boolean shouldSkipClass(Class< ?> clazz) {
return clazz.getAnnotation(FooAnnotation.class) != null;
}
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(FooAnnotation.class) != null;
}
}
< / pre >
< p > Now if you want to configure < code > Gson< / code > to use a user defined exclusion strategy, then
the < code > GsonBuilder< / code > is required. The following is an example of how you can use the
< code > GsonBuilder< / code > to configure Gson to use one of the above sample:
< pre class = "code" >
ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
Gson gson = new GsonBuilder()
.setExclusionStrategies(excludeStrings)
.create();
< / pre >
2011-04-13 01:05:52 +02:00
< p > For certain model classes, you may only want to serialize a field, but exclude it for
deserialization. To do that, you can write an < code > ExclusionStrategy< / code > as per normal;
however, you would register it with the
< A HREF = "../../../com/google/gson/GsonBuilder.html#addDeserializationExclusionStrategy(com.google.gson.ExclusionStrategy)" > < CODE > GsonBuilder.addDeserializationExclusionStrategy(ExclusionStrategy)< / CODE > < / A > method.
For example:
< pre class = "code" >
ExclusionStrategy excludeStrings = new UserDefinedExclusionStrategy(String.class);
Gson gson = new GsonBuilder()
.addDeserializationExclusionStrategy(excludeStrings)
.create();
< / pre >
2010-11-25 00:41:30 +01:00
< P >
< P >
< DL >
< DT > < B > Since:< / B > < / DT >
< DD > 1.4< / DD >
< DT > < B > Author:< / B > < / DT >
< DD > Inderjeet Singh, Joel Leitch< / DD >
2011-04-13 01:05:52 +02:00
< DT > < B > See Also:< / B > < DD > < A HREF = "../../../com/google/gson/GsonBuilder.html#setExclusionStrategies(com.google.gson.ExclusionStrategy...)" > < CODE > GsonBuilder.setExclusionStrategies(ExclusionStrategy...)< / CODE > < / A > ,
< A HREF = "../../../com/google/gson/GsonBuilder.html#addDeserializationExclusionStrategy(com.google.gson.ExclusionStrategy)" > < CODE > GsonBuilder.addDeserializationExclusionStrategy(ExclusionStrategy)< / CODE > < / A > ,
< A HREF = "../../../com/google/gson/GsonBuilder.html#addSerializationExclusionStrategy(com.google.gson.ExclusionStrategy)" > < CODE > GsonBuilder.addSerializationExclusionStrategy(ExclusionStrategy)< / CODE > < / A > < / DL >
2010-11-25 00:41:30 +01:00
< HR >
< P >
<!-- ========== METHOD SUMMARY =========== -->
< A NAME = "method_summary" > <!-- --> < / A >
< TABLE BORDER = "1" WIDTH = "100%" CELLPADDING = "3" CELLSPACING = "0" SUMMARY = "" >
< TR BGCOLOR = "#CCCCFF" CLASS = "TableHeadingColor" >
< TH ALIGN = "left" COLSPAN = "2" > < FONT SIZE = "+2" >
< B > Method Summary< / B > < / FONT > < / TH >
< / TR >
< TR BGCOLOR = "white" CLASS = "TableRowColor" >
< TD ALIGN = "right" VALIGN = "top" WIDTH = "1%" > < FONT SIZE = "-1" >
< CODE > boolean< / CODE > < / FONT > < / TD >
2011-04-13 22:41:54 +02:00
< TD > < CODE > < B > < A HREF = "../../../com/google/gson/ExclusionStrategy.html#shouldSkipClass(java.lang.Class)" > shouldSkipClass< / A > < / B > (< A HREF = "http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html?is-external=true" title = "class or interface in java.lang" > Class< / A > < ?> clazz)< / CODE >
2010-11-25 00:41:30 +01:00
< BR >
< / TD >
< / TR >
< TR BGCOLOR = "white" CLASS = "TableRowColor" >
< TD ALIGN = "right" VALIGN = "top" WIDTH = "1%" > < FONT SIZE = "-1" >
< CODE > boolean< / CODE > < / FONT > < / TD >
< TD > < CODE > < B > < A HREF = "../../../com/google/gson/ExclusionStrategy.html#shouldSkipField(com.google.gson.FieldAttributes)" > shouldSkipField< / A > < / B > (< A HREF = "../../../com/google/gson/FieldAttributes.html" title = "class in com.google.gson" > FieldAttributes< / A > f)< / CODE >
< BR >
< / TD >
< / TR >
< / TABLE >
< P >
<!-- ============ METHOD DETAIL ========== -->
< A NAME = "method_detail" > <!-- --> < / A >
< TABLE BORDER = "1" WIDTH = "100%" CELLPADDING = "3" CELLSPACING = "0" SUMMARY = "" >
< TR BGCOLOR = "#CCCCFF" CLASS = "TableHeadingColor" >
< TH ALIGN = "left" COLSPAN = "1" > < FONT SIZE = "+2" >
< B > Method Detail< / B > < / FONT > < / TH >
< / TR >
< / TABLE >
< A NAME = "shouldSkipField(com.google.gson.FieldAttributes)" > <!-- --> < / A > < H3 >
shouldSkipField< / H3 >
< PRE >
boolean < B > shouldSkipField< / B > (< A HREF = "../../../com/google/gson/FieldAttributes.html" title = "class in com.google.gson" > FieldAttributes< / A > f)< / PRE >
< DL >
< DD > < DL >
< DT > < B > Parameters:< / B > < DD > < CODE > f< / CODE > - the field object that is under test
< DT > < B > Returns:< / B > < DD > true if the field should be ignored; otherwise false< / DL >
< / DD >
< / DL >
< HR >
< A NAME = "shouldSkipClass(java.lang.Class)" > <!-- --> < / A > < H3 >
shouldSkipClass< / H3 >
< PRE >
2011-04-13 22:41:54 +02:00
boolean < B > shouldSkipClass< / B > (< A HREF = "http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html?is-external=true" title = "class or interface in java.lang" > Class< / A > < ?> clazz)< / PRE >
2010-11-25 00:41:30 +01:00
< DL >
< DD > < DL >
< DT > < B > Parameters:< / B > < DD > < CODE > clazz< / CODE > - the class object that is under test
< DT > < B > Returns:< / B > < DD > true if the class should be ignored; otherwise false< / DL >
< / DD >
< / DL >
<!-- ========= END OF CLASS DATA ========= -->
< HR >
<!-- ======= START OF BOTTOM NAVBAR ====== -->
< A NAME = "navbar_bottom" > <!-- --> < / A >
< A HREF = "#skip-navbar_bottom" title = "Skip navigation links" > < / A >
< TABLE BORDER = "0" WIDTH = "100%" CELLPADDING = "1" CELLSPACING = "0" SUMMARY = "" >
< TR >
< TD COLSPAN = 2 BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" >
< A NAME = "navbar_bottom_firstrow" > <!-- --> < / A >
< TABLE BORDER = "0" CELLPADDING = "0" CELLSPACING = "3" SUMMARY = "" >
< TR ALIGN = "center" VALIGN = "top" >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../overview-summary.html" > < FONT CLASS = "NavBarFont1" > < B > Overview< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "package-summary.html" > < FONT CLASS = "NavBarFont1" > < B > Package< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#FFFFFF" CLASS = "NavBarCell1Rev" > < FONT CLASS = "NavBarFont1Rev" > < B > Class< / B > < / FONT > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "class-use/ExclusionStrategy.html" > < FONT CLASS = "NavBarFont1" > < B > Use< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "package-tree.html" > < FONT CLASS = "NavBarFont1" > < B > Tree< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../deprecated-list.html" > < FONT CLASS = "NavBarFont1" > < B > Deprecated< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../index-all.html" > < FONT CLASS = "NavBarFont1" > < B > Index< / B > < / FONT > < / A > < / TD >
< TD BGCOLOR = "#EEEEFF" CLASS = "NavBarCell1" > < A HREF = "../../../help-doc.html" > < FONT CLASS = "NavBarFont1" > < B > Help< / B > < / FONT > < / A > < / TD >
< / TR >
< / TABLE >
< / TD >
< TD ALIGN = "right" VALIGN = "top" ROWSPAN = 3 > < EM >
< / EM >
< / TD >
< / TR >
< TR >
< TD BGCOLOR = "white" CLASS = "NavBarCell2" > < FONT SIZE = "-2" >
PREV CLASS
< A HREF = "../../../com/google/gson/FieldAttributes.html" title = "class in com.google.gson" > < B > NEXT CLASS< / B > < / A > < / FONT > < / TD >
< TD BGCOLOR = "white" CLASS = "NavBarCell2" > < FONT SIZE = "-2" >
< A HREF = "../../../index.html?com/google/gson/ExclusionStrategy.html" target = "_top" > < B > FRAMES< / B > < / A >
< A HREF = "ExclusionStrategy.html" target = "_top" > < B > NO FRAMES< / B > < / A >
< SCRIPT type = "text/javascript" >
<!--
if(window==top) {
document.writeln('< A HREF = "../../../allclasses-noframe.html" > < B > All Classes< / B > < / A > ');
}
//-->
< / SCRIPT >
< NOSCRIPT >
< A HREF = "../../../allclasses-noframe.html" > < B > All Classes< / B > < / A >
< / NOSCRIPT >
< / FONT > < / TD >
< / TR >
< TR >
< TD VALIGN = "top" CLASS = "NavBarCell3" > < FONT SIZE = "-2" >
SUMMARY: NESTED | FIELD | CONSTR | < A HREF = "#method_summary" > METHOD< / A > < / FONT > < / TD >
< TD VALIGN = "top" CLASS = "NavBarCell3" > < FONT SIZE = "-2" >
DETAIL: FIELD | CONSTR | < A HREF = "#method_detail" > METHOD< / A > < / FONT > < / TD >
< / TR >
< / TABLE >
< A NAME = "skip-navbar_bottom" > < / A >
<!-- ======== END OF BOTTOM NAVBAR ======= -->
< HR >
2011-04-13 01:05:52 +02:00
Copyright © 2008-2011 < a href = "http://www.google.com" > Google, Inc.< / a > . All Rights Reserved.
2010-11-25 00:41:30 +01:00
< / BODY >
< / HTML >