Gson Advanced — Mapping of Enums

At the beginning of this series, we've shown you how to map nested objects, arrays and lists, Java maps and more. In this blog post, you'll learn how to (de)serialize Java enums.

Of course, this is not the only post in our Gson series. If you're interested in the other topics, check out our series outline:

Gson Series Overview

Serialization of Java Enums

Java enums can be a very helpful data type. They limit the variable values to a predefined amount of constants. While this is super practical internally, there is no JSON equivalent. JSON doesn't know any constants, so what happens if your model contains enums?

First, let's create an example Java enum for the days of the week:

public enum Day {  
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
}

This is a standard enum, which you'll probably find in a lot of Java apps. Let's assume our app uses a UserDayEnum model class, which contains a enum property day:

public class UserDayEnum {  
    private String _name;
    private String email;
    private boolean isDeveloper;
    private int age;

    private Day day = Day.FRIDAY;
}    

The default initalization of day to Friday has no deeper mearning.

But what happens if we're trying to serialize one of those objects? Our long-time readers, or developers who've worked with Gson before, already have a gut feeling: yes, Gson does everything automatically :)

Without any additional configuration we can simply call toJson() and Gson will use the enum's value as JSON value:

UserDayEnum userObject = new UserDayEnum("Norman", "norman@fs.io", true, 26, Day.SUNDAY);

Gson gson = new Gson();  
String userWithEnumJson = gson.toJson(userObject);  

This results in:

{
  "_name": "Norman",
  "age": 26,
  "day": "SUNDAY",
  "email": "norman@fs.io",
  "isDeveloper": true
}

Deserialization of Java Enums

The deserialization works very similar. You don't have to do any additional configuration and can just call fromJson() with the known approach. For example, Gson would create a correctly matching Java object from the following JSON:

{
  "email": "norman@futurestud.io",
  "age": 26,
  "day": "FRIDAY"
}

As you might now, internally enums have an ordinal number assigned to each enum value. For our Day enum, MONDAY would be 0, TUESDAY would be 1 and so on. Gson also accepts those ordinals as an deserialization input and matches it to the correct enum value.

{
  "email": "norman@futurestud.io",
  "age": 26,
  "day": 4
}

For the case of the JSON above Gson would also set the day value to FRIDAY.

In case you would like to serialize the Friday to a 4, the next section is for you.

Customizing Enum (De)Serialization

Lastly, the enum values might not always have a good or an appropriate name. Of course, you can use the same annotations we've seen earlier to change the (de)serialization. For example, @SerializedName gives you the option to change the names of one or more enum values:

public enum Day {  
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    @SerializedName("LazyDay1") SATURDAY,
    @SerializedName("LazyDay2") SUNDAY
}

Outlook

In this blog post you've seen how you can serialize and deserialize Java enums from and to JSON. Gson does an excellent job of keeping it easy and intuitive.

If you've feedback or a question, let us know in the comments or on twitter @futurestud_io.

Make it rock & enjoy coding!

Explore the Library

Find interesting tutorials and solutions for your problems.