在传入的JSON中,我有一个ISO8601兼容的日期时间字段,其中包含区域偏移量。我想保留这个偏移量,但不幸的是杰克逊默认为GMT/UTC,而反序列化这个字段(我从http://wiki.fasterxml.com/JacksonFAQDateHandling了解到)。如何在反序列化时保留偏移量与Jackson的OffsetDateTime
@RunWith(JUnit4.class)
public class JacksonOffsetDateTimeTest {
private ObjectMapper objectMapper;
@Before
public void init() {
objectMapper = Jackson2ObjectMapperBuilder.json()
.modules(new JavaTimeModule())
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();
}
@Test
public void test() throws IOException {
final String json = "{ \"date\": \"2000-01-01T12:00:00.000-04:00\" }";
final JsonType instance = objectMapper.readValue(json, JsonType.class);
assertEquals(ZoneOffset.ofHours(-4), instance.getDate().getOffset());
}
}
public class JsonType {
private OffsetDateTime date;
// getter, setter
}
就是我在这里得到的是:
java.lang.AssertionError: expected:<-04:00> but was:<Z>
我怎样才能使返回OffsetDateTime包含原来的补偿?
我在杰克逊2.8.3。
太棒了,谢谢!在挖掘了杰克逊的内幕之后,我自己就明白了这一点。 –