eclipse-ee4j / eclipse-ee4j/yasson

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

Open
#653 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
218
Forks
109
Avg merge
1d 5h
Merged PRs (30d)
9

Description

**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:**
- OS: windows, IBM-I
- Java Version: 15
- Yasson Version: 3.0.? - could not pin down the exact version used by Liberty Server 24.0.0.9

**Additional context**
The javadoc of the used [DateTimeFormatter.ISO_DATE_TIME]( https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/time/format/DateTimeFormatter.html#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");`

Contributor guide

Open the contributing guide

Research direction

Review DateSerializer.java around line 28 and compare the formatter's use of UTC with the reported ZoneOffset.UTC alternative. Verify that serialization of a java.util.Date produces the expected ISO-8601 string without the [UTC] suffix.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.