In this quick article, we show you how to serialize and deserialize enum types to and from its JSON representation.
Video Tutorial
This tutorial is explained in below YouTube video:
GSON Maven Dependency
To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:
<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
Let's first create Project POJO class and ProjectStatus enum and we will use this class in upcoming examples.
enum ProjectStatus {
NEW, OPEN, PROGRESS, HOLD, COMPLETED, CLOSED
}
class Project {
private int projectId;
private String projectName;
private String projectDesc;
private ProjectStatus projectStatus;
public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
super();
this.projectId = projectId;
this.projectName = projectName;
this.projectDesc = projectDesc;
this.projectStatus = projectStatus;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDesc() {
return projectDesc;
}
public void setProjectDesc(String projectDesc) {
this.projectDesc = projectDesc;
}
public ProjectStatus getProjectStatus() {
return projectStatus;
}
public void setProjectStatus(ProjectStatus projectStatus) {
this.projectStatus = projectStatus;
}
@Override
public String toString() {
return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
", projectStatus=" + projectStatus + "]";
}
}
Serialize Enum Type
public class GSONEnumExamples {
public static void main(String[] args) {
serializeEnumGson();
}
public static void serializeEnumGson() {
Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String result = gson.toJson(project);
System.out.println(result);
}
}
Output:
{
"projectId": 100,
"projectName": "CMS",
"projectDesc": "Content Management System",
"projectStatus": "NEW"
}
Deserialize Enum Type
public class GSONEnumExamples {
public static void main(String[] args) {
deserializeEnumGson();
}
public static void deserializeEnumGson() {
String input = "{\r\n" +
" \"projectId\": 100,\r\n" +
" \"projectName\": \"CMS\",\r\n" +
" \"projectDesc\": \"Content Management System\",\r\n" +
" \"projectStatus\": \"NEW\"\r\n" +
"}";
Gson gson = new GsonBuilder().create();
Project project = gson.fromJson(input, Project.class);
System.out.println(project.toString());
}
}
Output:
Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]
Complete Example for Reference
package net.javaguides.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GSONEnumExamples {
public static void main(String[] args) {
serializeEnumGson();
deserializeEnumGson();
}
public static void serializeEnumGson() {
Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String result = gson.toJson(project);
System.out.println(result);
}
public static void deserializeEnumGson() {
String input = "{\r\n" +
" \"projectId\": 100,\r\n" +
" \"projectName\": \"CMS\",\r\n" +
" \"projectDesc\": \"Content Management System\",\r\n" +
" \"projectStatus\": \"NEW\"\r\n" +
"}";
Gson gson = new GsonBuilder().create();
Project project = gson.fromJson(input, Project.class);
System.out.println(project.toString());
}
}
enum ProjectStatus {
NEW,
OPEN,
PROGRESS,
HOLD,
COMPLETED,
CLOSED
}
class Project {
private int projectId;
private String projectName;
private String projectDesc;
private ProjectStatus projectStatus;
public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
super();
this.projectId = projectId;
this.projectName = projectName;
this.projectDesc = projectDesc;
this.projectStatus = projectStatus;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDesc() {
return projectDesc;
}
public void setProjectDesc(String projectDesc) {
this.projectDesc = projectDesc;
}
public ProjectStatus getProjectStatus() {
return projectStatus;
}
public void setProjectStatus(ProjectStatus projectStatus) {
this.projectStatus = projectStatus;
}
@Override
public String toString() {
return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
", projectStatus=" + projectStatus + "]";
}
}
Output:
{
"projectId": 100,
"projectName": "CMS",
"projectDesc": "Content Management System",
"projectStatus": "NEW"
}
Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]
Serialize and Deserialize Enum using @SerializedName annotation
By default, Gson serializes enums by name in lowercase. If this is not sufficient and you want some other string or an integer value, then have a look at the @SerializedName annotation. Gson can serialize and deserialize enums using the @SerializedName annotation. If we annotate an enum with the @SerializedName we can supply a value which will be mapped when GSON serializes or deserializes the JSON to or from Java Object.
Let’s start with an example. In the following class, we annotate the status enums with @SerializedName. We can map these enums with a value, this value is used during serialization. When deserialization, if the value is specified it’ll use the correct enum. When the value is not found it’ll return ‘null’.
Let's add a @SerializedName annotation to ProjectStatus enum as below:
enum ProjectStatus {
@SerializedName("1")
NEW, @SerializedName("2")
OPEN, @SerializedName("3")
PROGRESS, @SerializedName("4")
HOLD, @SerializedName("5")
COMPLETED, @SerializedName("6")
CLOSED
}
Let's write a complete example to Serialize and Deserialize Enum using @SerializedName annotation:
package net.javaguides.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
public class GSONEnumExamples {
public static void main(String[] args) {
serializeEnumGson();
deserializeEnumGson();
}
public static void serializeEnumGson() {
Project project = new Project(100, "CMS", "Content Management System", ProjectStatus.NEW);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String result = gson.toJson(project);
System.out.println(result);
}
public static void deserializeEnumGson() {
String input = "{\r\n" + " \"projectId\": 100,\r\n" + " \"projectName\": \"CMS\",\r\n" +
" \"projectDesc\": \"Content Management System\",\r\n" + " \"projectStatus\": \"1\"\r\n" + "}";
Gson gson = new GsonBuilder().create();
Project project = gson.fromJson(input, Project.class);
System.out.println(project.toString());
}
}
enum ProjectStatus {
@SerializedName("1")
NEW, @SerializedName("2")
OPEN, @SerializedName("3")
PROGRESS, @SerializedName("4")
HOLD, @SerializedName("5")
COMPLETED, @SerializedName("6")
CLOSED
}
class Project {
private int projectId;
private String projectName;
private String projectDesc;
private ProjectStatus projectStatus;
public Project(int projectId, String projectName, String projectDesc, ProjectStatus projectStatus) {
super();
this.projectId = projectId;
this.projectName = projectName;
this.projectDesc = projectDesc;
this.projectStatus = projectStatus;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDesc() {
return projectDesc;
}
public void setProjectDesc(String projectDesc) {
this.projectDesc = projectDesc;
}
public ProjectStatus getProjectStatus() {
return projectStatus;
}
public void setProjectStatus(ProjectStatus projectStatus) {
this.projectStatus = projectStatus;
}
@Override
public String toString() {
return "Project [projectId=" + projectId + ", projectName=" + projectName + ", projectDesc=" + projectDesc +
", projectStatus=" + projectStatus + "]";
}
}
Output:
{
"projectId": 100,
"projectName": "CMS",
"projectDesc": "Content Management System",
"projectStatus": "1"
}
Project [projectId=100, projectName=CMS, projectDesc=Content Management System, projectStatus=NEW]
Comments
Post a Comment
Leave Comment