gson. annotations Description. This package provides annotations that can be used with Gson .
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
@Expose is used to allow or disallow serialization and deserialization . @ Expose is optional and it has two configuration parameters: serialize and deserialize . By default they're set to true. To serialize and deserialize with @Expose we create gson object like this. Gson gsonBuilder = new GsonBuilder().
Using @SerializedName you are actually telling the Parser when receiving a callback from the server i.e. of a Json format: { "name":"John Doe", } that when Serializing or Deserializing an object to instead of searching for a key named: "userName", in the Json response, to search for "name".
Java class example,
public class Person {
@SerializedName("name")
private String personName;
@SerializedName("bd")
private String birthDate;
}
This class has two fields that represent the person name and birth date of a person. These fields are annotated with the @SerializedName annotation. The parameter (value) of this annotation is the name to be used when serialising
and deserialising
objects. For example, the Java field personName is represented as name in JSON.
JSON Example,
{
"name":"chintan",
"bd":"01-01-1990"
}
There are already few answers here,but I would like to add that if you are using ProGuard
to Obfuscate your code & don't use @SerializedName("name")
in your model class, then your GSON won't work. Because due to obfuscation, your variable names might have changed from String name
to String a
resulting into broken GSON parsing as GSON will look for key a
into json & it will fail.
By specifying @SerializedName
, GSON will not look in json based on variable name & will just use specified @SerializedName
.
Of Course you can tell proguard to not obfuscate your model, but if you would like to have model obfuscated, then you must specify @SerializedName
Using @SerializedName you are actually telling the Parser when receiving a callback from the server i.e. of a Json format:
{
"name":"John Doe",
}
that when Serializing or Deserializing an object to instead of searching for a key named: "userName", in the Json response, to search for "name".
@SerializedName("name")
var userName: String,
This is good because you may have a model that you would like it to have its members being called with whatever you like.
You can instruct Proguard to not obfuscate your data classes by specifying @Keep on top of the class. This will neither remove nor obfuscate your class. No need to add @SerializedName to each and every field explicitly if the field name is similar to the Json key being used for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With