Skip to content

Commit bf6336f

Browse files
authored
Fix temporal conversion for Json objects (#1396)
See #1323 Convert the temporal value to String using the same formatter as JsonObject. Then the value can always be read from Json as Instant, even when some parts are missing (hours, minutes, seconds...) Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 62fe19b commit bf6336f

File tree

2 files changed

+23
-17
lines changed
  • vertx-pg-client/src/test/java/io/vertx/pgclient
  • vertx-sql-client/src/main/java/io/vertx/sqlclient/impl

2 files changed

+23
-17
lines changed

vertx-pg-client/src/test/java/io/vertx/pgclient/RowTest.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,20 @@
1616
*/
1717
package io.vertx.pgclient;
1818

19+
import io.vertx.core.Vertx;
1920
import io.vertx.core.buffer.Buffer;
2021
import io.vertx.core.json.JsonArray;
2122
import io.vertx.core.json.JsonObject;
22-
import io.vertx.pgclient.data.Box;
23-
import io.vertx.pgclient.data.Circle;
24-
import io.vertx.pgclient.data.Interval;
25-
import io.vertx.pgclient.data.Line;
26-
import io.vertx.pgclient.data.LineSegment;
27-
import io.vertx.sqlclient.data.Numeric;
28-
import io.vertx.pgclient.data.Path;
29-
import io.vertx.pgclient.data.Point;
30-
import io.vertx.pgclient.data.Polygon;
31-
import io.vertx.sqlclient.Row;
32-
import io.vertx.core.Vertx;
3323
import io.vertx.ext.unit.Async;
3424
import io.vertx.ext.unit.TestContext;
25+
import io.vertx.pgclient.data.*;
26+
import io.vertx.sqlclient.Row;
3527
import org.junit.After;
3628
import org.junit.Before;
3729
import org.junit.Test;
3830

3931
import java.lang.reflect.Array;
32+
import java.time.*;
4033
import java.util.Arrays;
4134
import java.util.Base64;
4235
import java.util.List;
@@ -225,11 +218,13 @@ public void testToJsonObject(TestContext ctx) {
225218
"'[\"baz\",7,false]'::json \"json_array\"," +
226219
"E'\\\\x010203'::bytea \"buffer\"," +
227220
"'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid \"uuid\"," +
228-
"ARRAY[1, 2, 3] \"array\""
221+
"ARRAY[1, 2, 3] \"array\"," +
222+
"'2020-01-01'::TIMESTAMPTZ \"timestamp\""
229223
).execute(
230224
ctx.asyncAssertSuccess(result -> {
231225
Row row = result.iterator().next();
232226
JsonObject json = row.toJson();
227+
OffsetDateTime tz = OffsetDateTime.of(LocalDateTime.of(LocalDate.of(2020, 1, 1), LocalTime.MIDNIGHT), ZoneOffset.UTC);
233228
ctx.assertEquals((short)2, json.getValue("small_int"));
234229
ctx.assertEquals(2, json.getValue("integer"));
235230
ctx.assertEquals(2L, json.getValue("bigint"));
@@ -247,6 +242,7 @@ public void testToJsonObject(TestContext ctx) {
247242
ctx.assertEquals(new String(Base64.getEncoder().encode(new byte[]{1,2,3})), json.getValue("buffer"));
248243
ctx.assertEquals("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", json.getValue("uuid"));
249244
ctx.assertEquals(new JsonArray().add(1).add(2).add(3), json.getValue("array"));
245+
ctx.assertEquals(tz, json.getInstant("timestamp").atOffset(ZoneOffset.UTC));
250246
async.complete();
251247
}));
252248
}));

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/Utils.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import io.vertx.core.json.JsonObject;
77

88
import java.lang.reflect.Array;
9+
import java.time.format.DateTimeFormatter;
10+
import java.time.temporal.ChronoField;
11+
import java.time.temporal.Temporal;
912
import java.util.ArrayList;
1013
import java.util.List;
1114
import java.util.concurrent.atomic.AtomicLong;
@@ -24,24 +27,31 @@ private Utils() {
2427
public static Object toJson(Object value) {
2528
if (value == null || value == JSON_NULL) {
2629
return null;
27-
} else if (value instanceof String
30+
}
31+
if (value instanceof String
2832
|| value instanceof Boolean
2933
|| value instanceof Number
3034
|| value instanceof Buffer
3135
|| value instanceof JsonObject
3236
|| value instanceof JsonArray) {
3337
return value;
34-
} else if (value.getClass().isArray()) {
38+
}
39+
if (value.getClass().isArray()) {
3540
int len = Array.getLength(value);
3641
JsonArray array = new JsonArray(new ArrayList<>(len));
37-
for (int idx = 0;idx < len;idx++) {
42+
for (int idx = 0; idx < len; idx++) {
3843
Object component = toJson(Array.get(value, idx));
3944
array.add(component);
4045
}
4146
return array;
42-
} else {
43-
return value.toString();
4447
}
48+
if (value instanceof Temporal) {
49+
Temporal temporal = (Temporal) value;
50+
if (temporal.isSupported(ChronoField.INSTANT_SECONDS)) {
51+
return DateTimeFormatter.ISO_INSTANT.format(temporal);
52+
}
53+
}
54+
return value.toString();
4555
}
4656

4757
public static <T> Supplier<Future<T>> roundRobinSupplier(List<T> factories) {

0 commit comments

Comments
 (0)