Skip to content

Commit 1135b43

Browse files
committed
fix: avoid requirements to jdk.unsupported module
1 parent 73b1def commit 1135b43

File tree

1 file changed

+62
-0
lines changed
  • client/src/generated/java/com/influxdb/client

1 file changed

+62
-0
lines changed

client/src/generated/java/com/influxdb/client/JSON.java

+62
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
public class JSON {
4848
private Gson gson;
4949
private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
50+
private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
5051
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
5152
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
5253

@@ -173,6 +174,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
173174
public JSON() {
174175
gson = createGson()
175176
.registerTypeAdapter(Date.class, dateTypeAdapter)
177+
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
176178
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
177179
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
178180
.create();
@@ -302,6 +304,60 @@ public JSON setLocalDateFormat(DateTimeFormatter dateFormat) {
302304
return this;
303305
}
304306

307+
/**
308+
* Gson TypeAdapter for java.sql.Date type
309+
* If the dateFormat is null, a simple "yyyy-MM-dd" format will be used
310+
* (more efficient than SimpleDateFormat).
311+
*/
312+
public static class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
313+
314+
private DateFormat dateFormat;
315+
316+
public SqlDateTypeAdapter() {
317+
}
318+
319+
public SqlDateTypeAdapter(DateFormat dateFormat) {
320+
this.dateFormat = dateFormat;
321+
}
322+
323+
public void setFormat(DateFormat dateFormat) {
324+
this.dateFormat = dateFormat;
325+
}
326+
327+
@Override
328+
public void write(JsonWriter out, java.sql.Date date) throws IOException {
329+
if (date == null) {
330+
out.nullValue();
331+
} else {
332+
String value;
333+
if (dateFormat != null) {
334+
value = dateFormat.format(date);
335+
} else {
336+
value = date.toString();
337+
}
338+
out.value(value);
339+
}
340+
}
341+
342+
@Override
343+
public java.sql.Date read(JsonReader in) throws IOException {
344+
switch (in.peek()) {
345+
case NULL:
346+
in.nextNull();
347+
return null;
348+
default:
349+
String date = in.nextString();
350+
try {
351+
if (dateFormat != null) {
352+
return new java.sql.Date(dateFormat.parse(date).getTime());
353+
}
354+
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
355+
} catch (ParseException e) {
356+
throw new JsonParseException(e);
357+
}
358+
}
359+
}
360+
}
305361

306362
/**
307363
* Gson TypeAdapter for java.util.Date type
@@ -365,4 +421,10 @@ public JSON setDateFormat(DateFormat dateFormat) {
365421
dateTypeAdapter.setFormat(dateFormat);
366422
return this;
367423
}
424+
425+
public JSON setSqlDateFormat(DateFormat dateFormat) {
426+
sqlDateTypeAdapter.setFormat(dateFormat);
427+
return this;
428+
}
429+
368430
}

0 commit comments

Comments
 (0)