OffsetDateTime支持两种类型的json格式模式。

huangapple go评论69阅读模式
英文:

OffsetDateTime jsonFormat allowing two type of patterns

问题

我正在使用Spring Boot实现应用程序,使用Jackson。

有没有办法为解析两种类型的日期指定JsonFormat,比如:

  • 2020-10-06T10:15:30+01:00
  • 2020-10-06T10:15:30

我当前的字段:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss Z")
private OffsetDateTime interval;

我需要在配置中的某个地方指定ObjectMapper吗?

我得到了以下错误:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type 'java.time.OffsetDateTime' from String "2020-10-06T10:15:30+01:00": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2020-10-06T10:15:30+01:00' could not be parsed at index 10

谢谢。

英文:

I am implementing app in Spring Boot. Using Jackson.

Is there anyway to specify JsonFormat for parsing two types of date like:

  • 2020-10-06T10:15:30+01:00
  • 2020-10-06T10:15:30

My current field:

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss Z")
  private OffsetDateTime interval;

Do I have to somewhere specify some ObjectMapper in configuration?

I am retrieving this error:

 com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2020-10-06T10:15:30+01:00": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2020-10-06T10:15:30+01:00' could not be parsed at index 10

Regards

答案1

得分: 3

代码部分不提供翻译。以下为翻译好的内容:

你需要使用带有可选区域偏移的 DateTimeFormatter 进行解析,因此你需要一个自定义的 JsonDeserializer

Test

public class Test {

	@JsonDeserialize(using = OffsetDateTimeOptionalZoneDeserializer.class)
	private OffsetDateTime interval;

	public static void main(String[] args) throws Exception {
		String json = "["
		                + "{ \"interval\": \"2020-10-06T10:15:30+01:00\" },"
		                + "{ \"interval\": \"2020-10-06T10:15:30Z\" },"
		                + "{ \"interval\": \"2020-10-06T10:15:30\" }"
		              + "]";
		
		ObjectMapper mapper = new ObjectMapper();
		Test[] tests = mapper.readValue(json, Test[].class);
		for (Test t : tests)
			System.out.println(t.interval);
	}
}

Output

2020-10-06T10:15:30+01:00
2020-10-06T10:15:30Z
2020-10-06T10:15:30Z
英文:

You need to parse using a DateTimeFormatter with optional zone offset, so you need a custom JsonDeserializer:

public class OffsetDateTimeOptionalZoneDeserializer extends StdScalarDeserializer<OffsetDateTime> {
	private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
			.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
			.optionalStart()
			.appendOffsetId()
			.optionalEnd()
			.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
			.toFormatter();

	public OffsetDateTimeOptionalZoneDeserializer() { super(OffsetDateTime.class); }

	@Override
	public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
		return OffsetDateTime.parse(p.getText(), formatter);
	}
}

Test

public class Test {

	@JsonDeserialize(using = OffsetDateTimeOptionalZoneDeserializer.class)
	private OffsetDateTime interval;

	public static void main(String[] args) throws Exception {
		String json = "[" +
		                "{ \"interval\": \"2020-10-06T10:15:30+01:00\" }," +
		                "{ \"interval\": \"2020-10-06T10:15:30Z\" }," +
		                "{ \"interval\": \"2020-10-06T10:15:30\" }" +
		              "]";
		
		ObjectMapper mapper = new ObjectMapper();
		Test[] tests = mapper.readValue(json, Test[].class);
		for (Test t : tests)
			System.out.println(t.interval);
	}
}

Output

2020-10-06T10:15:30+01:00
2020-10-06T10:15:30Z
2020-10-06T10:15:30Z

huangapple
  • 本文由 发表于 2020年10月6日 21:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64226459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定