|
47 | 47 | public class JSON {
|
48 | 48 | private Gson gson;
|
49 | 49 | private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
|
| 50 | + private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); |
50 | 51 | private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
|
51 | 52 | private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
|
52 | 53 |
|
@@ -173,6 +174,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
|
173 | 174 | public JSON() {
|
174 | 175 | gson = createGson()
|
175 | 176 | .registerTypeAdapter(Date.class, dateTypeAdapter)
|
| 177 | + .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter) |
176 | 178 | .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
|
177 | 179 | .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
|
178 | 180 | .create();
|
@@ -302,6 +304,60 @@ public JSON setLocalDateFormat(DateTimeFormatter dateFormat) {
|
302 | 304 | return this;
|
303 | 305 | }
|
304 | 306 |
|
| 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 | + } |
305 | 361 |
|
306 | 362 | /**
|
307 | 363 | * Gson TypeAdapter for java.util.Date type
|
@@ -365,4 +421,10 @@ public JSON setDateFormat(DateFormat dateFormat) {
|
365 | 421 | dateTypeAdapter.setFormat(dateFormat);
|
366 | 422 | return this;
|
367 | 423 | }
|
| 424 | + |
| 425 | + public JSON setSqlDateFormat(DateFormat dateFormat) { |
| 426 | + sqlDateTypeAdapter.setFormat(dateFormat); |
| 427 | + return this; |
| 428 | + } |
| 429 | + |
368 | 430 | }
|
0 commit comments