eclipse-ee4j / yasson

Eclipse Yasson project
https://projects.eclipse.org/projects/ee4j.yasson
Other
204 stars 96 forks source link

Default Serialization of java.util.Date does not generate ISO-8601 conform string #653

Open RobertK66 opened 1 day ago

RobertK66 commented 1 day ago

Describe the bug Using out of box configuration. (jsonb-3.0 feature of Liberty server 24.0.0.9) for REST services delivering a JSON record gives the following string value for a java.util.Date field:

"2024-11-06T16:06:15.168Z[UTC]"

This string having a "[UTC]" postfix showing (a assumed) UTC/+0 ZoneID in textual form is not ISO-8601 conform. It does raise exceptions when you try to deserialize this with current up to date dotnet JSON deserializer.

To Reproduce use java.util.Date fields in REST response content without any annotations or configuration of the jsonb implementation.

Expected behavior The serialized string should be ISO-8601 conform:

"2024-11-06T16:06:15.168Z"

System information:

Additional context The javadoc of the used DateTimeFormatter.ISO_DATE_TIME states that it is only 'ISO-Like'. As described there, you can avoid to generate the non ISO standard square brackets by using a "ZoneOffset" instead of a "zoneID".

This would be an easy fix by changing the line 28 of the class DateSerializer.java from:

private static final DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ISO_DATE_TIME.withZone(UTC);

to:

private static final DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.UTC);

This makes a difference here because the const UTC in the current code is referencing a ZoneID by:

static final ZoneId UTC = ZoneId.of("UTC");

greek1979 commented 20 hours ago

I've seen this issue before, too, and fully concur that a fix is needed...