Java如何使用Jackson格式化LocalDate对象?
我们有一个Recording具有Java8java.time.LocalDate属性的类。我们需要从JSON字符串反序列化此属性并将其序列化。为此,我们可以使用@JsonDeserialize和@JsonSerialize注释来注释该类的LocalDate属性Recording。
@JsonDeserialize(using = LocalDateDeserializer.class) @JsonSerialize(using = LocalDateSerializer.class) private LocalDate releaseDate;
要使用注释,我们需要创建一个类来反序列化和序列化值。要创建一个反序列化器类,我们创建一个extends类StdDeserializer。序列化程序类扩展了StdSerializer该类。下面是LocalDateSerializerandLocalDateDeserializer类的定义。
package org.nhooo.example.jackson; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; import java.io.IOException; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class LocalDateSerializer extends StdSerializer<LocalDate> { public LocalDateSerializer() { super(LocalDate.class); } @Override public void serialize(LocalDate value, JsonGenerator generator, SerializerProvider provider) throws IOException { generator.writeString(value.format(DateTimeFormatter.ISO_LOCAL_DATE)); } }
package org.nhooo.example.jackson; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import java.io.IOException; import java.time.LocalDate; public class LocalDateDeserializer extends StdDeserializer<LocalDate> { protected LocalDateDeserializer() { super(LocalDate.class); } @Override public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException { return LocalDate.parse(parser.readValueAs(String.class)); } }
让我们创建一个简单的类,将Recording对象转换为JSON字符串并应用LocalDateSerializer该类中定义的日期格式程序。
package org.nhooo.example.jackson; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.time.LocalDate; import java.time.Month; public class RecordingToJson { public static void main(String[] args) { Recording recording = new Recording(); recording.setId(1L); recording.setTitle("Twist and Shout"); recording.setReleaseDate(LocalDate.of(1964, Month.FEBRUARY, 3)); ObjectMapper mapper = new ObjectMapper(); try { String json = mapper.writeValueAsString(recording); System.out.println("JSON = " + json); } catch (JsonProcessingException e) { e.printStackTrace(); } } }
上面的代码片段的输出是:
JSON = {"id":1,"title":"Twist and Shout","releaseDate":"1964-02-03","artist":null,"label":null}
这是Recording该类的完整定义。
package org.nhooo.example.jackson; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.time.LocalDate; import java.util.Objects; public class Recording { private Long id; private String title; @JsonDeserialize(using = LocalDateDeserializer.class) @JsonSerialize(using = LocalDateSerializer.class) private LocalDate releaseDate; private Artist artist; private Label label; public Recording() { } public Recording(Long id, String title, LocalDate releaseDate) { this.id = id; this.title = title; this.releaseDate = releaseDate; } //吸气剂和二传手 }
Maven依赖
<!-- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6.jar --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.6</version> </dependency>