|
| 1 | +package com.baeldung.jackson.objectmapper; |
| 2 | + |
| 3 | +import com.baeldung.jackson.objectmapper.dto.Car; |
| 4 | +import com.baeldung.jackson.objectmapper.dto.Request; |
| 5 | +import com.fasterxml.jackson.core.Version; |
| 6 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.text.DateFormat; |
| 13 | +import java.text.SimpleDateFormat; |
| 14 | +import java.util.Date; |
| 15 | + |
| 16 | +import static org.hamcrest.Matchers.containsString; |
| 17 | +import static org.hamcrest.Matchers.equalTo; |
| 18 | +import static org.junit.Assert.assertNotNull; |
| 19 | +import static org.junit.Assert.assertThat; |
| 20 | + |
| 21 | +public class TestSerializationDeserializationFeature { |
| 22 | + final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; |
| 23 | + final String JSON_CAR = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }"; |
| 24 | + final String JSON_ARRAY = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]"; |
| 25 | + |
| 26 | + @Test |
| 27 | + public void whenFailOnUnkownPropertiesFalse_thanJsonReadCorrectly() throws Exception { |
| 28 | + |
| 29 | + final ObjectMapper objectMapper = new ObjectMapper(); |
| 30 | + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 31 | + final Car car = objectMapper.readValue(JSON_CAR, Car.class); |
| 32 | + final JsonNode jsonNodeRoot = objectMapper.readTree(JSON_CAR); |
| 33 | + final JsonNode jsonNodeYear = jsonNodeRoot.get("year"); |
| 34 | + final String year = jsonNodeYear.asText(); |
| 35 | + |
| 36 | + assertNotNull(car); |
| 37 | + assertThat(car.getColor(), equalTo("Black")); |
| 38 | + assertThat(year, containsString("1970")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenCustomSerializerDeserializer_thanReadWriteCorrect() throws Exception { |
| 43 | + final ObjectMapper mapper = new ObjectMapper(); |
| 44 | + final SimpleModule serializerModule = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null)); |
| 45 | + serializerModule.addSerializer(Car.class, new CustomCarSerializer()); |
| 46 | + mapper.registerModule(serializerModule); |
| 47 | + final Car car = new Car("yellow", "renault"); |
| 48 | + final String carJson = mapper.writeValueAsString(car); |
| 49 | + assertThat(carJson, containsString("renault")); |
| 50 | + assertThat(carJson, containsString("model")); |
| 51 | + |
| 52 | + final SimpleModule deserializerModule = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null)); |
| 53 | + deserializerModule.addDeserializer(Car.class, new CustomCarDeserializer()); |
| 54 | + mapper.registerModule(deserializerModule); |
| 55 | + final Car carResult = mapper.readValue(EXAMPLE_JSON, Car.class); |
| 56 | + assertNotNull(carResult); |
| 57 | + assertThat(carResult.getColor(), equalTo("Black")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void whenDateFormatSet_thanSerializedAsExpected() throws Exception { |
| 62 | + final ObjectMapper objectMapper = new ObjectMapper(); |
| 63 | + final Car car = new Car("yellow", "renault"); |
| 64 | + final Request request = new Request(); |
| 65 | + request.setCar(car); |
| 66 | + request.setDatePurchased(new Date()); |
| 67 | + final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z"); |
| 68 | + objectMapper.setDateFormat(df); |
| 69 | + final String carAsString = objectMapper.writeValueAsString(request); |
| 70 | + assertNotNull(carAsString); |
| 71 | + assertThat(carAsString, containsString("datePurchased")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void whenUseJavaArrayForJsonArrayTrue_thanJsonReadAsArray() throws Exception { |
| 76 | + final ObjectMapper objectMapper = new ObjectMapper(); |
| 77 | + objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); |
| 78 | + final Car[] cars = objectMapper.readValue(JSON_ARRAY, Car[].class); |
| 79 | + for (final Car car : cars) { |
| 80 | + assertNotNull(car); |
| 81 | + assertThat(car.getType(), equalTo("BMW")); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments