Gson @SerializedName

In this Gson @SerializedName example, learn to change the name of fields between json and java objects while serialization and deserialization process.

1. @SerializedName

By default, we assume the Java model class and the JSON will exactly same field names. But sometimes, it is not the case and certain names differ. Now we have to map the someName in json to someOtherName in Java class. This is where @SerializedName annotation helps.

@SerializedName annotation indicates the annotated member should be serialized to JSON with the provided name value as its field name. This annotation will override any FieldNamingPolicy, including the default field naming policy, that may have been using the GsonBuilder class.

Note that the value you specify in this annotation must be a valid JSON field name.

1.1. Annotation attributes

It accepts two attributes:

  • value – the desired name of the field when it is serialized or deserialized.
  • alternate – the alternative names of the field when it is deserialized. It provides more possible names other than ‘value’ attribute. If there are multiple fields that match one property, Gson will use the one that is processed last.

Remember, alternate option to have multiple names is limited to deserialization. In serialization, it will not have any impact.

2. Change field names during Serialization

Lets take an example of Employee class with only 4 fields. We want to create JSON where "email" is written as field name "emailId".

public class Employee 
{
	private Integer id;
    private String firstName;
    private String lastName;

    @SerializedName(value = "emailId", alternate = "emailAddress")
    private String email;
}

Let’s serialize one employee record and see the JSON output.

Employee emp = new Employee(1001, "Lokesh", "Gupta", "howtodoinjava@gmail.com");

Gson gson = new GsonBuilder().setPrettyPrinting().create();  

System.out.println(gson.toJson(emp));

Program output.

{
  "id": 1001,
  "firstName": "Lokesh",
  "lastName": "Gupta",
  "emailId": "howtodoinjava@gmail.com"
}

3. Change field names during Deserialization

Java program to map different field name during deserializing JSON to Java class.

{
  "id": 1001,
  "firstName": "Lokesh",
  "lastName": "Gupta",
  "email": "howtodoinjava@gmail.com",
  "emailAddress": "admin@gmail.com"
}
String json = "{'id': 1001,"
		+ "'firstName': 'Lokesh',"
		+ "'lastName': 'Gupta',"
		+ "'email': 'howtodoinjava@gmail.com',"
		+ "'emailAddress': 'admin@gmail.com'}";
		
Gson gson = new GsonBuilder().setPrettyPrinting().create(); 

Employee emp = gson.fromJson(json, Employee.class);

System.out.println(emp);

Program output.

Employee [id=1001, firstName=Lokesh, lastName=Gupta, email=admin@gmail.com]

Notice the program output. we had two matches for email field i.e. email and emailAddress. The last occurrence was for "emailAddress", so it’s value got populated into Employee object.

Drop me your questions related to mapping multiple different names to member field in Java class using Gson @SerializedName annotation.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

2 thoughts on “Gson @SerializedName”

  1. This is INCORRECT: “Notice the program output. we had two matches for email field i.e. email and emailAddress. The last occurrence was for “emailAddress”, so it’s value got populated into Employee object.”

    The name of the field i.e. email in this case does not to Gson. If we have value = “XYZ” and alternate=”DEF”, then first it looks for XYZ, if not then it looks for DEF[Only during deserialization].

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.