Gson @SerializedName Annotation (GSA)


GSA:   SOLD       




Hi dev, do any of you understand some of the annotations contained in the Gson library? One of which is  @SerializedName.

At that time, I had quite a hard time understanding it before someone could explain it while giving examples in real case studies.

After increasing my flying hours to be involved in various Android application development cases, I finally understood the purpose of making these annotations.

Naming Convention

Yup, friends must know about naming standards in the Java environment, in general in the programming world that adopts the OOP paradigm, it certainly requires these naming standards, although in practice, each platform has its own standards.

For example, if you are a .NET developer, then it is natural for you to name each Class or Variable as  PascalCase. On the other hand, if you are a mobile application developer such as Android, for example, then it is natural for the standard you use to be  camelCase. Meanwhile, if you are a php web developer or backend database, then it is not surprising if the variable naming uses  snake_case.

Such cross-standards need to be addressed in a way  @SerializeName that avoids inter-platform confusion.

Okay, since I happen to be an Andriod developer, so I will use a java example. The case study is that we consume an API that provides a json object response, where each property is named using the standard  snake_case. So the way we transform it into the java standard is by using  @SerializeName.

Example of User Json Response

{
   "first_name": "tejo",
   "birth_date": "02-02-2000"
}

Person Class Data Example

public class Person {

    @SerializedName("first_name")
    private String personName;

    @SerializedName("birth_date")
    private String birthDate;

}

So that's it, while for  Deserialising annotations  we will discuss it on another occasion, and in another post.


Post a Comment

Previous Next

نموذج الاتصال