|
1 | 1 | package com.baeldung.jackson.test; |
2 | 2 |
|
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.hamcrest.CoreMatchers.not; |
3 | 5 | import static org.hamcrest.Matchers.equalTo; |
4 | 6 | import static org.hamcrest.Matchers.notNullValue; |
5 | 7 | import static org.junit.Assert.assertNotNull; |
6 | 8 | import static org.junit.Assert.assertThat; |
7 | 9 |
|
8 | 10 | import java.io.IOException; |
| 11 | +import java.time.ZoneId; |
| 12 | +import java.time.ZonedDateTime; |
| 13 | + |
| 14 | +import org.junit.Test; |
9 | 15 |
|
10 | 16 | import com.baeldung.jackson.deserialization.ItemDeserializer; |
11 | 17 | import com.baeldung.jackson.dtos.Item; |
12 | 18 | import com.baeldung.jackson.dtos.ItemWithSerializer; |
13 | 19 | import com.baeldung.jackson.dtos.MyDto; |
14 | 20 | import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown; |
15 | | -import org.junit.Test; |
16 | | - |
17 | 21 | import com.fasterxml.jackson.core.JsonFactory; |
18 | 22 | import com.fasterxml.jackson.core.JsonParseException; |
19 | 23 | import com.fasterxml.jackson.core.JsonParser; |
20 | 24 | import com.fasterxml.jackson.databind.DeserializationFeature; |
21 | 25 | import com.fasterxml.jackson.databind.JsonMappingException; |
22 | 26 | import com.fasterxml.jackson.databind.JsonNode; |
23 | 27 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | +import com.fasterxml.jackson.databind.SerializationFeature; |
24 | 29 | import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
25 | 30 | import com.fasterxml.jackson.databind.module.SimpleModule; |
26 | 31 |
|
@@ -165,4 +170,35 @@ public final void givenDeserializerIsOnClass_whenDeserializingCustomRepresentati |
165 | 170 | assertThat(readValue, notNullValue()); |
166 | 171 | } |
167 | 172 |
|
| 173 | + @Test |
| 174 | + public void whenDeserialisingZonedDateTimeWithDefaults_thenTimeZoneIsNotPreserved() throws IOException { |
| 175 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 176 | + objectMapper.findAndRegisterModules(); |
| 177 | + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
| 178 | + // construct a new instance of ZonedDateTime |
| 179 | + ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin")); |
| 180 | + String converted = objectMapper.writeValueAsString(now); |
| 181 | + // restore an instance of ZonedDateTime from String |
| 182 | + ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class); |
| 183 | + System.out.println("serialized: " + now); |
| 184 | + System.out.println("restored: " + restored); |
| 185 | + assertThat(now, is(not(restored))); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void whenDeserialisingZonedDateTimeWithFeaturesDisabled_thenTimeZoneIsPreserved() throws IOException { |
| 190 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 191 | + objectMapper.findAndRegisterModules(); |
| 192 | + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
| 193 | + objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); |
| 194 | + // construct a new instance of ZonedDateTime |
| 195 | + ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin")); |
| 196 | + String converted = objectMapper.writeValueAsString(now); |
| 197 | + // restore an instance of ZonedDateTime from String |
| 198 | + ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class); |
| 199 | + System.out.println("serialized: " + now); |
| 200 | + System.out.println("restored: " + restored); |
| 201 | + assertThat(now, is(restored)); |
| 202 | + } |
| 203 | + |
168 | 204 | } |
0 commit comments