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;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
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
|
|
|
|
*/
|
2017-05-31 03:12:50 +02:00
|
|
|
final class DefaultDateTypeAdapter extends TypeAdapter<Date> {
|
2011-11-21 07:23:42 +01:00
|
|
|
|
2017-03-20 22:25:52 +01:00
|
|
|
private static final String SIMPLE_NAME = "DefaultDateTypeAdapter";
|
2017-05-31 03:12:50 +02:00
|
|
|
|
|
|
|
private final Class<? extends Date> dateType;
|
2011-11-21 07:23:42 +01:00
|
|
|
private final DateFormat enUsFormat;
|
|
|
|
private final DateFormat localFormat;
|
2017-03-20 22:25:52 +01:00
|
|
|
|
2017-05-31 03:12:50 +02:00
|
|
|
DefaultDateTypeAdapter(Class<? extends Date> dateType) {
|
|
|
|
this(dateType,
|
|
|
|
DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US),
|
2011-11-21 07:23:42 +01:00
|
|
|
DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT));
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:12:50 +02:00
|
|
|
DefaultDateTypeAdapter(Class<? extends Date> dateType, String datePattern) {
|
|
|
|
this(dateType, new SimpleDateFormat(datePattern, Locale.US), new SimpleDateFormat(datePattern));
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 03:12:50 +02:00
|
|
|
DefaultDateTypeAdapter(Class<? extends Date> dateType, int style) {
|
|
|
|
this(dateType, DateFormat.getDateInstance(style, Locale.US), DateFormat.getDateInstance(style));
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public DefaultDateTypeAdapter(int dateStyle, int timeStyle) {
|
2017-05-31 03:12:50 +02:00
|
|
|
this(Date.class,
|
|
|
|
DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US),
|
2011-11-21 07:23:42 +01:00
|
|
|
DateFormat.getDateTimeInstance(dateStyle, timeStyle));
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:12:50 +02:00
|
|
|
public DefaultDateTypeAdapter(Class<? extends Date> dateType, int dateStyle, int timeStyle) {
|
|
|
|
this(dateType,
|
|
|
|
DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US),
|
|
|
|
DateFormat.getDateTimeInstance(dateStyle, timeStyle));
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultDateTypeAdapter(final Class<? extends Date> dateType, DateFormat enUsFormat, DateFormat localFormat) {
|
|
|
|
if ( dateType != Date.class && dateType != java.sql.Date.class && dateType != Timestamp.class ) {
|
|
|
|
throw new IllegalArgumentException("Date type must be one of " + Date.class + ", " + Timestamp.class + ", or " + java.sql.Date.class + " but was " + dateType);
|
|
|
|
}
|
|
|
|
this.dateType = dateType;
|
2011-11-21 07:23:42 +01:00
|
|
|
this.enUsFormat = enUsFormat;
|
|
|
|
this.localFormat = localFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2011-11-21 07:23:42 +01:00
|
|
|
synchronized (localFormat) {
|
2017-05-31 03:12:50 +02:00
|
|
|
String dateFormatAsString = enUsFormat.format(value);
|
|
|
|
out.value(dateFormatAsString);
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:52:20 +01:00
|
|
|
@Override
|
2017-05-31 03:12:50 +02:00
|
|
|
public Date read(JsonReader in) throws IOException {
|
|
|
|
if (in.peek() != JsonToken.STRING) {
|
2011-11-21 07:23:42 +01:00
|
|
|
throw new JsonParseException("The date should be a string value");
|
|
|
|
}
|
2017-05-31 03:12:50 +02:00
|
|
|
Date date = deserializeToDate(in.nextString());
|
|
|
|
if (dateType == Date.class) {
|
2011-11-21 07:23:42 +01:00
|
|
|
return date;
|
2017-05-31 03:12:50 +02:00
|
|
|
} else if (dateType == Timestamp.class) {
|
2011-11-21 07:23:42 +01:00
|
|
|
return new Timestamp(date.getTime());
|
2017-05-31 03:12:50 +02:00
|
|
|
} else if (dateType == java.sql.Date.class) {
|
2011-11-21 07:23:42 +01:00
|
|
|
return new java.sql.Date(date.getTime());
|
|
|
|
} else {
|
2017-05-31 03:12:50 +02:00
|
|
|
// This must never happen: dateType is guarded in the primary constructor
|
|
|
|
throw new AssertionError();
|
2011-11-21 07:23:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:12:50 +02:00
|
|
|
private Date deserializeToDate(String s) {
|
2011-11-21 07:23:42 +01:00
|
|
|
synchronized (localFormat) {
|
|
|
|
try {
|
2017-05-31 03:12:50 +02:00
|
|
|
return localFormat.parse(s);
|
2015-11-23 07:11:41 +01:00
|
|
|
} catch (ParseException ignored) {}
|
2011-11-21 07:23:42 +01:00
|
|
|
try {
|
2017-05-31 03:12:50 +02:00
|
|
|
return enUsFormat.parse(s);
|
2015-11-23 07:11:41 +01:00
|
|
|
} 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() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2017-03-20 22:25:52 +01:00
|
|
|
sb.append(SIMPLE_NAME);
|
2011-11-21 07:23:42 +01:00
|
|
|
sb.append('(').append(localFormat.getClass().getSimpleName()).append(')');
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|