为什么 Jackson 的默认解串器将 Zone 设置为 UTC 而不是 Z?
Why does Jackson's default deserializer set the Zone to UTC rather than Z?
我想我一定是误解了 Zones 在 java 的 ZonedDateTime class 中是如何工作的。当我使用 Jackson 序列化然后反序列化 now() 时,反序列化值具有 getZone() == "UTC" 而不是序列化值中的 "Z" 。谁能向我解释这是为什么以及我应该做什么?
下面的代码打印:
{"t":"2017-11-24T18:00:08.425Z"}
Data [t=2017-11-24T18:00:08.425Z]
Data [t=2017-11-24T18:00:08.425Z[UTC]]
Z
UTC
java来源:
<!-- language: java -->
package model;
import static org.junit.Assert.*;
import java.io.IOException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class ZonedDateTimeSerializationTest {
static public class Data {
@Override
public String toString() {
return "Data [t=" + t + "]";
}
public ZonedDateTime getT() {
return t;
}
public void setT(ZonedDateTime t) {
this.t = t;
}
ZonedDateTime t = ZonedDateTime.now(ZoneOffset.UTC);
};
@Test
public void testDeSer() throws IOException {
Data d = new Data();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
String serialized = objectMapper.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(d);
System.out.println(serialized);
Data d2 = objectMapper.readValue(serialized, Data.class);
System.out.println(d);
System.out.println(d2);
System.out.println(d.getT().getZone());
System.out.println(d2.getT().getZone());
// this fails
assertEquals(d, d2);
}
}
默认情况下,在反序列化 ZonedDateTime
期间,Jackson 会将解析的时区调整为上下文提供的时区。您可以使用此设置修改此行为,以便解析的 ZonedDateTime
将保留在 Z
:
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
更多详情here
我这样做是为了实际保留时区:
mapper.enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
我想我一定是误解了 Zones 在 java 的 ZonedDateTime class 中是如何工作的。当我使用 Jackson 序列化然后反序列化 now() 时,反序列化值具有 getZone() == "UTC" 而不是序列化值中的 "Z" 。谁能向我解释这是为什么以及我应该做什么?
下面的代码打印:
{"t":"2017-11-24T18:00:08.425Z"}
Data [t=2017-11-24T18:00:08.425Z]
Data [t=2017-11-24T18:00:08.425Z[UTC]]
Z
UTC
java来源:
<!-- language: java -->
package model;
import static org.junit.Assert.*;
import java.io.IOException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class ZonedDateTimeSerializationTest {
static public class Data {
@Override
public String toString() {
return "Data [t=" + t + "]";
}
public ZonedDateTime getT() {
return t;
}
public void setT(ZonedDateTime t) {
this.t = t;
}
ZonedDateTime t = ZonedDateTime.now(ZoneOffset.UTC);
};
@Test
public void testDeSer() throws IOException {
Data d = new Data();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
String serialized = objectMapper.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(d);
System.out.println(serialized);
Data d2 = objectMapper.readValue(serialized, Data.class);
System.out.println(d);
System.out.println(d2);
System.out.println(d.getT().getZone());
System.out.println(d2.getT().getZone());
// this fails
assertEquals(d, d2);
}
}
默认情况下,在反序列化 ZonedDateTime
期间,Jackson 会将解析的时区调整为上下文提供的时区。您可以使用此设置修改此行为,以便解析的 ZonedDateTime
将保留在 Z
:
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
更多详情here
我这样做是为了实际保留时区:
mapper.enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID)
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)