Introduction
In this blog we will learn how to serialize and deserialize enums using GSON.
To learn basics of GSON you can refer this guide.
Let’s get started!
Enum is a datatype which has predefined constants.
In our example we will use AccountState as enum which have 3 account opening states: OPEN,CLOSED,PENDING
But before that let us add below dependency to out maven project
1
2
3
4
5
|
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
|
Serializing enums using GSON
In the below example we have Account class which has enum AccountState as member variable.
To get started we initialize Gson class.
1
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
To serialize Account class we can call toJson() method on gson object and pass Account object as argument to it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package com.harishgowda.demo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonEnumDemo {
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Account account = new Account(1,"Saving", AccountState.OPEN);
System.out.println(gson.toJson(account));
}
}
class Account {
private int accountId;
private String accountType;
private AccountState accountState;
public Account(int accountId, String accountType, AccountState accountState) {
this.accountId = accountId;
this.accountType = accountType;
this.accountState = accountState;
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public AccountState getAccountState() {
return accountState;
}
public void setAccountState(AccountState accountState) {
this.accountState = accountState;
}
@Override
public String toString() {
return "Account{" +
"accountId=" + accountId +
", accountType=" + accountType +
", accountState=" + accountState +
'}';
}
}
enum AccountState {
OPEN,
CLOSED,
PENDING
}
|
Output is
1
2
3
4
5
|
{
"accountId": 1,
"accountType": "Saving",
"accountState": "OPEN"
}
|
Deserializing enums using GSON
Given a json string we can deserialize it to Account class which contains AccountState enum as member variable.
In this case we need to call fromJson() method of GSON object and pass jsonString as first argument and Account class as second argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package com.harishgowda.demo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonEnumDemo {
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonString = "{\n" +
" \"accountId\": 10,\n" +
" \"accountType\": \"Current\",\n" +
" \"accountState\": \"PENDING\"\n" +
"}";
Account accountUpdated = gson.fromJson(jsonString,Account.class);
System.out.println(accountUpdated);
}
}
class Account {
private int accountId;
private String accountType;
private AccountState accountState;
public Account(int accountId, String accountType, AccountState accountState) {
this.accountId = accountId;
this.accountType = accountType;
this.accountState = accountState;
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public AccountState getAccountState() {
return accountState;
}
public void setAccountState(AccountState accountState) {
this.accountState = accountState;
}
@Override
public String toString() {
return "Account{" +
"accountId=" + accountId +
", accountType=" + accountType +
", accountState=" + accountState +
'}';
}
}
enum AccountState {
OPEN,
CLOSED,
PENDING
}
|
Output is:
1
|
Account{accountId=10, accountType=Current, accountState=PENDING}
|
What will happen if we have incorrect enum state in json string?
In this case it will map enum value to null
1
2
3
4
5
6
7
8
9
|
String jsonString = "{\n" +
" \"accountId\": 10,\n" +
" \"accountType\": \"Current\",\n" +
" \"accountState\": \"TEST\"\n" +
"}";
Account accountUpdated = gson.fromJson(jsonString,Account.class);
System.out.println(accountUpdated);
|
In above json string we have TEST as accountState which is invalid enum value
Hence accountState will be null
1
2
|
Account{accountId=10, accountType=Current, accountState=null}
|
Serializing and Deserializing Enums using @SerializedName annotation
@SerializedName annotation is used to serialize a field with a different name instead of actual specified name
In the below we have annotated enum AccountState values with numbers for different states using @SerializedName annotation.
1
2
3
4
5
6
7
8
9
10
|
enum AccountState {
@SerializedName("1")
OPEN,
@SerializedName("0")
CLOSED,
@SerializedName("2")
PENDING
}
|
Below is complete program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package com.harishgowda.demo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
enum AccountState {
@SerializedName("1")
OPEN,
@SerializedName("0")
CLOSED,
@SerializedName("2")
PENDING
}
public class GsonEnumDemo {
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Account account = new Account(1,"Saving", AccountState.OPEN);
System.out.println(gson.toJson(account));
String jsonString = "{\n" +
" \"accountId\": 10,\n" +
" \"accountType\": \"Current\",\n" +
" \"accountState\": \"2\"\n" +
"}";
Account accountUpdated = gson.fromJson(jsonString,Account.class);
System.out.println(accountUpdated);
}
}
class Account {
private int accountId;
private String accountType;
private AccountState accountState;
public Account(int accountId, String accountType, AccountState accountState) {
this.accountId = accountId;
this.accountType = accountType;
this.accountState = accountState;
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public AccountState getAccountState() {
return accountState;
}
public void setAccountState(AccountState accountState) {
this.accountState = accountState;
}
@Override
public String toString() {
return "Account{" +
"accountId=" + accountId +
", accountType=" + accountType +
", accountState=" + accountState +
'}';
}
}
|
In the above code snippet AccountState.OPEN is serialized to 1 after serializing.
Similarly,while deserializing we have json string which has accountState=2 which is mapped to AccountState.PENDING as required.
Output is:
1
2
3
4
5
6
7
|
{
"accountId": 1,
"accountType": "Saving",
"accountState": "1"
}
Account{accountId=10, accountType=Current, accountState=PENDING}
|