如何动态自定义日期格式的反序列化器?

我正在研究自定义JSON反序列化器并拥有以下课程


public class yyyy_MM_dd_DateDeserializer extends StdDeserializer <LocalDate> {


 public yyyy_MM_dd_DateDeserializer() {

  this(null);

 }


 public yyyy_MM_dd_DateDeserializer(Class t) {

  super(t);

 }


 @Override

 public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

  String dateString = jsonParser.getText();

  LocalDate localDate = null;

  try {

   localDate = LocalDate.parse(dateString, "yyyy-MM-dd");

  } catch (DateTimeParseException ex) {

   throw new RuntimeException("Unparsable date: " + dateString);

  }

  return localDate;

 }

}

在我的请求课程中


@Valid

@JsonDeserialize(using = LocalDateDeserializer.class)

@JsonSerialize(using = LocalDateSerializer.class)

private LocalDate endDate;

它工作正常,但我想知道我是否可以动态传递日期格式。而不是硬编码在yyyy_MM_dd_DateDeserializer. 我想从我的请求类中传递日期格式,以便我的反序列化器更通用,任何人都可以通过发送所需的格式来使用它。


茅侃侃
浏览 131回答 3
3回答

HUWWW

我认为你工作太努力了,没有得到你想要的。有一种更简单的方法,无需编写自己的反序列化器。看看这个问题。本质上它看起来像@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="EEE MMM dd HH:mm:ss Z yyyy")@JsonProperty("created_at")&nbsp;ZonedDateTime created_at;而你只是戴上你自己的面具。另外,我曾经有一个任务是解析未知格式的日期,本质上我需要解析任何有效的日期。这是一篇描述如何实现它的想法的文章:Java 8 java.time package: parsing any string to date。你可能会发现它很有用

墨色风雨

不是在使用活页夹库时(绑定的关键在于它不是动态的。)。但是你可以在使用简单的解析库时,比如 org.json

繁花不似锦

当您使用java.time.*类并且Jackson最好从JavaTimeModule来自jackson-datatype-jsr310模块的注册开始。我们可以扩展它并使用提供的模式注册序列化程序,如下例所示:import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;import java.time.LocalDate;import java.time.format.DateTimeFormatter;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapperIso = createObjectMapper("yyyy-MM-dd");&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapperCustom0 = createObjectMapper("yyyy/MM/dd");&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapperCustom1 = createObjectMapper("MM-dd-yyyy");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapperIso.writeValueAsString(new Time()));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapperCustom0.writeValueAsString(new Time()));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapperCustom1.writeValueAsString(new Time()));&nbsp; &nbsp; }&nbsp; &nbsp; private static ObjectMapper createObjectMapper(String pattern) {&nbsp; &nbsp; &nbsp; &nbsp; JavaTimeModule javaTimeModule = new JavaTimeModule();&nbsp; &nbsp; &nbsp; &nbsp; javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(pattern)));&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; mapper.registerModule(javaTimeModule);&nbsp; &nbsp; &nbsp; &nbsp; return mapper;&nbsp; &nbsp; }}class Time {&nbsp; &nbsp; private LocalDate now = LocalDate.now();&nbsp; &nbsp; public LocalDate getNow() {&nbsp; &nbsp; &nbsp; &nbsp; return now;&nbsp; &nbsp; }&nbsp; &nbsp; public void setNow(LocalDate now) {&nbsp; &nbsp; &nbsp; &nbsp; this.now = now;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Time{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "now=" + now +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}Aboce 代码打印:{"now":"2019-02-24"}{"now":"2019/02/24"}{"now":"02-24-2019"}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java