2011-09-26 23:44:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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.internal.bind;
|
|
|
|
|
2011-11-20 16:23:08 +01:00
|
|
|
import com.google.gson.Gson;
|
2011-09-26 23:44:08 +02:00
|
|
|
import com.google.gson.JsonSyntaxException;
|
2011-11-20 16:23:08 +01:00
|
|
|
import com.google.gson.TypeAdapter;
|
2011-12-23 19:27:13 +01:00
|
|
|
import com.google.gson.TypeAdapterFactory;
|
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;
|
2011-09-26 23:44:08 +02:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.google.gson.stream.JsonReader;
|
2011-10-14 05:17:12 +02:00
|
|
|
import com.google.gson.stream.JsonToken;
|
2011-09-26 23:44:08 +02:00
|
|
|
import com.google.gson.stream.JsonWriter;
|
2017-12-29 20:14:43 +01:00
|
|
|
|
2011-09-26 23:44:08 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.ParseException;
|
2015-11-23 07:11:41 +01:00
|
|
|
import java.text.ParsePosition;
|
2017-12-29 20:14:43 +01:00
|
|
|
import java.util.ArrayList;
|
2011-09-26 23:44:08 +02:00
|
|
|
import java.util.Date;
|
2017-12-29 20:14:43 +01:00
|
|
|
import java.util.List;
|
2011-09-26 23:44:08 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
2011-09-28 19:56:54 +02:00
|
|
|
* Adapter for Date. Although this class appears stateless, it is not.
|
2011-09-26 23:44:08 +02:00
|
|
|
* DateFormat captures its time zone and locale when it is created, which gives
|
|
|
|
* this class state. DateFormat isn't thread safe either, so this class has
|
|
|
|
* to synchronize its read and write methods.
|
|
|
|
*/
|
|
|
|
public final class DateTypeAdapter extends TypeAdapter<Date> {
|
2011-12-23 19:27:13 +01:00
|
|
|
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
|
2011-09-26 23:44:08 +02:00
|
|
|
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
2015-11-05 03:52:20 +01:00
|
|
|
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
2011-09-26 23:44:08 +02:00
|
|
|
return typeToken.getRawType() == Date.class ? (TypeAdapter<T>) new DateTypeAdapter() : null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 (default US format) is used for serialization as well.
|
|
|
|
*/
|
|
|
|
private final List<DateFormat> dateFormats = new ArrayList<DateFormat>();
|
|
|
|
|
|
|
|
public DateTypeAdapter() {
|
|
|
|
dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US));
|
|
|
|
if (!Locale.getDefault().equals(Locale.US)) {
|
|
|
|
dateFormats.add(DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT));
|
|
|
|
}
|
2018-05-09 22:10:08 +02:00
|
|
|
if (JavaVersion.isJava9OrLater()) {
|
2017-12-29 20:14:43 +01:00
|
|
|
dateFormats.add(PreJava9DateFormatProvider.getUSDateTimeFormat(DateFormat.DEFAULT, DateFormat.DEFAULT));
|
|
|
|
}
|
|
|
|
}
|
2011-09-26 23:44:08 +02:00
|
|
|
|
2011-12-03 20:46:25 +01:00
|
|
|
@Override public Date read(JsonReader in) throws IOException {
|
|
|
|
if (in.peek() == JsonToken.NULL) {
|
|
|
|
in.nextNull();
|
2011-10-14 05:17:12 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-11-01 23:08:04 +01:00
|
|
|
return deserializeToDate(in);
|
2011-09-26 23:44:08 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 23:08:04 +01:00
|
|
|
private Date deserializeToDate(JsonReader in) throws IOException {
|
|
|
|
String s = in.nextString();
|
|
|
|
synchronized (dateFormats) {
|
|
|
|
for (DateFormat dateFormat : dateFormats) {
|
|
|
|
try {
|
|
|
|
return dateFormat.parse(s);
|
|
|
|
} catch (ParseException ignored) {}
|
|
|
|
}
|
2011-09-26 23:44:08 +02:00
|
|
|
}
|
|
|
|
try {
|
2021-11-01 23:08:04 +01:00
|
|
|
return ISO8601Utils.parse(s, new ParsePosition(0));
|
2011-09-26 23:44:08 +02:00
|
|
|
} catch (ParseException e) {
|
2021-11-01 23:08:04 +01:00
|
|
|
throw new JsonSyntaxException("Failed parsing '" + s + "' as Date; at path " + in.getPreviousPath(), e);
|
2011-09-26 23:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 23:08:04 +01:00
|
|
|
@Override public void write(JsonWriter out, Date value) throws IOException {
|
2011-10-14 05:17:12 +02:00
|
|
|
if (value == null) {
|
2011-12-03 20:46:25 +01:00
|
|
|
out.nullValue();
|
2011-10-14 05:17:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-11-01 23:08:04 +01:00
|
|
|
|
|
|
|
DateFormat dateFormat = dateFormats.get(0);
|
|
|
|
String dateFormatAsString;
|
|
|
|
synchronized (dateFormats) {
|
|
|
|
dateFormatAsString = dateFormat.format(value);
|
|
|
|
}
|
2011-12-03 20:46:25 +01:00
|
|
|
out.value(dateFormatAsString);
|
2011-09-26 23:44:08 +02:00
|
|
|
}
|
|
|
|
}
|