2011-11-21 07:23:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.google.gson;
|
|
|
|
|
2017-05-31 03:12:50 +02:00
|
|
|
import java.io.IOException;
|
2011-11-21 07:23:42 +01:00
|
|
|
import java.sql.Timestamp;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.ParseException;
|
2015-11-23 07:11:41 +01:00
|
|
|
import java.text.ParsePosition;
|
2011-11-21 07:23:42 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
2017-12-29 20:14:43 +01:00
|
|
|
import java.util.ArrayList;
|
2011-11-21 07:23:42 +01:00
|
|
|
import java.util.Date;
|
2017-12-29 20:14:43 +01:00
|
|
|
import java.util.List;
|
2011-11-21 07:23:42 +01:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2018-05-09 22:10:08 +02:00
|
|
|
import com.google.gson.internal.JavaVersion;
|
2017-12-29 20:14:43 +01:00
|
|
|
import com.google.gson.internal.PreJava9DateFormatProvider;
|
2015-11-23 07:11:41 +01:00
|
|
|
import com.google.gson.internal.bind.util.ISO8601Utils;
|
2017-05-31 03:12:50 +02:00
|
|
|
import com.google.gson.stream.JsonReader;
|
|
|
|
import com.google.gson.stream.JsonToken;
|
|
|
|
import com.google.gson.stream.JsonWriter;
|
2015-11-23 07:11:41 +01:00
|
|
|
|
2011-11-21 07:23:42 +01:00
|
|
|
/**
|
|
|
|
* This type adapter supports three subclasses of date: Date, Timestamp, and
|
|
|
|
* java.sql.Date.
|
|
|
|
*
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
2020-02-12 19:25:56 +01:00
|
|
|
final class DefaultDateTypeAdapter<T extends Date> extends TypeAdapter<T> {
|
2017-03-20 22:25:52 +01:00
|
|
|
private static final String SIMPLE_NAME = "DefaultDateTypeAdapter";
|
2017-05-31 03:12:50 +02:00
|
|
|
|
2020-02-12 19:25:56 +01:00
|
|
|
static abstract class DateType<T extends Date> {
|
|
|
|
private DateType() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateType<Date> DATE = new DateType<Date>() {
|
|
|
|
@Override
|
|
|
|
protected Date deserialize(Date date) {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
public static final DateType<java.sql.Date> SQL_DATE = new DateType<java.sql.Date>() {
|
|
|
|
@Override
|
|
|
|
protected java.sql.Date deserialize(Date date) {
|
|
|
|
return new java.sql.Date(date.getTime());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
public static final DateType<Timestamp> SQL_TIMESTAMP = new DateType<Timestamp>() {
|
|
|
|
@Override
|
|
|
|
protected Timestamp deserialize(Date date) {
|
|
|
|
return new Timestamp(date.getTime());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
protected abstract T deserialize(Date date);
|
|
|
|
|
|
|
|
public DefaultDateTypeAdapter<T> createAdapter(String datePattern) {
|
|
|
|
return new DefaultDateTypeAdapter<T>(this, datePattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
public DefaultDateTypeAdapter<T> createAdapter(int style) {
|
|
|
|
return new DefaultDateTypeAdapter<T>(this, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
public DefaultDateTypeAdapter<T> createAdapter(int dateStyle, int timeStyle) {
|
|
|
|
return new DefaultDateTypeAdapter<T>(this, dateStyle, timeStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public DefaultDateTypeAdapter<T> createDefaultsAdapter() {
|
|
|
|
return new DefaultDateTypeAdapter<T>(this, DateFormat.DEFAULT, DateFormat.DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final DateType<T> dateType;
|
2017-12-29 20:14:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of 1 or more different date formats used for de-serialization attempts.
|
|
|
|
* The first of them is used for serialization as well.
|
|
|
|
*/
|
|
|
|
private final List<DateFormat> dateFormats = new ArrayList<DateFormat>();
|
|
|
|
|
2020-02-12 19:25:56 +01:00
|
|
|
private DefaultDateTypeAdapter(DateType<T> dateType, String datePattern) {
|
2017-12-29 20:14:43 +01:00
|
|
|
this.dateType = verifyDateType(dateType);
|
|
|
|
dateFormats.add(new SimpleDateFormat(datePattern, Locale.US));
|
|
|
|
if (!Locale.getDefault().equals(Locale.US)) {
|
|
|
|
dateFormats.add(new SimpleDateFormat(datePattern));
|
|
|
|
}
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 19:25:56 +01:00
|
|
|
private DefaultDateTypeAdapter(DateType<T> dateType, int style) {
|
2017-12-29 20:14:43 +01:00
|
|
|
this.dateType = verifyDateType(dateType);
|
|
|
|
dateFormats.add(DateFormat.getDateInstance(style, Locale.US));
|
|
|
|
if (!Locale.getDefault().equals(Locale.US)) {
|
|
|
|
dateFormats.add(DateFormat.getDateInstance(style));
|
|
|
|
}
|
2018-05-09 22:10:08 +02:00
|
|
|
if (JavaVersion.isJava9OrLater()) {
|
2017-12-29 20:14:43 +01:00
|
|
|
dateFormats.add(PreJava9DateFormatProvider.getUSDateFormat(style));
|
|
|
|
}
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 19:25:56 +01:00
|
|
|
private DefaultDateTypeAdapter(DateType<T> dateType, int dateStyle, int timeStyle) {
|
2017-12-29 20:14:43 +01:00
|
|
|
this.dateType = verifyDateType(dateType);
|
|
|
|
dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US));
|
|
|
|
if (!Locale.getDefault().equals(Locale.US)) {
|
|
|
|
dateFormats.add(DateFormat.getDateTimeInstance(dateStyle, timeStyle));
|
|
|
|
}
|
2018-05-09 22:10:08 +02:00
|
|
|
if (JavaVersion.isJava9OrLater()) {
|
2017-12-29 20:14:43 +01:00
|
|
|
dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(dateStyle, timeStyle));
|
|
|
|
}
|
2017-05-31 03:12:50 +02:00
|
|
|
}
|
|
|
|
|
2020-02-12 19:25:56 +01:00
|
|
|
private static <T extends Date> DateType<T> verifyDateType(DateType<T> dateType) {
|
|
|
|
if (dateType == null) {
|
|
|
|
throw new NullPointerException("dateType == null");
|
2017-05-31 03:12:50 +02:00
|
|
|
}
|
2017-12-29 20:14:43 +01:00
|
|
|
return dateType;
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// These methods need to be synchronized since JDK DateFormat classes are not thread-safe
|
|
|
|
// See issue 162
|
2015-11-05 03:52:20 +01:00
|
|
|
@Override
|
2017-05-31 03:12:50 +02:00
|
|
|
public void write(JsonWriter out, Date value) throws IOException {
|
2017-09-18 08:49:13 +02:00
|
|
|
if (value == null) {
|
|
|
|
out.nullValue();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 20:14:43 +01:00
|
|
|
synchronized(dateFormats) {
|
|
|
|
String dateFormatAsString = dateFormats.get(0).format(value);
|
2017-05-31 03:12:50 +02:00
|
|
|
out.value(dateFormatAsString);
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:52:20 +01:00
|
|
|
@Override
|
2020-02-12 19:25:56 +01:00
|
|
|
public T read(JsonReader in) throws IOException {
|
2017-09-18 08:49:13 +02:00
|
|
|
if (in.peek() == JsonToken.NULL) {
|
|
|
|
in.nextNull();
|
|
|
|
return null;
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
2017-05-31 03:12:50 +02:00
|
|
|
Date date = deserializeToDate(in.nextString());
|
2020-02-12 19:25:56 +01:00
|
|
|
return dateType.deserialize(date);
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 03:12:50 +02:00
|
|
|
private Date deserializeToDate(String s) {
|
2017-12-29 20:14:43 +01:00
|
|
|
synchronized (dateFormats) {
|
|
|
|
for (DateFormat dateFormat : dateFormats) {
|
|
|
|
try {
|
|
|
|
return dateFormat.parse(s);
|
|
|
|
} catch (ParseException ignored) {}
|
|
|
|
}
|
2011-11-21 07:23:42 +01:00
|
|
|
try {
|
2017-05-31 03:12:50 +02:00
|
|
|
return ISO8601Utils.parse(s, new ParsePosition(0));
|
2011-11-21 07:23:42 +01:00
|
|
|
} catch (ParseException e) {
|
2017-05-31 03:12:50 +02:00
|
|
|
throw new JsonSyntaxException(s, e);
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2017-12-29 20:14:43 +01:00
|
|
|
DateFormat defaultFormat = dateFormats.get(0);
|
|
|
|
if (defaultFormat instanceof SimpleDateFormat) {
|
|
|
|
return SIMPLE_NAME + '(' + ((SimpleDateFormat) defaultFormat).toPattern() + ')';
|
|
|
|
} else {
|
|
|
|
return SIMPLE_NAME + '(' + defaultFormat.getClass().getSimpleName() + ')';
|
|
|
|
}
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
}
|