Skip to content

ZonedDateTime and OffsetDateTime JsonbConfig withStrictIJSON(true) #514

@JohnSchneider

Description

@JohnSchneider

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions