Package com.google.gson.annotations
Annotation Type Expose
-
@Documented @Retention(RUNTIME) @Target(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 aGsonBuilder
and invokeGsonBuilder.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 withnew Gson()
, thetoJson()
andfromJson()
methods will use thepassword
field along-withfirstName
,lastName
, andemailAddress
for serialization and deserialization. However, if you created Gson withGson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()
then thetoJson()
andfromJson()
methods of Gson will exclude thepassword
field. This is because thepassword
field is not marked with the@Expose
annotation. Gson will also excludelastName
andemailAddress
from serialization sinceserialize
is set tofalse
. Similarly, Gson will excludeemailAddress
from deserialization sincedeserialize
is set to false.Note that another way to achieve the same effect would have been to just mark the
password
field astransient
, 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.
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description boolean
deserialize
Iftrue
, the field marked with this annotation is deserialized from the JSON.boolean
serialize
Iftrue
, the field marked with this annotation is written out in the JSON while serializing.
-