Deserialize String To Enum Java

Deserialize String To Enum Java | The Enumeration is a named constant used to define a class type. Like class, Java enumerations can also have methods, instance variables, and constructors also. In Java, enumerations are created by using the enum keyword. 

Convert JSON String To Enum In Java

// Distance class
public enum Distance {
   KM("km", 1000), CM("cm", 0.01), 
   INCH("inch", 0.0254), MILE("mile", 1609.34), 
   MM("mm", 0.001), METER("meter", 1),
   UNKNOWN("un", 0);

   public String units;
   public final double meter;

   private Distance(String unit, double meters) {
      this.units = unit;
      this.meter = meters;
   }
}
class City {
   Distance distance;

   public Distance getDistance() {
      return distance;
   }
}
// Main class
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
   public static void main(String args[]) 
    throws JsonMappingException, JsonProcessingException {

      String string = "{\"distance\":\"INCH\"}";
      ObjectMapper objectMapper = new ObjectMapper();

      City city = objectMapper.readValue(string, City.class);
      System.out.println("Distance of the City is in: " 
         + city.getDistance());
   }
}

Output:

Distance of the City is in: INCH

Deserialize String To Enum Java

The annotation @JsonValue is the annotation that can be used for both serialization and deserialization, as enums are constant this is possible. Let us see it through an example:-

// Distance class
import com.fasterxml.jackson.annotation.JsonValue;

public enum Distance {
   KM("km", 1000), METER("meter", 1), 
   MM("mm", 0.001), INCH("inch", 0.0254), 
   CM("cm", 0.01), MILE("mile", 1609.34),
   UNKNOWN("un", 0);

   public String units;
   public final double meters;

   private Distance(String unit, double meter) {
      this.units = unit;
      this.meters = meter;
   }

   @JsonValue
   public double getEnumValue() {
      return meters;
   }
}
// Main class
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
   public static void main(String args[]) 
      throws JsonMappingException, JsonProcessingException {

      String string = "{\"distance\":\"0.0254\"}";
      ObjectMapper objectMapper = new ObjectMapper();

      City city = objectMapper.readValue(string, City.class);
      System.out.println("Distance of the City is in: " 
         + city.getDistance());
   }
}

Output:-

Distance of the City: INCH

We have the same Distance class given in the above example. This time based on the value it matches the valid enum constant.

Also see:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.