[deserializeEnumCaseInsensitve] Deserializing Enums in a Case-Insensitive Manner in Jackson (#12868)
This commit is contained in:
parent
4da0255a71
commit
b522237512
@ -1,20 +1,33 @@
|
|||||||
package com.baeldung.jackson.enums.deserialization;
|
package com.baeldung.jackson.enums.deserialization;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
|
||||||
|
import com.fasterxml.jackson.databind.MapperFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
|
|
||||||
public class DefaultEnumDeserializationUnitTest {
|
public class DefaultEnumDeserializationUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEnum_whenDeserializingJson_thenCorrectRepresentation() throws JsonParseException, IOException {
|
public void givenEnum_whenDeserializingJson_thenCorrectRepresentation() throws IOException {
|
||||||
String json = "{\"distance\":\"KILOMETER\"}";
|
String json = "{\"distance\":\"KILOMETER\"}";
|
||||||
City city = new ObjectMapper().readValue(json, City.class);
|
City city = new ObjectMapper().readValue(json, City.class);
|
||||||
|
|
||||||
assertEquals(Distance.KILOMETER, city.getDistance());
|
assertEquals(Distance.KILOMETER, city.getDistance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEnum_whenDeserializingJsonWithMapperFeature_thenCorrectRepresentation() throws IOException {
|
||||||
|
String json = "{\"distance\":\"KiLoMeTeR\"}";
|
||||||
|
ObjectMapper objectMapper = JsonMapper.builder()
|
||||||
|
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
|
||||||
|
.build();
|
||||||
|
City city = objectMapper.readValue(json, City.class);
|
||||||
|
|
||||||
|
assertEquals(Distance.KILOMETER, city.getDistance());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user