[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;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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.json.JsonMapper;
|
||||
|
||||
public class DefaultEnumDeserializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenEnum_whenDeserializingJson_thenCorrectRepresentation() throws JsonParseException, IOException {
|
||||
public void givenEnum_whenDeserializingJson_thenCorrectRepresentation() throws IOException {
|
||||
String json = "{\"distance\":\"KILOMETER\"}";
|
||||
City city = new ObjectMapper().readValue(json, City.class);
|
||||
|
||||
|
||||
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…
Reference in New Issue