Serializing and Deserializing Enums using GSON

By default Gson serializes enums by name in lowercase. If this is not sufficient and you want some other stirng 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’ll 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’.

package com.memorynotfound.json;

import com.google.gson.annotations.SerializedName;
import java.lang.management.ManagementFactory;

public class Health {

    enum Status {
        @SerializedName("1") UP,
        @SerializedName("0") DOWN
    }

    private String hostname;
    private String ip;
    private long startTime;
    private long upTime;
    private Status status;

    public Health(String hostname, String ip, Status status) {
        this.hostname = hostname;
        this.ip = ip;
        this.startTime = ManagementFactory.getRuntimeMXBean().getStartTime();
        this.upTime = ManagementFactory.getRuntimeMXBean().getUptime();
        this.status = status;
    }

    @Override
    public String toString() {
        return "Health{" +
                "hostname='" + hostname + '\'' +
                ", ip='" + ip + '\'' +
                ", startTime=" + startTime +
                ", upTime=" + upTime +
                ", status=" + status +
                '}';
    }
}

Serialize Enum using GSON

Here is an example how you can serialize an enum using GSON.

package com.memorynotfound.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SerializeEnumGson {

    public static void main(String... args){
        Health health = new Health("host", "ip", Health.Status.UP);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String result = gson.toJson(health);
        System.out.println(result);
    }
}

Generated output:

{
  "hostname": "host",
  "ip": "ip",
  "startTime": 1457088308405,
  "upTime": 62,
  "status": "1"
}

Deserialize Enum using GSON

Here is an example how to deserialize a JSON string with an enum to a Java object.

package com.memorynotfound.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class DeserializeEnumGson {

    public static void main(String... args){
        String input = "{\n" +
                "  \"hostname\": \"host\",\n" +
                "  \"ip\": \"ip\",\n" +
                "  \"startTime\": 1457088308405,\n" +
                "  \"upTime\": 62,\n" +
                "  \"status\": \"1\"\n" +
                "}";
        Gson gson = new GsonBuilder().create();
        Health health = gson.fromJson(input, Health.class);
        System.out.println(health);
    }
}

Generated output:

Health{hostname='host', ip='ip', startTime=1457088308405, upTime=62, status=UP}

References

You may also like...