➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java
➤ Take Multiple String Input in Java
➤ How To Reverse a String In Java
➤ Remove Special Characters
➤ String – Remove Character
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Second Occurrence of Character
➤ Replace nth Occurrence String
➤ Last Occurrence of Character
➤ Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String
➤ Substring Method In Java
➤ Split Method In 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:-
- Enum of Strings Java
- How to Remove Spaces From String In Java
- Java Set Character in String
- Java For Each Character In String
- Java String Interpolation
- Quotes in a String Java
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!