Top Description Methods
com.google.gson.annotations

public @Interface Expose

extends Annotation
Annotations
@Documented
@Retention:RUNTIME
@Target:FIELD
Imports
java.lang.annotation.Documented, .ElementType, .Retention, .RetentionPolicy, .Target

An annotation that indicates this member should be exposed for JSON serialization or deserialization.

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

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

public class User {
  @Expose private String firstName;
  @Expose(serialize = false) private String lastName;
  @Expose (serialize = false, deserialize = false) private String emailAddress;
  private String password;
}
If you created Gson with new Gson(), the toJson() and fromJson() methods will use the password field along-with firstName, lastName, and emailAddress for serialization and deserialization. However, if you created Gson with Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() then the toJson() and fromJson() methods of Gson will exclude the password field. This is because the password field is not marked with the @Expose annotation. Gson will also exclude lastName and emailAddress from serialization since serialize is set to false. Similarly, Gson will exclude emailAddress from deserialization since deserialize is set to false.

Note that another way to achieve the same effect would have been to just mark the password field as transient, and Gson would have excluded it even with default settings. The @Expose annotation is useful in a style of programming where you want to explicitly specify all fields that should get considered for serialization or deserialization.

Authors
Inderjeet Singh, Joel Leitch

Method Summary

Modifier and TypeMethod and Description
public boolean
deserialize()

If true, the field marked with this annotation is deserialized from the JSON.

public boolean
serialize()

If true, the field marked with this annotation is written out in the JSON while serializing.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

deserializeback to summary
public boolean deserialize()

If true, the field marked with this annotation is deserialized from the JSON. If false, the field marked with this annotation is skipped during deserialization. Defaults to true.

Since
1.4
serializeback to summary
public boolean serialize()

If true, the field marked with this annotation is written out in the JSON while serializing. If false, the field marked with this annotation is skipped from the serialized output. Defaults to true.

Since
1.4