JodaTime 日期的序列化和反序列化为 json
Serialization and Deserialization of JodaTime dates as json
根据此处找到的文档https://github.com/FasterXML/jackson-datatype-joda
下面的代码片段应该通过
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
final String INPUT_JSON = "{\"start\" : \"1972-12-28T12:00:01.000Z\"}";
Bean bean = mapper.readValue(INPUT_JSON, Bean.class);
String json = mapper.writeValueAsString(bean);
Assert.assertEquals(INPUT_JSON, json);
然而,这个 returns:
org.junit.ComparisonFailure:
Expected :{"start" : "1972-12-28T12:00:01.000Z"}
Actual :{"start":94392001000}
这是我正在使用的 pom 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
为什么 datetime 对象没有正确序列化?
测试似乎缺少此功能。
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ,false);
根据此处找到的文档https://github.com/FasterXML/jackson-datatype-joda
下面的代码片段应该通过
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
final String INPUT_JSON = "{\"start\" : \"1972-12-28T12:00:01.000Z\"}";
Bean bean = mapper.readValue(INPUT_JSON, Bean.class);
String json = mapper.writeValueAsString(bean);
Assert.assertEquals(INPUT_JSON, json);
然而,这个 returns:
org.junit.ComparisonFailure:
Expected :{"start" : "1972-12-28T12:00:01.000Z"}
Actual :{"start":94392001000}
这是我正在使用的 pom 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
为什么 datetime 对象没有正确序列化?
测试似乎缺少此功能。
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ,false);