135

What is the basic purpose of @SerializedName annotation in Android using Gson?

Give me some different examples. I can't understand the main purpose of using it.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Muhammad Ali
  • 1,377
  • 2
  • 9
  • 7

5 Answers5

306

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"
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • 3
    @MuhammadAli its my pleasure to help you. You can also give me credit by upvote and accept answer my dear :) – Chintan Rathod Mar 16 '15 at 05:39
  • 6
    Negative voters, please add your comment so that I can understand what is purpose of down vote and I can improve my answer onwards.. :) – Chintan Rathod Mar 09 '16 at 14:13
  • 4
    Good answer .... Right to the point .... Example with `Json` was helpful – Devrath Nov 07 '16 at 15:05
  • Glad that it helped. :) – Chintan Rathod Sep 29 '17 at 06:29
  • few more info can be found from [here](https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html). – CoDe Oct 08 '17 at 07:02
  • @MuhammadAli if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Sufian Dec 07 '18 at 12:07
80

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

Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47
  • 1
    Gr8 info, I used to tell proguard to not obfuscate the models. Now, with this I can do. :) – Akash Patra Aug 21 '18 at 09:43
  • 1
    This is very very important. Lost 2 hours and have added @SerializedName every where. – Abhijit Kurane Mar 04 '20 at 14:10
  • Suppose my API endpoint response has multiple index like `data` { status : true, message : success, data :{name:demo,url: http://mysiet.com} } , Can I use SerializedName in model like `@SerializedName("data.name")` for getting name value – Ajith Sep 20 '20 at 07:37
  • @Ajith Nope that's not possible. You'll need to create a separate class for parsing `name` and `url` – Sandip Fichadiya Sep 20 '20 at 13:52
  • I tried that too like but when I tried it doesn't work . Canln you send an example please? – Ajith Sep 20 '20 at 16:05
9

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.

Dimitri Leite
  • 1,791
  • 13
  • 16
5

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.

Alankar Gupta
  • 71
  • 1
  • 6
2

Let's say in a real-world scenario, your backend dev is giving you this response for an API request you make

{
 "name":"John Doe",
 "id":"1478"
}

Now, in the data class you make to handle this, there might be chances you want to specify a different variable name at Android side for the fields "name" and "id" that you are getting from backend.

@SerializedName comes to rescue here.

You just need to specify the actual key value you will be getting from backend in the @SerializedName (which will be used to serialize and deserialize) and then you can use a variable name of your choice that stores that value received from the operation.

For example, for the JSON I mentioned earlier, here is how its data class will look like:

data class User(
    @SerializedName("name") val userName: String,
    @SerializedName("id") val userId: Int
)

Here name, id is used in @SerializedName because it's the backend key. But I have used userName, userId to store those values.