eclipse-ee4j / eclipse-ee4j/yasson

ZonedDateTime and OffsetDateTime JsonbConfig withStrictIJSON(true)

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

Description

When configured to use StrictIJson, ZonedDateTime and OffsetDateTime are serialized with "Z" plus the time zone offset when the time is not UTC. Conversely, de-serializing requires "Z" even for times not in UTC.

The way I interpret https://datatracker.ietf.org/doc/html/rfc3339#section-5.6 is that if the time is in UTC (e.g "2021-09-14T15:59:50Z", then "Z" could be specified (or alternatively "+00:00"), and if the time is not in UTC then only the offset should be specified (e.g. "2021-09-14T15:59:50-04:00".

**To Reproduce**

public static void main(String... args) {
JsonbConfig config = new JsonbConfig().withStrictIJSON(true);
Jsonb jsonb = JsonbBuilder.create(config);
Foo foo = new Foo(ZonedDateTime.now(), OffsetDateTime.now());

String serialized = jsonb.toJson(foo);
boolean containsZ = serialized.contains("Z");
if (containsZ) {
System.out.println("INCORRECT: Serialized values contains 'Z' plus timezone offset: " + serialized);
} else {
System.out.println("CORRECT: Serialized values contains only timezone offset: " + serialized);
}

try {
String fooString1 = "{\"offset\":\"2021-09-14T15:59:50Z-04:00\",\"zoned\":\"2021-09-14T15:59:50Z-04:00\"}";
jsonb.fromJson(fooString1, Foo.class);
System.out.println("INCORRECT: deserialized values including 'Z' plus timezone offset");
} catch (Exception e) {
System.out.println("CORRECT: deserialized values including 'Z' plus timezone offset: " + e.getMessage());
}

try {
String fooString2 = "{\"offset\":\"2021-09-14T15:59:50-04:00\",\"zoned\":\"2021-09-14T15:59:50-04:00\"}";
jsonb.fromJson(fooString2, Foo.class);
System.out.println("CORRECT: deserialized values including only timezone offset");
} catch (Exception e) {
System.out.println("INCORRRECT: deserializing values including only timezone offset: " + e.getMessage());
}

try {
ZonedDateTime.parse("2021-09-14T15:59:50Z-04:00");
System.out.println("INCORRECT: direct parsing value including 'Z' plus timezone offset");
} catch (Exception e) {
System.out.println("CORRECT: direct parsing datetime including 'Z' plus timezone offset: " + e.getMessage());
}

try {
ZonedDateTime.parse("2021-09-14T15:59:50-04:00");
System.out.println("CORRECT: direct parsing value including only timezone offset");
} catch (Exception e) {
System.out.println("INCORRECT: direct parsing value including only timezone offset: " + e.getMessage());
}

}

public class Foo {

private ZonedDateTime zoned;
private OffsetDateTime offset;

public Foo(ZonedDateTime zoned, OffsetDateTime offset) {
this.zoned = zoned;
this.offset = offset;
}

public Foo() {
}

public ZonedDateTime getZoned() {
return zoned;
}

public void setZoned(ZonedDateTime zoned) {
this.zoned = zoned;
}

public OffsetDateTime getOffset() {
return offset;
}

public void setOffset(OffsetDateTime offset) {
this.offset = offset;
}

}

**Expected behavior**
Example serialization ZonedDateTime / OffsetDateTime with withStrictIJSON(true).
Expected: 2021-09-14T15:59:50-04:00
Actual: 2021-09-14T15:59:50Z-04:00

Example de-serialization of "2021-09-14T15:59:50-04:00".
Expected: ZoneDateTime and OffsetDateTime representing input string
Actual: DateTimeParseException

Example de-serialization of "2021-09-14T15:59:50Z-04:00".
Expected: DateTimeParseException
Actual: ZoneDateTime and OffsetDateTime representing input string

**System information:**
- OS: any
- Java 11
- Yasson Version: 1.0.5, 1.0.9

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with JsonbConfig.withStrictIJSON(true) using ZonedDateTime and OffsetDateTime, then inspect Yasson's date-time serialization and deserialization paths. Compare the handling of UTC and non-UTC offsets against the RFC 3339 examples in the issue; done means offset-only values round-trip and values containing both Z and an offset are rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.