-
@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
Gsonwith aGsonBuilderand 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 thepasswordfield along-withfirstName,lastName, andemailAddressfor serialization and deserialization. However, if you created Gson withGson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()then thetoJson()andfromJson()methods of Gson will exclude thepasswordfield. This is because thepasswordfield is not marked with the@Exposeannotation. Gson will also excludelastNameandemailAddressfrom serialization sinceserializeis set tofalse. Similarly, Gson will excludeemailAddressfrom deserialization sincedeserializeis set to false.Note that another way to achieve the same effect would have been to just mark the
passwordfield astransient, and Gson would have excluded it even with default settings. The@Exposeannotation is useful in a style of programming where you want to explicitly specify all fields that should get considered for serialization or deserialization.- Author:
- Inderjeet Singh, Joel Leitch
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description booleandeserializeIftrue, the field marked with this annotation is deserialized from the JSON.booleanserializeIftrue, the field marked with this annotation is written out in the JSON while serializing.
-