diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java index f14f5f53..adab5bea 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java @@ -35,10 +35,7 @@ import java.nio.file.Path; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; +import java.time.*; import java.util.*; import java.util.regex.Pattern; @@ -79,6 +76,7 @@ static Gson gson() { static final Gson jsonDataSerializer = new GsonBuilder().disableHtmlEscaping() .registerTypeAdapter(Date.class, new DateSerializer()) .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()) + .registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeSerializer()) .serializeNulls().create(); static SerializedError serializeError(Throwable e) { @@ -519,6 +517,13 @@ public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext } } + private static class OffsetDateTimeSerializer implements JsonSerializer { + @Override + public JsonElement serialize(OffsetDateTime src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(dateFormat.format(Date.from(src.toInstant()))); + } + } + private static class LocalDateTimeSerializer implements JsonSerializer { @Override public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) { diff --git a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextFetch.java b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextFetch.java index ed38aaf9..eb831612 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextFetch.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestBrowserContextFetch.java @@ -28,6 +28,7 @@ import java.nio.file.Path; import java.text.ParseException; import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.time.ZoneId; import java.util.*; import java.util.concurrent.ExecutionException; @@ -520,6 +521,16 @@ void shouldSerializeDateAndLocalDateTime() throws ExecutionException, Interrupte assertEquals("{\"name\":\"foo\",\"localDateTime\":\"2022-12-23T06:14:58.818Z\",\"date\":\"2022-12-23T06:14:58.818Z\",\"nullLocalDateTime\":null,\"nullDate\":null}", new String(req.get().postBody)); } + + @Test + void shouldSupportOffsetDateTimeInData() throws ExecutionException, InterruptedException { + APIRequestContext request = playwright.request().newContext(); + OffsetDateTime offsetDateTime = OffsetDateTime.parse("2024-07-10T10:15:30-08:00"); + Future serverRequest = server.futureRequest("/empty.html"); + request.get(server.EMPTY_PAGE, RequestOptions.create().setData(mapOf("date", offsetDateTime))); + byte[] body = serverRequest.get().postBody; + assertEquals("{\"date\":\"2024-07-10T18:15:30.000Z\"}", new String(body)); + } @Test void shouldSupportApplicationXWwwFormUrlencoded() throws ExecutionException, InterruptedException {